资源描述:
《计算方法的MATLAB实现》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、误差基础%ex01.mclear;digit=3;S0=log(6)-log(5);S0=vpa(S0,digit);S(1)=1-5*S0;S0=vpa(S(1),digit);forn=2:8S(n)=1/n-5*S(n-1);S(n)=vpa(S(n),digit);endS=double(S);fprintf('S0=%11f',double(S0));forn=1:8fprintf('S%1d=%11f',n,S(n));end____________________________
2、%ex02.mclear;digit=3;S(8)=(1/45+1/54)/2;S(8)=vpa(S(8),digit);forn=7:-1:1S(n)=1/(5*(n+1))-S(n+1)/5;S(n)=vpa(S(n),digit);endS0=1/5-S(1)/5;S0=vpa(S0,digit);S=double(S);fprintf('S0=%11f',double(S0));forn=1:8fprintf('S%1d=%11f',n,S(n));end_______________
3、__________________________%ex03.mclear;disp('用单精度计算10^8+1');result=single(1e8+1);fprintf('10^8+1=%f',result);fprintf('pressanykeytocontinue...');pause;disp('计算1+2+...+40+10^9')result=0;fork=1:40result=single(result+k);endresult=single(result+1e9);
4、fprintf('1+2+...+40+10^9=%f',result);fprintf('pressanykeytocontinue...');pause;disp('计算10^9+1+2+...+40')result=1e9;fork=1:40result=single(result+k);endfprintf('10^8+1+2+...+40=%f',result);_________________________________________________________
5、_______lagrange插值%textbookpage35functionyh=lagrange(x,y,xh)n=length(x);m=length(xh);x=reshape(x,n,1);%x=x(:);y=reshape(y,n,1);%y=y(:);xh=reshape(xh,m,1);%xh=xh(:);yh=zeros(m,1);c1=ones(1,n-1);c2=ones(m,1);fori=1:n,xp=x([1:i-1,i+1:n]);yh=yh+y(i)*prod((xh
6、*c1-c2*xp')./(c2*(x(i)*c1-xp')),2);end__________________________________________%Lagrangeinterpolation1functionyh=mylagrange1(x,y,xh)n=length(x)-1;x=reshape(x,1,n+1);y=reshape(y,1,n+1);yh=0;forj=0:nxp=x([1:j,j+2:n+1]);yh=yh+y(j+1)*prod((xh-xp)./(x(j+1)-
7、xp));end_______________________________________%Lagrangeinterpolation2functionyh=mylagrange1(x,y,xh)%xhcanbeavectorn=length(x)-1;m=length(xh);x=reshape(x,1,n+1);y=reshape(y,1,n+1);xh=reshape(xh,m,1);yh=zeros(m,1);forj=0:nxp=x([1:j,j+2:n+1]);tmp=(xh*ones
8、(1,n)-ones(m,1)*xp)./(ones(m,1)*(x(j+1)-xp));yh=yh+y(j+1)*prod(tmp,2);end____________________________________newton插值function[p,q]=chashang(x,y)n=length(x);x=reshape(x,n,1);p(:,1)=x;p(:,2)=reshape(y,n,1);forj=3:n+1p(1:n+2-j,j)=di