matlab 输入n,【关于Matlabn=input('\n请输入分点数n:n=');x=-1:2/(n-1):1;fx=1./(1.+25.*x.^2);plot(x,fx,'ko');holdo...

关于Matlabn=input('\n 请输入分点数n: n='); x=-1:2/(n-1):1; fx=1./(1.+25.*x.^2); plot(x,fx,'ko');hold on; y=polyfit(x,fx,n-1); cs=csapi(x,fx); x=-1:0.01:1; yy=polyval(y,x); fx='1/(1+25*x^2)'; fnplt(cs,'m-');hold on;fplot(fx,[-1,1],'b-');plot(x,yy,'g-');grid on legend('original points','cubic spline curve','exact function','interpolating polynomials') x=-1:0.01:1; fx=1./(1.+25.*x.^2); yy=polyval(y,x); s=max(abs(fx-yy)); fprintf('原函数与差值函数的最大误差为:'); disp(s) x=-1:2/(n-1):1; fx=1./(1.+25.*x.^2); x1=-1:0.01:1; fx1=1./(1.+25.*x1.^2); s1=csapi(x,fx,x1); s=max(abs(s1-fx1)); fprintf('三次样条函数与原函数的最大误差为:'); disp(s) 运行后 ??? Error using ==> run at 56 Error: The input character is not valid in MATLAB statements or expressions. 第2个! function coefs=cubicspline(x0,y0) m=length(x0); for i=1:m-1 h(i)=x0(i+1)-x0(i); end B=zeros(m-2,m-2); for i=1:m-2 for j=1:m-2 switch i-j case -1 B(i,j)=h(i+1); case 0 B(i,j)=2*(h(i)+h(i+1)); case 1 B(i,j)=h(i); end end end for i=1:m-1 dd(i)=(y0(i+1)-y0(i))/(x0(i+1)-x0(i)); end for i=1:m-2 c(i)=6*(dd(i+1)-dd(i)); end c=c'; x1=B\c; A=zeros(m,m); A(2,1)=h(1);A(m-1,m)=h(m-1); for i=2:m-1 for j=2:m-1 switch i-j case -1 A(i,j)=h(i); case 0 A(i,j)=2*(h(i)+h(i-1)); case 1 A(i,j)=h(i-1); end end end A, b(1)=0;b(m)=0; for i=2:m-1 b(i)=6*(dd(i)-dd(i-1)); end b=b', S1=[0.8;x1;0.2]; d=[0.8 0 0 0 0 0 0 0 0.2]'; c1=c-d; x2=B\c1; S=[0.8;x2;0.2], fprintf('样条曲线的系数矩阵为:\n'); for i=1:m-1 coef_a(i)=(S(i+1)-S(i))/(6*h(i)); coef_b(i)=S(i)/2; coef_c(i)=dd(i)-((2*h(i)*S(i)+h(i)*S(i+1))/6); coef_d(i)=y0(i); end coefs=[coef_a',coef_b',coef_c',coef_d']; x=[]; s=[]; for i=1:m-1 xx=linspace(x0(i),x0(i+1),30); M=length(xx); for k=1:M ss(k)=coef_a(i)*(xx(k)-x0(i))^3+coef_b(i)*(xx(k)-x0(i))^2+coef_c(i)*(xx(k)-x0(i))+coef_d(i); end s=[s,ss]; x=[x,xx]; end plot(x0,y0,'*',x,s,'b-');grid on 运行后 ??? Input argument "x0" is undefined. Error in ==> cubicspline at 2 m=length(x0); 哪出问题了?

作业帮用户2016-11-27举报

6c0a0adef10d1043fa4f41a7897345ed.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值