学数值分析怎样进行编程

来源:百度知道 编辑:UC知道 时间:2024/05/16 11:19:16

当然可以用来编程了,比如数值分析中的龙贝格(Romberg)求积分的算法如下:
#include "iostream"
#include "cmath"
#include "iomanip"
using namespace std;
#define N 20
#define e 1E-10
typedef double TYPE;

TYPE f(TYPE x) {
if (x==0) return 1.0;
return sin(x)/x;
}

double T2n(double a,double b,int n=0,double Tn=0) {
double New=0;
double h;
if (n==0) {
h = b-a;
return (f(a)+f(b))*h/2;
}
h = (b-a)/n;
double x = a+h/2;
while (x<b) {
New += f(x);
x += h;
}
return (Tn+h*New)/2;
}

double Romberg(double a,double b) {
double T1,T2,S1,S2,C1,C2,R1,R2;
int k = 0,n=1;
cout<<setprecision(10)<<left;
cout<<"k "<<"T "<<" S "<<" C "<<" R "<<endl;
T1 = T2n(0,1);
cout<<setw(5)<<k<<setw(15)&l