数值分析MATLAB编程

题目1:用Gauss消去法解方程组

??1+2??2+3??3=14

3??1+??2+5??3=20

2??1+5??2+2??3=18

解:用MATLAB编写的程序如下: A=[1 2 3;3 1 5;2 5 2]; b=[14 20 18]'; [m,n]=size(A);

if rank(A)~=rank([A,b]) error return; end c=n+1; A(:,c)=b; for k=1:n-1

A(k+1:n,k:c)=A(k+1:n,k:c)-(A(k+1:n,k)/A(k,k))*A(k,k:c); end

x=zeros(length(b),1); x(n)=A(n,c)/A(n,n); for k=n-1:-1:1

x(k)=(A(k,c)-A(k,k+1:n)*x(k+1:n))/A(k,k); end

disp('x='); x=

disp(x);

题目2:已知 4=2, 9=3,用线性插值法求 7的近似值。 >>x0=4:9; >> y0=x0.^0.5; >> x=4:1:9; >> y=x.^0.5;

>> y2=interp1(x0,y0,x);

>>figure(2),plot(x,y,'b',x,y2,'k:'),grid >>disp('y2=') y2=

>>disp(y2)

2.0000 2.2361 2.4495 2.6458 2.8284 3.0000

题目三:设f ?? =????,在[-1,1]上用Legendre多项式作f ?? 的3次最佳平方逼近多项式。 >> a=-1;b=1; >> n=3; >>syms x; >>fx=exp(x); >>exp(x); >>for k=3 F=x^k*fx;

d(k+1)=int(F,a,b); end

>> d=reshape(d,n+1,1); >> H=hilb(n+1); >> a=H\\d a =

-3696*exp(-1)+536*exp(1) 42900*exp(-1)-6180*exp(1) -105240*exp(-1)+15120*exp(1) 69300*exp(-1)-9940*exp(1) >>vpa(a) ans =

97.3166454843974255360582301282 -1016.953673622023958078606833033 2384.788857418174033934624995061 -1525.676101701956752911460193057

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4