计算方法实验.docx
《计算方法实验.docx》由会员分享,可在线阅读,更多相关《计算方法实验.docx(15页珍藏版)》请在冰豆网上搜索。
计算方法实验
实验六
一、阅读理解下列程序,并在计算机上运行.
1.qjsh.m
functions=qjsh(a,x)%a为多项式系数向量(按降幂)
n=length(a);s=a
(1);
fork=1:
n
s=s*x+a(k);
end
(以
测试,并计算
的值)
2.wanshu.m
functionwanshu(n)%n为正整数
A=[];
form=1:
n
s=0;
fork=1:
m/2
ifrem(m,k)==0
s=s+k;
end
end
ifm==s
A=[A,m];
end
end
disp(A)
(对
测试,并对不同的
计算)
3.dazhe.m
price=input('请输入商品价格:
');
switchfix(price/100)
case{0,1}
rate=0;
case{2,3,4}
rate=3/100;
casenum2cell(5:
9)
rate=5/100;
casenum2cell(10:
24)
rate=8/100;
casenum2cell(25:
49)
rate=10/100;
otherwise
rate=14/100;
end
price=price*(1-rate)
(对各种商品价格进行计算)
4.num.m
%Classic“3n+1”problemfromnumbertheory.
while1
n=input(‘Entern,negativequits.’)
ifn<=0,break,end
whilen>1
ifrem(n,2)==0
n=n/2
else
n=3*n+1
end
end
end
5.dd.m
function[k,y]=dd(x,e)%x为迭代初值,e为精度
y
(1)=x;y
(2)=f(x);k=2;w=7/25;
whileabs(y(k)-y(k-1))>e
x=w*x+(1-w)*f(x);
k=k+1;
y(k)=x;
end
k=k-1;y
(2)=[];
functiony=f(x)%为子函数
y=1/(1+x);
(课本P266,对不同的初值及精度运行)
二、阅读理解下列程序,并在计算机上调试修改运行.
1.jc.m
functions=jc(n)%n为正整数
s=n;
fork=n:
1
s=(s+1)*k;
2.la.m(以课本P32例2、P33例3测试)
functiony=la(X,Y,x)
%X为节点向量,Y为节点相应的函数值向量,x为插值点
n=size(X);y=0;
fori=1:
n
k=1;
forj=1:
n
ifj~=i
k=k*(x-X(j))/(X(i)-X(j));
end
y=y+k*Y(i);
end
end
三、编程并在计算机上调试修改运行.
1.编写构造Newton插值多项式求值程序.要求:
输出差商表及计算结果;以课本P32例2、P33例3测试,并计算课本P6016(x=2.25)
2.选做题:
编写构造Neville逐步插值算法求值程序.要求:
输出逐步插值表及计算结果.以课本P38例6测试,并计算课本P6016(x=2.25)
一、
1、>>qjsh([379-23],1)
ans=
-4
2、>>wanshu(10)
6
>>wanshu(100)
628
3、请输入商品价格:
2000
price=
1840
请输入商品价格:
90
price=
90
请输入商品价格:
5000
price=
4300
请输入商品价格:
500
price=
475
4、Entern,negativequits.6
n=
6
n=
3
n=
10
n=
5
n=
16
n=
8
n=
4
n=
2
n=
1
Entern,negativequits.5
n=
5
n=
16
n=
8
n=
4
n=
2
n=
1
Entern,negativequits.-1
n=
-1
5、>>[k,y]=dd(5,0.001)
k=
6
y=
5.00001.52000.71130.61990.61800.6180
二、
1、
functions=jc(n)
s=n+1;
fork=n-1:
-1:
2
s=s*k+1;
end
>>jc(3)
ans=
9
>>jc(5)
ans=
153
>>jc(6)
ans=
873
2、
functiony=la(X,Y,x)%X为节点向量,Y为节点相应的函数值向量,x为插值点
n=length(X);y=0;
fori=1:
n
k=1;
forj=1:
n
ifj~=i
k=k*(x-X(j))/(X(i)-X(j));
end
end
y=y+k*Y(i);
end
>>la([100121144],[101112],115)
ans=
10.7228
>>X=[0.30.40.50.60.7];
>>Y=[0.29850.396460.493110.588130.68122];
>>la(X,Y,0.462)
ans=
0.4566
三、
1、
Way一:
functiony=Newton(x0,y0,x)
n=length(x0);
forj=1:
n
c(j)=y0(j);
end
fork=1:
n
fori=1:
n-k
c(i+k)=(c(i+k)-y0(i+k-1))/(x0(i+k)-x0(i));
end
forj=k+1:
n
y0(j)=c(j);
end
end
y=c(n);
form=n-1:
-1:
1
y=y*(x-x0(m))+c(m);
end
c
>>Newton([100121144],[101112],115)
c=
10.00000.0476-0.0001
ans=
10.7228
>>x0=[0.30.40.50.60.7];
>>y0=[0.298500.396460.493110.588130.68122];
>>Newton(x0,y0,0.462)
c=
0.29850.9796-0.0655-0.05330.0083
ans=
0.4566
>>Newton[-2-10123],[-5111725],2.25)
c=
-56-3100
ans=
10.1406
Way二:
functiony=newton2(X,Y,x)
n=length(X);
A=zeros(n);
A(:
1)=Y';
forj=2:
n
fori=j:
n
A(i,j)=(A(i,j-1)-A(i-1,j-1))/(X(i)-X(i-j+1));
end
end
y=A(n,n);
fori=n-1:
-1:
1
y=y*(x-X(i))+A(i,i);
end
A=[X'A]
>>newton2([100121144],[101112],115)
A=
100.000010.000000
121.000011.00000.04760
144.000012.00000.0435-0.0001
ans=
10.7228
>>X=[0.30.40.50.60.7];
>>Y=[0.298500.396460.493110.588130.68122];
>>newton2(X,Y,0.462)
A=
0.30000.29850000
0.40000.39650.9796000
0.50000.49310.9665-0.065500
0.60000.58810.9502-0.0815-0.05330
0.70000.68120.9309-0.0965-0.05000.0083
ans=
0.4566
>>newton2([-2-10123],[-5111725],2.25)
A=
-2-500000
-1160000
010-3000
1100100
2763100
325186100
ans=
10.1406
2、选做题:
Way一:
functiony=Neville(x0,y0,x)
n=length(x0);
forj=1:
n
c(j)=y0(j);
end
fork=1:
n
fori=1:
n-k
c(i+k)=y0(i+k-1)+(c(i+k)-y0(i+k-1))*(x-x0(i))/(x0(i+k)-x0(i));
end
forj=k+1:
n
y0(j)=c(j);
end
end
c
>>x0=[0.30.40.50.60.7];
>>y0=[0.29850.396460.493110.588130.68122];
>>Neville(x0,y0,0.462)
c=
Columns1through3
0.2985000000000000.4571952000000000.456537318000000
Columns4through5
0.4565576738400000.456558112762800
(补充:
求得是对角线上的值)
>>Neville([-2-10123],[-5111725],2.25)
c=
Columns1through3
-5.00000000000000020.500000000000000-20.937500000000000
Columns4through6
10.14062500000000010.14062500000000010.140625000000000
(补充:
求得是对角线上的值)
Way二:
functiony=neville1(X,Y,x)
n=length(X);
A=zeros(n);
A(:
1)=Y';
forj=2:
n
fori=j:
n
A(i,j)=A(i-1,j-1)+(x-X(i-j+1))*(A(i,j-1)-A(i-1,j-1))/(X(i)-X(i-j+1));
end
end
A=[X'A]
>>X=[0.30.40.50.60.7];
>>Y=[0.29850.396460.493110.588130.68122];
>>neville1(X,Y,0.462)
A=
Columns1through3
0.3000000000000000.2985000000000000
0.4000000000000000.3964600000000000.457195200000000
0.5000000000000000.4931100000000000.456383000000000
0.6000000000000000.5881300000000000.457002400000000
0.7000000000000000.6812200000000000.459665800000000
Columns4through6
000
000
0.45653731800000000
0.4565750140000000.4565576738400000
0.4564963540000000.4565587576000000.456558112762800
>>neville1([-2-10123],[-5111725],2.25)
A=
-2.0000-5.000000000
-1.00001.000020.50000000
01.00001.0000-20.9375000
1.00001.00001.00001.000010.140600
2.00007.00008.50009.437510.140610.14060
3.000025.000011.500010.375010.140610.140610.1406