数值分析上机实验报告 下载本文

内容发布更新时间 : 2024/12/23 7:19:54星期一 下面是文章的全部内容请认真阅读。

哈工大A16公寓1214室 院士之家团队之作品

(Ps:请各位师兄弟姐妹们抄的时候注意改动一下,尽量不要太雷同)

function y=f(x); y=1/(1+25*x*x);

写成如上形式即可,下面给出主程序 Lagrange插值源程序:

n=input('将区间分为的等份数输入:\\n');

s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数 x=-1:0.01:1; f=0; for q=1:n+1;

l=1;%求插值基函数 for k=1:n+1; if k~=q;

l=l.*(x-s(k))./(s(q)-s(k)); else l=l; end end

f=f+Rf(s(q))*l;%求插值函数 end

plot(x,f,'r')%作出插值函数曲线 grid on hold on 分段线性插值源程序

clear

n=input('将区间分为的等份数输入:\\n');

s=[-1+2/n*[0:n]];%%%给定的定点,Rf为给定的函数 m=0; hh=0.001; for x=-1:hh:1; ff=0;

for k=1:n+1;%%%求插值基函数 switch k case 1

if x<=s(2);

l=(x-s(2))./(s(1)-s(2));

.11.

哈工大A16公寓1214室 院士之家团队之作品

(Ps:请各位师兄弟姐妹们抄的时候注意改动一下,尽量不要太雷同)

else l=0; end case n+1 if x>s(n);

l=(x-s(n))./(s(n+1)-s(n)); else l=0; end otherwise

if x>=s(k-1)&x<=s(k); l=(x-s(k-1))./(s(k)-s(k-1)); else if x>=s(k)&x<=s(k+1); l=(x-s(k+1))./(s(k)-s(k+1)); else l=0; end end end

ff=ff+Rf(s(k))*l;%%求插值函数值 end m=m+1; f(m)=ff; end

%%%作出曲线 x=-1:hh:1; plot(x,f,'r'); grid on hold on

三次样条插值源程序:(采用第一边界条件)

clear

n=input('将区间分为的等份数输入:\\n'); %%%插值区间 a=-1; b=1;

.12.

哈工大A16公寓1214室 院士之家团队之作品

(Ps:请各位师兄弟姐妹们抄的时候注意改动一下,尽量不要太雷同)

hh=0.001;%画图的步长

s=[a+(b-a)/n*[0:n]];%%%给定的定点,Rf为给定的函数 %%%%第一边界条件Rf\v=5000*1/(1+25*a*a)^3-50/(1+25*a*a)^4; for k=1:n;%取出节点间距 h(k)=s(k+1)-s(k); end

for k=1:n-1;%求出系数向量lamuda,miu la(k)=h(k+1)/(h(k+1)+h(k)); miu(k)=1-la(k); end

%%%%赋值系数矩阵A for k=1:n-1; for p=1:n-1; switch p case k A(k,p)=2; case k-1

A(k,p)=miu(p+1); case k+1

A(k,p)=la(p-1); otherwise A(k,p)=0; end end end

%%%%求出d阵 for k=1:n-1; switch k case 1

d(k)=6*f2c([s(k) s(k+1) s(k+2)])-miu(k)*v; case n-1

d(k)=6*f2c([s(k) s(k+1) s(k+2)])-la(k)*v; otherwise

d(k)=6*f2c([s(k) s(k+1) s(k+2)]);

.13.

哈工大A16公寓1214室 院士之家团队之作品

(Ps:请各位师兄弟姐妹们抄的时候注意改动一下,尽量不要太雷同)

end end

%%%%求解M阵 M=A\\d'; M=[v;M;v]; %%%% m=0; f=0; for x=a:hh:b; if x==a; p=1; else

p=ceil((x-s(1))/((b-a)/n)); end ff1=0; ff2=0; ff3=0; ff4=0; m=m+1;

ff1=1/h(p)*(s(p+1)-x)^3*M(p)/6; ff2=1/h(p)*(x-s(p))^3*M(p+1)/6;

ff3=((Rf(s(p+1))-Rf(s(p)))/h(p)-h(p)*(M(p+1)-M(p))/6)*(x-s(p)); ff4=Rf(s(p))-M(p)*h(p)*h(p)/6; f(m)=ff1+ff2+ff3+ff4 ; end

%%%作出插值图形 x=a:hh:b; plot(x,f,'k') hold on grid on

结果分析和讨论:

本实验采用函数f(x)?1进行数值插值,插值区间为[-1,1],给定节点为 21?25x.14.

xj=-1+jh,h=0.1,j=0,…,n。下面分别给出Lagrange插值,三次样条插值,线性插值的函

哈工大A16公寓1214室 院士之家团队之作品

(Ps:请各位师兄弟姐妹们抄的时候注意改动一下,尽量不要太雷同)

数曲线和数据表。图中只标出Lagrange插值的十次多项式的曲线,其它曲线没有标出,从数据表中可以看出具体的误差。

表中,L10(x)为Lagrange插值的10次多项式,S10(x),S40(x)分别代表n=10,40的三次样条插值函数,X10(x),X40(x)分别代表n=10,40的线性分段插值函数。

x f(x) L10(x) S10(x) S40(x) X10(x) X40(x)

-1.00000000000000 0.03846153846154 0.03846153846154 0.03846153846154 0.03846153846154 0.03846153846154 0.03846153846154 -0.95000000000000 0.04244031830239 1.92363114971920 0.04240833151040 0.04244031830239 0.04355203619910 0.04244031830239 -0.90000000000000 0.04705882352941 1.57872099034926 0.04709697585458 0.04705882352941 0.04864253393665 0.04705882352941 -0.85000000000000 0.05245901639344 0.71945912837982 0.05255839923979 0.05245901639344 0.05373303167421 0.05245901639344 -0.80000000000000 0.05882352941176 0.05882352941176 0.05882352941176 0.05882352941176 0.05882352941176 0.05882352941176 -0.75000000000000 0.06639004149378 -0.23146174989674 0.06603986172744 0.06639004149378 0.06911764705882 0.06639004149378 -0.70000000000000 0.07547169811321 -0.22619628906250 0.07482116198866 0.07547169811321 0.07941176470588 0.07547169811321 -0.65000000000000 0.08648648648649 -0.07260420322418 0.08589776360849 0.08648648648649 0.08970588235294 0.08648648648649 -0.60000000000000 0.10000000000000 0.10000000000000 0.10000000000000 0.10000000000000 0.10000000000000 0.10000000000000 -0.55000000000000 0.11678832116788 0.21559187891257 0.11783833017713 0.11678832116788 0.12500000000000 0.11678832116788 -0.50000000000000 0.13793103448276 0.25375545726103 0.14004371555730 0.13793103448276 0.15000000000000 0.13793103448276 -0.45000000000000 0.16494845360825 0.23496854305267 0.16722724315883 0.16494845360825 0.17500000000000 0.16494845360825 -0.40000000000000 0.20000000000000 0.20000000000000 0.20000000000000 0.20000000000000 0.20000000000000 0.20000000000000 -0.35000000000000 0.24615384615385 0.19058046675376 0.24054799403464 0.24615384615385 0.27500000000000 0.24615384615385 -0.30000000000000 0.30769230769231 0.23534659131080 0.29735691695860 0.30769230769231 0.35000000000000 0.30769230769231 -0.25000000000000 0.39024390243902 0.34264123439789 0.38048738140327 0.39024390243902 0.42500000000000 0.39024390243902 -0.20000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000 -0.15000000000000 0.64000000000000 0.67898957729340 0.65746969368431 0.64000000000000 0.62500000000000 0.64000000000000 -0.10000000000000 0.80000000000000 0.84340742982890 0.82052861660828 0.80000000000000 0.75000000000000 0.80000000000000 -0.05000000000000 0.94117647058824 0.95862704866073 0.94832323122810 0.94117647058824 0.87500000000000 0.94117647058824 0 1.00000000000000 1.00000000000000 1.00000000000000 1.00000000000000 1.00000000000000 1.00000000000000 0.05000000000000 0.94117647058824 0.95862704866073 0.94832323122810 0.94117647058824 0.87500000000000 0.94117647058824 0.10000000000000 0.80000000000000 0.84340742982890 0.82052861660828 0.80000000000000 0.75000000000000 0.80000000000000 0.15000000000000 0.64000000000000 0.67898957729340 0.65746969368431 0.64000000000000 0.62500000000000 0.64000000000000 0.20000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000 0.50000000000000

.15.