5 數值計算

2022-02-03 13:19:17 字數 2201 閱讀 6217

/*

romberg 求定積分

輸入:積分區間[a,b],被積函式 f(x,y,z)

輸出:積分結果

f(x,y,z)示例:

double f0( double x, double l, double t )

*/double integral(double a, double b, double (*f)(double x, double y, double z), double

eps,

double l, double

t)double romberg (double a, double b, double (*f)(double x, double y, double z), double

eps,

double l, double

t)h = b-a;

min = (int)(log(h*10.0)/log(2.0)); //

h should be at most 0.1

r[0][0] = ((*f)(a, l, t)+(*f)(b, l, t))*h*0.50

;i = 1

;temp2 = 1

;while (iif ((fabs(r[1][i-1]-r[0][i-2])min))

return r[1][i-1

];h *= 0.50

;temp2 *= 2

;for (j=0; j)

r[0][j] = r[1

][j];

}return r[1][max_n-1];}

double integral(double a, double b, double (*f)(double x, double y, double z), double

eps,

double l, double

t)

/*

牛頓法解多項式的根

輸入:多項式係數 c,多項式度數 n,求在[a,b]間的根

輸出:根

要求保證[a,b]間有根

*/double fabs( double

x )double f(int m, double c, double

x)int newton(double x0, double *r,

double c, double cp, int

n,double a, double b, double

eps)

x1 =x2;

i++;

}return0;

}double polynomial_root(double c, int n, double a, double b, double

eps)

if (a>b)

if ((!newton(a, &root, c, cp, n, a, b, eps)) &&(!newton(b, &root, c, cp, n, a, b, eps)))

newton((a+b)*0.5, &root, c, cp, n, a, b, eps);

free(cp);

if (fabs(root)return

fabs(root);

else

return

root;

}

/*

追趕法解週期性方程

週期性方程定義:| a1 b1 c1 ... | = x1

| a2 b2 c2 ... | = x2

| ... | * x = ...

| cn-1 ... an-1 bn-1 | = xn-1

| bn cn an | = xn

輸入:a,b,c,x

輸出:求解結果 x 在 x中

*/void

run()

a[n - 2] = -a[n - 2] - c[n - 2

];for (int i = n - 3; i >= 0; i --)

x[n - 1] -= (c[n - 1] * x[0] + a[n - 1] * x[n - 2

]);x[n - 1] /= (c[n - 1] * a[0] + a[n - 1] * a[n - 2] + b[n - 1

]);for (int i = n - 2; i >= 0; i --)

x[i] += a[i] * x[n - 1

];}

linux 數值計算

root fedora14 lxg a 1 root fedora14 lxg b 3 root fedora14 lxg echo a b 4 root fedora14 lxg echo a b 4 root fedora14 lxg echo a b 4 root fedora14 lxg e...

js數值計算

var nndjh new number ndjhs val var nbyxs new number cols 2 firstchild.value var nyxszb nbyxs nndjh obj.value nyxszb.tofixed 4 100 將值轉換成數值 除計算取4位小數,再 1...

Shell 數值計算

一 整數計算 shell中 let expr能進行整數運算。這個方法很方便。很容易使用,推薦使用 的用法 表示式 示例 root localhost shell protest echo 1 2 3 root localhost shell protest echo 1 2 2 root local...