欢迎来到天天文库
浏览记录
ID:44049866
大小:265.49 KB
页数:23页
时间:2019-10-18
《数值分析-代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、注:凡"iiKopen「C:\Users\Administrator\Desktop\data・拉格朗日插值.txt")”等里面的路径自己合理修改。1・1・拉格朗日插值(用一系列已知点求近似函数,并估计未知点的函数值)#include#includeintn;〃插值节点个数double*X,*Y;〃插值节点数组doublex;〃估计点voidread(){doubletemp;ifstrcamin;in.open(MC:\Users\Administrator\Desktop\data-拉格朗Fl插值.txt");if(!in){co
2、ut«Mcannotopenthefile!M«endl;exit(O);}in»n;X=newdoublefn];Y=ncwdoublc[n];for(inti=0;i>temp;Y[i]=temp;coiitvvtempvv”";}in»temp;x=temp;cout«,,x=n«temp«endl;in.close();}voidmain(){doublet,y=();rcad();for(intk=0;k3、[i])/(X[k]-X[i])*t;y=y+t*Y[k];}cout«Hy=n«y«endl;}1・2•埃特金逐步插值法(同上)#include#includedouble*X,*Y;〃插值节点(x,y)doublex;//带估点intn;//N个插值点,插n-1次。voidrcad(){doubletemp;ifstreamin;in.open(HC:\Users\Administrator\Desktop\data-埃特金插值.txtn);if(!in){cout«Mcannotopenthefile!H;exit(0);}in»n4、;X=newdouble[n];Y=newdoublefn];for(inti=0;ivn;i++){in»temp;X[i]=temp;cout«temp«Hn;in»temp;Y[iJ=temp;cout«temp«endl;}in»x;cout«nx=H«x«endl;in.closc();}voidmain(){read();for(intk=l;k5、elete[JX;delete[]Y;1・3三次样条插值(除了各插值点的函数值外,还需要知道两头的一阶导数值)#include〃不敢相信除了总是忘记调丿LIread外,居然一气呵成?#includedouble*X,*Y,*h,*a,*b,*m;//插值节点(x,y),步thdoublex,y;〃待估点intn;//插值节点数voidread(){ifstreamin;in.opcn("C:\Uscrs\Administrator\Dcsktop\data-三次样条插值.txt");if(!in){cout«Mcannotopenthefil6、e!";cxit(0);}in»n;X=newdouble[nj;Y=newdouble[n];m=newdoublefn];for(inti=0;ivn;i++){in»X[i]»Y[i];cout«X[iJ«*'n«Y[iJ«endl;}in»m[()]»mfn-l];cout«m[0]«HH«m[n-1]«endl;in»x;cout«Mx=M«x«endl;in.close();voidmain(){read();h=newdouble[n-lj;a=newdouble[n];〃使用1至n-2b=newdouble[n];〃使用1至n-2for(inti=0;i7、h[i]=X[i+1]-X[i];for(i=l;i<=n-2;i++){//先计算a[i],b[i]a[i]=h[i-l]/(h[i-l]+h[i]);b[i]=3*((l-a[i])*(Y[i]-Y[i-l])/h[i-l]+a[i]*(Y[i+l]-Y[i])/h[i]);}//+++++++++++++追赶法求m[1〜n・2]++++++++++++++++++++++double*A=newdoub
3、[i])/(X[k]-X[i])*t;y=y+t*Y[k];}cout«Hy=n«y«endl;}1・2•埃特金逐步插值法(同上)#include#includedouble*X,*Y;〃插值节点(x,y)doublex;//带估点intn;//N个插值点,插n-1次。voidrcad(){doubletemp;ifstreamin;in.open(HC:\Users\Administrator\Desktop\data-埃特金插值.txtn);if(!in){cout«Mcannotopenthefile!H;exit(0);}in»n
4、;X=newdouble[n];Y=newdoublefn];for(inti=0;ivn;i++){in»temp;X[i]=temp;cout«temp«Hn;in»temp;Y[iJ=temp;cout«temp«endl;}in»x;cout«nx=H«x«endl;in.closc();}voidmain(){read();for(intk=l;k5、elete[JX;delete[]Y;1・3三次样条插值(除了各插值点的函数值外,还需要知道两头的一阶导数值)#include〃不敢相信除了总是忘记调丿LIread外,居然一气呵成?#includedouble*X,*Y,*h,*a,*b,*m;//插值节点(x,y),步thdoublex,y;〃待估点intn;//插值节点数voidread(){ifstreamin;in.opcn("C:\Uscrs\Administrator\Dcsktop\data-三次样条插值.txt");if(!in){cout«Mcannotopenthefil6、e!";cxit(0);}in»n;X=newdouble[nj;Y=newdouble[n];m=newdoublefn];for(inti=0;ivn;i++){in»X[i]»Y[i];cout«X[iJ«*'n«Y[iJ«endl;}in»m[()]»mfn-l];cout«m[0]«HH«m[n-1]«endl;in»x;cout«Mx=M«x«endl;in.close();voidmain(){read();h=newdouble[n-lj;a=newdouble[n];〃使用1至n-2b=newdouble[n];〃使用1至n-2for(inti=0;i7、h[i]=X[i+1]-X[i];for(i=l;i<=n-2;i++){//先计算a[i],b[i]a[i]=h[i-l]/(h[i-l]+h[i]);b[i]=3*((l-a[i])*(Y[i]-Y[i-l])/h[i-l]+a[i]*(Y[i+l]-Y[i])/h[i]);}//+++++++++++++追赶法求m[1〜n・2]++++++++++++++++++++++double*A=newdoub
5、elete[JX;delete[]Y;1・3三次样条插值(除了各插值点的函数值外,还需要知道两头的一阶导数值)#include〃不敢相信除了总是忘记调丿LIread外,居然一气呵成?#includedouble*X,*Y,*h,*a,*b,*m;//插值节点(x,y),步thdoublex,y;〃待估点intn;//插值节点数voidread(){ifstreamin;in.opcn("C:\Uscrs\Administrator\Dcsktop\data-三次样条插值.txt");if(!in){cout«Mcannotopenthefil
6、e!";cxit(0);}in»n;X=newdouble[nj;Y=newdouble[n];m=newdoublefn];for(inti=0;ivn;i++){in»X[i]»Y[i];cout«X[iJ«*'n«Y[iJ«endl;}in»m[()]»mfn-l];cout«m[0]«HH«m[n-1]«endl;in»x;cout«Mx=M«x«endl;in.close();voidmain(){read();h=newdouble[n-lj;a=newdouble[n];〃使用1至n-2b=newdouble[n];〃使用1至n-2for(inti=0;i7、h[i]=X[i+1]-X[i];for(i=l;i<=n-2;i++){//先计算a[i],b[i]a[i]=h[i-l]/(h[i-l]+h[i]);b[i]=3*((l-a[i])*(Y[i]-Y[i-l])/h[i-l]+a[i]*(Y[i+l]-Y[i])/h[i]);}//+++++++++++++追赶法求m[1〜n・2]++++++++++++++++++++++double*A=newdoub
7、h[i]=X[i+1]-X[i];for(i=l;i<=n-2;i++){//先计算a[i],b[i]a[i]=h[i-l]/(h[i-l]+h[i]);b[i]=3*((l-a[i])*(Y[i]-Y[i-l])/h[i-l]+a[i]*(Y[i+l]-Y[i])/h[i]);}//+++++++++++++追赶法求m[1〜n・2]++++++++++++++++++++++double*A=newdoub
此文档下载收益归作者所有