numerical

来源:互联网 发布:淘宝拍摄摄影学校 编辑:程序博客网 时间:2024/05/16 06:26
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 100;const int linit = 1000;double coe_array[maxn][maxn + 1], l[maxn], ans[maxn];int n;void gauss_array(int target_col, int from_row) {    int main_row = target_col;    for(int i = from_row; i <= n; i++) {        l[i] = coe_array[i][target_col] / coe_array[main_row][target_col];        for(int j = 1; j <= n + 1; j++)            coe_array[i][j] = coe_array[i][j] - coe_array[main_row][j] * l[i];    }}void gauss() {    int col = 1;    for(int i = 2; i <= n; i++)        gauss_array(col++, i);  //delete which column and from which row}void get_ans() {    int cnt = 1;    for(int i = n - 1; i > 0; i--) {        int tmp = 0;        for(int j = n; j > (n - cnt); j--)            tmp += coe_array[i][j] * ans[j];        ans[i] = (coe_array[i][n + 1] - tmp) / coe_array[i][n - cnt];        cnt++;    }}int main(){    while(~scanf("%d", &n) && n) {        memset(coe_array, 0, sizeof(coe_array));        memset(ans, 0, sizeof(ans));        memset(l, 0, sizeof(l));        for(int i = 1; i <= n; i++)        for(int j = 1; j <= n + 1; j++)            cin >> coe_array[i][j];        gauss();        ans[n] = coe_array[n][n+1] / coe_array[n][n];        get_ans();        for(int i = 1; i <= n; i++) cout << ans[i] << " ";    }    return 0;}
//二分法求解方程f(x)=x3+4x2-10=0#include <iostream>#include <algorithm>#include <cstdio>#define acc 0.000001using namespace std;int cnt = 0;inline double result(double x) {    return x * x * x + 4 * x * x - 10;}double binary_find(double Left, double Right) {    double mid = (Left + Right) / 2;    cout << "第" << ++cnt << "次二分求解得到结果为:" << mid << endl;    if(Right - mid < acc)   return mid;    else {        if(result(Left) * result(mid) < 0)  return binary_find(Left, mid);        if(result(Right) * result(mid) < 0) return binary_find(mid, Right);    }}int main(){    double Left = 1.0, Right = 2.0;    printf("%.5lf", binary_find(Left, Right));    return 0;}
//迭代法#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#define acc 0.000001using namespace std;int cnt = 0;inline double result(double x) {    return sqrt(10 / (4 + x));}double iteration(double a) {    double next = result(a);    printf("第%d次计算结果为:%.5lf\n", ++cnt, a);    if(a - next > -acc && a - next < acc)   return a;    else return iteration(next);}int main(){    double ans = 0.5;    printf("%.5lf", iteration(ans));    return 0;}
//牛顿法#include <iostream>#include <cstdio>#define acc 0.000001using namespace std;inline double iterat_equation(double x) {    return x - (x*x*x + 4*x*x - 10) / (3*x*x + 8*x);}double newton(double x) {    double next_x = iterat_equation(x);    if(next_x - x < acc && next_x - x > -acc)   return x;    else    return newton(next_x);}int main(){    double ans = 1.5;    printf("%.8lf", newton(ans));    return 0;}
//割线法#include <iostream>#include <cstdio>#define acc 0.000001using namespace std;inline double f(double x) {    return x*x*x + 4*x*x - 10;}inline double iterat_equotion(double x, double last_x) {    return x - f(x) / (f(x) - f(last_x)) * (x - last_x);}double solve(double x, double last_x) {    double next_x = iterat_equotion(x, last_x);    if(next_x - x > -acc && next_x - x < acc)   return x;    else    return solve(next_x, x);}int main(){    double x0 = 1.5, x1 = 1.0;    printf("%.5lf", solve(x0, x1));    return 0;}
0 0
原创粉丝点击