break
end
k=k+1;
end
以文件名sor.m保存。
程序:
A=[4-10-100;-14-10-10;0-14-10-1;-10-14-10;0-10-14-1;00-10-14];
b=[05-25-26]';
[x,k,flag]=sor(A,b,1e-4,1.334,100)
[x,k,flag]=sor(A,b,1e-4,1.95,100)
[x,k,flag]=sor(A,b,1e-4,0.95,100)
运行结果分别为:
x=
1.00001878481009
1.99998698322858
1.00001815013068
2.00000041318053
0.999991858543476
2.0000068413569
k=
12
flag=
OK!
x=
1.00708190171991
2.00285345528501
1.01400422955242
1.98988871374656
1.00950321530473
2.00707099515554
k=
100
flag=
OK!
x=
0.999961518309351
1.99995706825231
0.999963054838452
1.99996580572033
0.999971141727588
1.9999827300678
k=
16
flag=
OK!
5.用逆幂迭代法求
最接近于11的特征值和特征向量,准确到
。
解:
算法为结合原点平移的反幂法,编写MATLAB程序如下:
functiont=maxnorm(A)
n=length(A);
t=0;
fori=1:
n
ifabs(A(i))-abs(t)>=0
t=A(i);
end
end
function函数保存为maxnorm.m文件
程序:
A=[631;321;111];
v=[111]';
lam
(1)=maxnorm(v);
u=v/lam
(1);
fori=1:
20
fprintf('n=%d,x=[%f%f%f],lamda=%f\n',i-1,u
(1),u
(2),u(3),1/lam(i)+11);
v=(A-11*eye(3))\u;
lam(i+1)=maxnorm(v);
u=v/lam(i+1);
ifabs(1/lam(i+1)-1/lam(i))<1e-3;
break
end
end
运行结果如下:
n=0.000000,x=[1.0000001.0000001.000000],lamda=12.000000
n=1.000000,x=[1.0000000.6666670.424242],lamda=8.424242
n=2.000000,x=[1.0000000.5843680.284130],lamda=8.037233
n=3.000000,x=[1.0000000.5601180.243417],lamda=7.923772
n=4.000000,x=[1.0000000.5526220.230993],lamda=7.888859
n=5.000000,x=[1.0000000.5502720.227144],lamda=7.877961
n=6.000000,x=[1.0000000.5495330.225946],lamda=7.874545
n=7.000000,x=[1.0000000.5493000.225573],lamda=7.873473
故所求特征值为7.873473,特征向量为[1.0000000.5493000.225573]。
6.用经典R-K方法求解初值问题
(1)
,
,
;
(2)
,
,
。
和精确解
比较,分析结论。
解:
(1)编写MATLAB程序如下:
四阶R-K方法:
function[x,y,z]=nark4(dyfun1,dyfun2,xspan,y0,z0,h)
%y'=f(x,y,z),z'=g(x,y,z),y(x0)=y0,z(x0)=z0
%dyfun为函数f(x,y),xspan为求解区间[x0,xN],y0为初值y(x0),z0为初值z(x0),h为步长
%x返回节点,y返回数值解
x=xspan
(1):
h:
xspan
(2);
y
(1)=y0;
z
(1)=z0;
forn=1:
length(x)-1
k1=feval(dyfun1,x(n),y(n),z(n));
l1=feval(dyfun2,x(n),y(n),z(n));
k2=feval(dyfun1,x(n)+h/2,y(n)+h/2*k1,z(n)+h/2*l1);
l2=feval(dyfun2,x(n)+h/2,y(n)+h/2*k1,z(n)+h/2*l1);
k3=feval(dyfun1,x(n)+h/2,y(n)+h/2*k2,z(n)+h/2*l2);
l3=feval(dyfun2,x(n)+h/2,y(n)+h/2*k2,z(n)+h/2*l2);
k4=feval(dyfun1,x(n+1),y(n)+h*k3,z(n)+h*l3);
l4=feval(dyfun2,x(n+1),y(n)+h*k3,z(n)+h*l3);
y(n+1)=y(n)+h*(k1+2*k2+2*k3+k4)/6;
z(n+1)=z(n)+h*(l1+2*l2+2*l3+l4)/6;
end
程序保存为nark4.m文件。
程序:
dyfun1=inline('-2*y+z+2*sin(x)');
dyfun2=inline('y-2*z+2*cos(x)-2*sin(x)');
[x,y,z]=nark4(dyfun1,dyfun2,[0,10],2,3,0.01);
u=2*exp(-x)+sin(x);%u为y的真值
v=2*exp(-x)+cos(x);%v为z的真值
fori=1:
length(x)
err1=(y(i)-u(i));
err2=(z(i)-v(i));
plot(x(i),err1,'r-')
holdon
plot(x(i),err2,'g-')
end
运行结果:
(2)二阶R-K方法:
function[x,y,z]=nark2(dyfun1,dyfun2,xspan,y0,z0,h)
%y'=f(x,y,z),z'=g(x,y,z),y(x0)=y0,z(x0)=z0
%dyfun为函数f(x,y),xspan为求解区间[x0,xN],y0为初值y(x0),z0为初值z(x0),h为步长
%x返回节点,y返回数值解
x=xspan
(1):
h:
xspan
(2);
y
(1)=y0;
z
(1)=z0;
forn=1:
length(x)-1
k1=feval(dyfun1,x(n),y(n),z(n));
I1=feval(dyfun2,x(n),y(n),z(n));
k2=feval(dyfun1,x(n)+h,y(n)+h*k1,z(n)+h*I1);
I2=feval(dyfun2,x(n)+h,y(n)+h*k1,z(n)+h*I1);
y(n+1)=y(n)+h*(k1/2+k2/2);
z(n+1)=z(n)+h*(I1/2+I2/2);
end
程序保存为nark2.m文件。
程序:
dyfun1=inline('-2*y+z+2*sin(x)');
dyfun2=inline('998*y-999*z+999*cos(x)-999*sin(x)');
[x,y,z]=nark2(dyfun1,dyfun2,[0,10],2,3,0.0005);
u=2*exp(-x)+sin(x);%u为y的真值
v=2*exp(-x)+cos(x);%v为z的真值
fori=1:
length(x)
err1=(y(i)-u(i));
err2=(z(i)-v(i));
plot(x(i),err1,'r-')
holdon
plot(x(i),err2,'g-')
end
运行结果:
7.用有限差分法求解边值问题(h=0.1):
。
解:
通过差商逼近导数,将微分方程离散为差分方程
编写MATLAB程序如下:
a0=-1;b0=1;c0=1;d0=1;
h=0.1;
fori=1:
19;
x=a0+i*h;
q=-(1+x*x);
a(i)=1;b(i)=-2+h*h*q;c(i)=1;d(i)=0;
fprintf('%f%f%f%f\n',a(i),b(i),c(i),d(i))
end
运行结果如下:
1.000000-2.0181001.0000000.000000
1.000000-2.0164001.0000000.000000
1.000000-2.0149001.0000000.000000
1.000000-2.0136001.0000000.000000
1.000000-2.0125001.0000000.000000
1.000000-2.0116001.0000000.000000
1.000000-2.0109001.0000000.000000
1.000000-2.0104001.0000000.000000
1.000000-2.0101001.0000000.000000
1.000000-2.0100001.0000000.000000
1.000000-2.0101001.0000000.000000
1.000000-2.0104001.0000000.000000
1.000000-2.0109001.0000000.000000
1.000000-2.0116001.0000000.000000
1.000000-2.0125001.0000000.000000
1.000000-2.0136001.0000000.000000
1.000000-2.0149001.0000000.000000
1.000000-2.0164001.0000000.000000
1.000000-2.0181001.0000000.000000
采用追赶法解该线性方程组,编写MATLAB程序如下:
a=[1111111111111111111];
b=[-2.0181-2.0164-2.0149-2.0136-2.0125-2.0116-2.0109-2.0104-2.0101-2.0100-2.0101-2.0104-2.0109-2.0116-2.0125-2.0136-2.0149-2.0164-2.0181];
c=[1111111111111111111];
d=[-100000000000000000-1];
x=d;
n=length(x);
y
(1)=1;
y(21)=1;
forj=1:
n-1
mu=a(j)/b(j);
b(j+1)=b(j+1)-mu*c(j);
x(j+1)=x(j+1)-mu*x(j);
end
x(n)=x(n)/b(n);
forj=n-1:
-1:
1
x(j)=(x(j)-c(j)*x(j+1))/b(j);
end
form=1:
1:
19
y(m+1)=x(m);
end
h=-1:
0.1:
1;
plot(h,y);
grid