• 计算方法之用追赶法求线性方程组


    /*************************************
    * 用追赶法求线性方程组
    * 
    * |-             -| |-  -|   |-  -|
    * | 2  -1         | | x1 |   |  3 |
    * |-1   3  -2     | | x2 |   |  1 |
    * |    -2   4  -2 |*| x3 | = |  0 |
    * |        -2   5 | | x4 |   | -5 |
    * |-             -| |-  -|   |-  -|
    *
    **************************************/
    #include<stdio.h>
    #include<math.h>
    #include<conio.h>
    #include<string.h>
    #include<stdlib.h>
    
    #define n 5
    
    int main() {
    	float a[n] = { 0, 0, -1, -2, -2 };
    	float b[n] = { 0, 2, 3, 4, 5 };
    	float c[n] = { 0, -1, -2, -2, 0 };
    	float d[n] = { 0, 3, 1, 0, -5 };
    	float x[n] = { 0, 0, 0, 0, 0 };
    	float r[n] = { 0, 0, 0, 0, 0 };
    	float y[n] = { 0, 0, 0, 0, 0 };
    
    	float q;
    	int k;
    	r[1] = c[1] / b[1];
    	y[1] = d[1] / b[1];
    	for (k = 2; k < n - 1; k++) {
    		q = b[k] - r[k - 1] * a[k];
    		r[k] = c[k] / q;
    		y[k] = (d[k] - y[k - 1] * a[k]) / q;
    	}
    	y[n - 1] = (d[n - 1] - y[n - 2] * a[n - 1])
    			/ (b[n - 1] - r[n - 2] * a[n - 1]);
    	x[n - 1] = y[n - 1];
    	for (k = n - 2; k >= 1; k--)
    		x[k] = y[k] - r[k] * x[k + 1];
    	for (k = 1; k < n; k++)
    		printf("x[%d] = %f\n", k, x[k]);
    	return 0;
    }

  • 相关阅读:
    系统设计实现
    反编译
    android layout analyze
    2016.7.20-notes
    如何认识刘昊然?
    leetcode-中等-队列-二叉树的层次遍历
    leetcode-简单-栈-逆波兰表达式
    leetcode-简单-栈-有效的括号
    队列-链式队列
    队列-循环队列-顺序存储
  • 原文地址:https://www.cnblogs.com/java20130722/p/3206789.html
Copyright © 2020-2023  润新知