C++数值三次样条插值自动选取步长梯形法Romberg求积法列主元高斯消去法列主元LU分解法Jacobi迭Word文档下载推荐.docx
《C++数值三次样条插值自动选取步长梯形法Romberg求积法列主元高斯消去法列主元LU分解法Jacobi迭Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C++数值三次样条插值自动选取步长梯形法Romberg求积法列主元高斯消去法列主元LU分解法Jacobi迭Word文档下载推荐.docx(25页珍藏版)》请在冰点文库上搜索。
x=newdouble[n];
y=newdouble[n];
a=newdouble[n];
b=newdouble[n];
a1=newdouble[n];
b1=newdouble[n];
h=newdouble[n-1];
m=newdouble[n+1];
请输入"
n<
个插值的节点(xi,yi):
for(inti=0;
i<
n;
i++)
{
cin>
x[i]>
y[i];
}
for(intj=0;
j<
n-1;
j++)
h[j]=x[j+1]-x[j];
请输入待估点xx:
cin>
xx;
请选择边界条件:
choice;
switch(choice)
{
case1:
{
doubletemp1,temp2;
a[0]=0;
a[n-1]=1;
cout<
请输入边界条件的两个一阶微商值s'
(x1)与s'
(xn):
cin>
temp1>
temp2;
b[0]=2*temp1;
b[n-1]=2*temp2;
break;
}
case2:
a[0]=1;
a[n-1]=0;
b[0]=3/h[0]*(y[1]-y[0]);
b[n-1]=3/h[n-2]*(y[n-1]-y[n-2]);
for(intk=1;
k<
n-1;
k++)
a[k]=h[k-1]/(h[k-1]+h[k]);
b[k]=3*((1-a[k])/h[k-1]*(y[k]-y[k-1])+a[k]/h[k]*(y[k+1]-y[k]));
a1[0]=-a[0]/2;
b1[0]=b[0]/2;
for(intl=1;
l<
n;
l++)
a1[l]=-a[l]/(2+(1-a[l])*a1[l-1]);
b1[l]=(b[l]-(1-a[l])*b1[l-1])/(2+(1-a[l])*a1[l-1]);
m[n]=0;
for(j=n-1;
j>
=0;
j--)
m[j]=a1[j]*m[j+1]+b1[j];
//判别xx所在区间并输出结果
\n插值结果为:
;
for(k=0;
k<
k++)
if(x[k]<
=xx&
&
x[k+1]>
xx)
doubleoutput=0;
output=(1+2*(xx-x[k])/(x[k+1]-x[k]))*pow(((xx-x[k+1])/(x[k]-x[k+1])),2)*y[k]
+(1+2*(xx-x[k+1])/(x[k]-x[k+1]))*pow(((xx-x[k])/(x[k+1]-x[k])),2)*y[k+1]
+(xx-x[k])*pow(((xx-x[k+1])/(x[k]-x[k+1])),2)*m[k]
+(xx-x[k+1])*pow(((xx-x[k])/(x[k+1]-x[k])),2)*m[k+1];
cout<
output<
deletex;
deletey;
deletea;
deleteb;
deletea1;
deleteb1;
deleteh;
deletem;
}
运行结果截图:
2.三次样条插值(初值条件2):
P52.10、给定函数
0.25
0.3
0.39
0.45
0.53
0.5
0.5477
0.6245
0.6708
0.728
yangtiao.cpp(同上)
3.自动选取步长梯形法:
P97
7、使用自动选取步长梯形法计算积分
(给定ε=0.01)
SelfSelLength.cpp
doublefun(doublea)
return2/(1+a*a);
doubleSelfSelLength(doubleR_a,doubleR_b,doublee)
doubleh=(R_b-R_a)/2;
doubleR1=(fun(R_a)+fun(R_b))*h;
intn=1;
doubleR0;
doubleS;
doubleE;
do//每当误差值不符合要求时,计算下一个result值
R0=R1;
S=0;
for(intk=1;
=n;
k++)
S=S+fun(R_a+(2*k-1)*h/n);
R1=R0/2+S*h/n;
E=fabs(R1-R0);
n=2*n;
}while(E>
3*e);
returnR1;
doublea,b,e;
cout<
"
请依次输入待求积分函数的下界a、上界b及精度要求e:
<
endl;
a>
b>
e;
自动选取步长梯形法可求得积分:
SelfSelLength(a,b,e)<
4.Romberg求积法:
8、使用Romberg求积法计算积分
(给定ε=0.01,且取
)
Romberg.cpp
staticdoubleTri[128][128];
returnsqrt(a);
doubleRomberg(doubleR_a,doubleR_b,doublee)
Tri[0][0]=(R_b-R_a)/2*(fun(R_a)+fun(R_b));
intk=0;
do//每当误差值不符合要求时,计算下一行的Tri[][]值
k++;
doubletemp=0;
//计算T[0][k]的数值
for(inti=1;
i<
=pow(2,k-1);
i++)
temp=temp+fun(R_a+(2*i-1)*(R_b-R_a)/pow(2,k));
Tri[0][k]=0.5*(Tri[0][k-1]+(R_b-R_a)/pow(2,k-1)*temp);
for(intm=1;
m<
=k;
m++)
Tri[m][k-m]=(pow(4,m)*Tri[m-1][k-m+1]-Tri[m-1][k-m])/(pow(4,m)-1);
E=fabs(Tri[k][0]-Tri[k-1][0]);
e);
returnTri[k][0];
按Romberg求积法可求得积分:
Romberg(a,b,e)<
5.列主元高斯消去法:
测试矩阵为:
=
Guess_Elimination.cpp
//列主元高斯消去法
#defineN4//矩阵的维数,可按需更改
staticdoubleA[N][N]={2,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,-1,2};
//系数矩阵
staticdoubleB[N]={1,0,1,0};
//右端项
staticdoubleX[N];
inti,j,k;
//计数器
for(k=0;
k<
N-1;
//选取最大主元
intindex=k;
for(i=k;
N;
if(fabs(A[index][k])<
fabs(A[i][k]))
{
index=i;
}
//交换行
doubletemp;
for(i=k;
i++)
temp=A[index][i];
A[index][i]=A[k][i];
A[k][i]=temp;
temp=B[index];
B[index]=B[k];
B[k]=temp;
for(i=k+1;
N;
doubleT=A[i][k]/A[k][k];
B[i]=B[i]-T*B[k];
for(j=k+1;
j++)
A[i][j]=A[i][j]-T*A[k][j];
X[N-1]=B[N-1]/A[N-1][N-1];
for(i=N-2;
i>
=0;
i--)
doubleTemp=0;
for(intj=i+1;
j<
N;
j++)
Temp=Temp+A[i][j]*X[j];
X[i]=(B[i]-Temp)/A[i][i];
线性方程组的解(X1,X2,X3......Xn)为:
for(i=0;
cout<
X[i]<
6.列主元LU分解法:
LU_Decomposition.cpp
#defineN4//矩阵维数,可自定义
staticdoubleA[N][N];
//系数矩阵
staticdoubleB[N];
staticdoubleY[N];
//中间项
//输出
staticdoubleS[N];
//选取列主元的比较器
请输入线性方程组(ai1,ai2,ai3......ain,yi):
for(i=0;
for(intj=0;
j++)
cin>
A[i][j];
cin>
B[i];
for(k=0;
N;
//选列主元
for(i=k;
doubletemp=0;
for(intm=0;
k;
m++)
temp=temp+A[i][m]*A[m][k];
S[i]=A[i][k]-temp;
if(S[index]<
S[i])
//构造L、U矩阵
for(j=k;
m<
k;
m++)
temp=temp+A[k][m]*A[m][j];
A[k][j]=A[k][j]-temp;
//先构造U一行的向量
}
for(i=k+1;
for(intm=0;
{
temp=temp+A[i][m]*A[m][k];
A[i][k]=(A[i][k]-temp)/A[k][k];
//再构造L一列的向量
//求解LY=B
Y[0]=B[0];
for(i=1;
for(intj=0;
i;
temp=temp+A[i][j]*Y[j];
Y[i]=B[i]-temp;
//求解UX=Y
X[N-1]=Y[N-1]/A[N-1][N-1];
for(i=N-2;
=0;
i--)
for(intj=i+1;
temp=temp+A[i][j]*X[j];
X[i]=(Y[i]-temp)/A[i][i];
//打印X
7.简单迭代法(Jacobi迭代):
取精度为esp=0.001,最大迭代次数M=100
Jacobi.cpp
//输出比较项staticdoubleY[N];
//输出项
staticdoubleG[N];
//X=BX'
+G的G矩阵
doubleeps=0.001;
intM=100;
booldistance()
{//求两输出项的差的范数是否满足精度要求
doubletemp=0;
for(i=0;
i<
temp=temp+fabs(X[i]-Y[i]);
if(temp>
eps)
returnfalse;
else
returntrue;
//满足精度要求则结束程序
//形成迭代矩阵B,存放到A中
if(fabs(A[i][i])<
eps)
打印失败"
return;
doubleT=A[i][i];
for(j=0;
A[i][j]=-A[i][j]/T;
A[i][i]=0;
G[i]=B[i]/T;
intcounter=0;
while(counter<
M)
//迭代
for(i=0;
i<
for(j=0;
j<
temp=temp+A[i][j]*Y[j];
X[i]=G[i]+temp;
if(distance()==true)
else
//交换X,Y向量;
for(i=0;
Y[i]=X[i];
counter++;
迭代次数为:
counter<
次。
该线性方程组的解(X1,X2,X3......Xn)为:
8.Seidel迭代:
Seidel.cpp
//输出比较项
temp=temp+A[i][j]*X[j];
9.松弛法(SOR迭代)
取松弛系数w=1.46
SOR.cpp
staticdoubleA[N][N]={2,-1,0,0,-1,2,-1,0,0,-1,2,-1,0,0,