实验matlab计算30,计算方法实验结果及Matlab程序
Lagrange插值計算結果
1-1
n
x
0.75
1.75
2.75
3.75
4.75
5
0.5290
0.3733
0.1537
-0.0260
-0.0157
10
0.6790
0.1906
0.2156
-0.2315
1.9236
20
0.6368
0.2384
0.0807
-0.4471
-39.9524
1-2
n
x
-0.95
-0.05
0.05
0.95
5
0.3868
0.9512
1.0513
2.5858
10
0.3867
0.9512
1.0513
2.5857
20
0.3867
0.9512
1.0513
2.5857
2-1
n
x
-0.95
-0.05
0.05
0.95
5
0.5171
0.9928
0.9928
0.5171
10
0.5264
0.9975
0.9975
0.5264
20
0.5256
0.9975
0.9975
0.5256
2-2
n
x
-4.75
-0.25
0.25
4.75
5
1.1470
1.3022
1.8412
119.6210
10
-0.0020
0.7787
1.2841
115.6074
20
0.0087
0.7788
1.2840
115.5843
3-1
n
x
-0.95
-0.05
0.05
0.95
5
0.5239
0.9879
0.9879
0.5239
10
0.5257
0.9975
0.9975
0.5257
20
0.5256
0.9975
0.9975
0.5256
3-2
n
x
-0.95
-0.05
0.05
0.95
5
0.3868
0.9513
1.0513
2.5857
10
0.3867
0.9512
1.0513
2.5857
20
0.3867
0.9512
1.0513
2.5857
4-1
節點x
5
50
115
185
[1,4,9]
2.2667
-20.2333
-171.9000
-492.7333
[36,49,64]
3.1158
7.0718
10.1670
10.0388
[100,121,144]
4.4391
7.2850
10.7228
13.5357
[169,196,225]
5.4972
7.8001
10.8005
13.6006
Rungkuta4計算結果
>>
f=@f2;
1-1
1-2
>>
Rungkuta4(f,0,1,5,-1)
x=0.200000
y=-1.200000
x=0.400000
y=-1.400000
x=0.600000
y=-1.600000
x=0.800000
y=-1.800000
x=1.000000
y=-2.000000
>>
Rungkuta4(f,0,1,10,-1)
x=0.100000
y=-1.100000
x=0.200000
y=-1.200000
x=0.300000
y=-1.300000
x=0.400000
y=-1.400000
x=0.500000
y=-1.500000
x=0.600000
y=-1.600000
x=0.700000
y=-1.700000
x=0.800000
y=-1.800000
x=0.900000
y=-1.900000
x=1.000000
y=-2.000000
>>
Rungkuta4(f,0,1,20,-1)
x=0.050000
y=-1.050000
x=0.100000
y=-1.100000
x=0.150000
y=-1.150000
x=0.200000
y=-1.200000
x=0.250000
y=-1.250000
x=0.300000
y=-1.300000
x=0.350000
y=-1.350000
x=0.400000
y=-1.400000
x=0.450000
y=-1.450000
x=0.500000
y=-1.500000
x=0.550000
y=-1.550000
x=0.600000
y=-1.600000
x=0.650000
y=-1.650000
x=0.700000
y=-1.700000
x=0.750000
y=-1.750000
x=0.800000
y=-1.800000
x=0.850000
y=-1.850000
x=0.900000
y=-1.900000
x=0.950000
y=-1.950000
x=1.000000
y=-2.000000
>>
Rungkuta4(f,0,1,5,1)
x=0.200000
y=0.833339
x=0.400000
y=0.714292
x=0.600000
y=0.625006
x=0.800000
y=0.555561
x=1.000000
y=0.500004
>>
Rungkuta4(f,0,1,10,1)
x=0.100000
y=0.909091
x=0.200000
y=0.833334
x=0.300000
y=0.769231
x=0.400000
y=0.714286
x=0.500000
y=0.666667
x=0.600000
y=0.625000
x=0.700000
y=0.588236
x=0.800000
y=0.555556
x=0.900000
y=0.526316
x=1.000000
y=0.500000
>>
Rungkuta4(f,0,1,20,1)
x=0.050000
y=0.952381
x=0.100000
y=0.909091
x=0.150000
y=0.869565
x=0.200000
y=0.833333
x=0.250000
y=0.800000
x=0.300000
y=0.769231
x=0.350000
y=0.740741
x=0.400000
y=0.714286
x=0.450000
y=0.689655
x=0.500000
y=0.666667
x=0.550000
y=0.645161
x=0.600000
y=0.625000
x=0.650000
y=0.606061
x=0.700000
y=0.588235
x=0.750000
y=0.571429
x=0.800000
y=0.555556
x=0.850000
y=0.540541
x=0.900000
y=0.526316
x=0.950000
y=0.512821
x=1.000000
y=0.500000
2-1
2-2
>>
Rungkuta4(f,1,3,5,0)
x=1.400000
y=2.613943
x=1.800000
y=10.776313
x=2.200000
y=30.491654
x=2.600000
y=72.585599
x=3.000000
y=156.225198
>>
Rungkuta4(f,1,3,10,0)
x=1.200000
y=0.866379
x=1.400000
y=2.619741
x=1.600000
y=5.719895
x=1.800000
y=10.792018
x=2.000000
y=18.680852
x=2.200000
y=30.521598
x=2.400000
y=47.832366
x=2.600000
y=72.634504
x=2.800000
y=107.608852
x=3.000000
y=156.298257
>>
Rungkuta4(f,1,3,20,0)
x=1.100000
y=0.345910
x=1.200000
y=0.866622
x=1.300000
y=1.607181
x=1.400000
y=2.620311
x=1.500000
y=3.967602
x=1.600000
y=5.720879
x=1.700000
y=7.963772
x=1.800000
y=10.793502
x=1.900000
y=14.322936
x=2.000000
y=18.682927
x=2.100000
y=24.024989
x=2.200000
y=30.524356
x=2.300000
y=38.383459
x=2.400000
y=47.835905
x=2.500000
y=59.151004
x=2.600000
y=72.638926
x=2.700000
y=88.656573
x=2.800000
y=107.614264
x=2.900000
y=129.983333
x=3.000000
y=156.304772
>>
Rungkuta4(f,1,3,5,-2)
x=1.400000
y=-1.553989
x=1.800000
y=-1.383617
x=2.200000
y=-1.293402
x=2.600000
y=-1.237540
x=3.000000
y=-1.199548
>>
Rungkuta4(f,1,3,10,-2)
x=1.200000
y=-1.714245
x=1.400000
y=-1.555523
x=1.600000
y=-1.454520
x=1.800000
y=-1.384595
x=2.000000
y=-1.333316
x=2.200000
y=-1.294103
x=2.400000
y=-1.263145
x=2.600000
y=-1.238084
x=2.800000
y=-1.217381
x=3.000000
y=-1.199991
>>
Rungkuta4(f,1,3,20,-2)
x=1.100000
y=-1.833333
x=1.200000
y=-1.714285
x=1.300000
y=-1.625000
x=1.400000
y=-1.555555
x=1.500000
y=-1.500000
x=1.600000
y=-1.454545
x=1.700000
y=-1.416666
x=1.800000
y=-1.384615
x=1.900000
y=-1.357143
x=2.000000
y=-1.333333
x=2.100000
y=-1.312500
x=2.200000
y=-1.294117
x=2.300000
y=-1.277778
x=2.400000
y=-1.263158
x=2.500000
y=-1.250000
x=2.600000
y=-1.238095
x=2.700000
y=-1.227273
x=2.800000
y=-1.217391
x=2.900000
y=-1.208333
x=3.000000
y=-1.200000
3-1
3-2
3-3
>>
Rungkuta4(f,0,1,5,1/3)
x=0.200000
y=1.760000
x=0.400000
y=8.813333
x=0.600000
y=43.680000
x=0.800000
y=217.293333
x=1.000000
y=1084.320000
>>
Rungkuta4(f,0,1,10,1/3)
x=0.100000
y=0.122778
x=0.200000
y=0.079259
x=0.300000
y=0.104753
x=0.400000
y=0.166584
x=0.500000
y=0.253861
x=0.600000
y=0.362954
x=0.700000
y=0.492651
x=0.800000
y=0.642550
x=0.900000
y=0.812517
x=1.000000
y=1.002506
>>
Rungkuta4(f,0,1,20,1/3)
x=0.050000
y=0.127552
x=0.100000
y=0.056947
x=0.150000
y=0.040157
x=0.200000
y=0.046673
x=0.250000
y=0.065055
x=0.300000
y=0.091010
x=0.350000
y=0.122931
x=0.400000
y=0.160214
x=0.450000
y=0.202632
x=0.500000
y=0.250102
x=0.550000
y=0.302590
x=0.600000
y=0.360086
x=0.650000
y=0.422584
x=0.700000
y=0.490084
x=0.750000
y=0.562583
x=0.800000
y=0.640083
x=0.850000
y=0.722583
x=0.900000
y=0.810083
x=0.950000
y=0.902583
x=1.000000
y=1.000083
>>
Rungkuta4(f,0,1,5,1)
x=0.200000
y=5.197338
x=0.400000
y=25.376171
x=0.600000
y=125.486815
x=0.800000
y=625.312096
x=1.000000
y=3123.795151
>>
Rungkuta4(f,0,1,10,1)
x=0.100000
y=0.433139
x=0.200000
y=0.309660
x=0.300000
y=0.332325
x=0.400000
y=0.401414
x=0.500000
y=0.483074
x=0.600000
y=0.565435
x=0.700000
y=0.643989
x=0.800000
y=0.716722
x=0.900000
y=0.782499
x=1.000000
y=0.840526
>>
Rungkuta4(f,0,1,20,1)
x=0.050000
y=0.424979
x=0.100000
y=0.240456
x=0.150000
y=0.202168
x=0.200000
y=0.218439
x=0.250000
y=0.254812
x=0.300000
y=0.298291
x=0.350000
y=0.343929
x=0.400000
y=0.389795
x=0.450000
y=0.435096
x=0.500000
y=0.479463
x=0.550000
y=0.522688
x=0.600000
y=0.564629
x=0.650000
y=0.605166
x=0.700000
y=0.644194
x=0.750000
y=0.681613
x=0.800000
y=0.717328
x=0.850000
y=0.751251
x=0.900000
y=0.783296
x=0.950000
y=0.813383
x=1.000000
y=0.841437
>>
Rungkuta4(f,0,1,5,0)
x=0.200000
y=0.298646
x=0.400000
y=0.927220
x=0.600000
y=2.835477
x=0.800000
y=10.710885
x=1.000000
y=47.941446
>>
Rungkuta4(f,0,1,10,0)
x=0.100000
y=0.112055
x=0.200000
y=0.245117
x=0.300000
y=0.401778
x=0.400000
y=0.584097
x=0.500000
y=0.793822
x=0.600000
y=1.032418
x=0.700000
y=1.301015
x=0.800000
y=1.600321
x=0.900000
y=1.930521
x=1.000000
y=2.291157
>>
Rungkuta4(f,0,1,20,0)
x=0.050000
y=0.052595
x=0.100000
y=0.110409
x=0.150000
y=0.173709
x=0.200000
y=0.242749
x=0.250000
y=0.317772
x=0.300000
y=0.399014
x=0.350000
y=0.486702
x=0.400000
y=0.581054
x=0.450000
y=0.682276
x=0.500000
y=0.790556
x=0.550000
y=0.906069
x=0.600000
y=1.028968
x=0.650000
y=1.159384
x=0.700000
y=1.297422
x=0.750000
y=1.443157
x=0.800000
y=1.596634
x=0.850000
y=1.757860
x=0.900000
y=1.926802
x=0.950000
y=2.103383
x=1.000000
y=2.287480
Newton迭代法計算結果
1-1
1-2
2-1
2-2
Newton(f,1e-6,1e-4,10,pi/4)
ans =
0.739085178106
Newton(f,1e-6,1e-4,10,0.6)
ans =
0.588532742848
Newton(f,1e-6,1e-4,10,0.5)
ans =
0.567143165035
Newton(f,1e-6,1e-4,20,0.5)
ans =
0.566605704128
3-1
>>
Legendre(2)
ans =(3*x^2 - 1)/2
>>
Legendre(3)
ans =(x*(5*x^2 - 3))/2
>>
Legendre(4)
ans =(35*x^4 - 30*x^2 + 3)/8
>>
Legendre(5)
ans =(x*(63*x^4 - 70*x^2 +
15))/8
>>
Legendre(6)
ans =(231*x^6 - 315*x^4 + 105*x^2 -
5)/16
求得零點:
>>
Newton(f,1e-6,1e-6,10,-0.5)
>>
Newton(f,1e-6,1e-6,10,0.5)
>>
Newton(f,1e-6,1e-6,10,0.3)
>>
Newton(f,1e-6,1e-6,10,-0.3)
>>
Newton(f,1e-6,1e-6,10,0.6)
>>
Newton(f,1e-6,1e-6,10,-0.6)
ans
=-0.932469515098
ans
=0.932469515098
ans
=0.238619186084
ans =
-0.238619186084
ans
=0.661209399083
ans =
-0.661209399083
3-2
>>
Chebyshev(2)
ans =2*x^2 - 1
>>
Chebyshev(3)
ans =x*(4*x^2 - 3)
>>
Chebyshev(4)
ans =8*x^4 - 8*x^2 + 1
>>
Chebyshev(5)
ans =x*(16*x^4 - 20*x^2 + 5)
>>
Chebyshev(6)
ans =(2*x^2 - 1)*(16*x^4 - 16*x^2 +
1)
求得零點:
>>
Newton(f,1e-6,1e-6,10,1)
>>
Newton(f,1e-6,1e-6,10,-1)
>>
Newton(f,1e-6,1e-6,10,0.85)
>>
Newton(f,1e-6,1e-6,10,-0.85)
>>
Newton(f,1e-6,1e-6,10,-0.75)
>>
Newton(f,1e-6,1e-6,10,0.75)
ans
=0.965925826291
ans
=-0.965925826291
ans
=0.258819101812
ans
=-0.258819101812
ans
=-0.707106781187
ans
=0.707106781187
3-3
>>
Laguerre(2)
ans =(x^2 - 4*x + 2)/2
>>
Laguerre(3)
ans =-(x^3 - 9*x^2 + 18*x - 6)/6
>>
Laguerre(4)
ans =(x^4 - 16*x^3 + 72*x^2 - 96*x +
24)/24
>>
Laguerre(5)
ans =-(x^5 - 25*x^4 + 200*x^3 - 600*x^2 + 600*x -
120)/120
求得零點:
>>
Newton(f,1e-6,1e-6,10,0.1)
>>
Newton(f,1e-6,1e-6,10,1)
>>
Newton(f,1e-6,1e-6,10,4)
>>
Newton(f,1e-6,1e-6,10,7)
>>
Newton(f,1e-6,1e-6,10,12)
ans
=0.263560319715
ans
=1.41340309772
ans
=3.59642581677
ans
=7.08581000587
ans
=12.6408008443
3-4
>>
Hermite(2)
ans =2*(2*x^2 - 1)
>>
Hermite(3)
ans =4*x*(2*x^2 - 3)
>>
Hermite(4)
ans =4*(4*x^4 - 12*x^2 + 3)
>>
Hermite(5)
ans =8*x*(4*x^4 - 20*x^2 + 15)
>>
Hermite(6)
ans =8*(8*x^6 - 60*x^4 + 90*x^2 -
15)
源代碼:
function
[y0,l]=Lagrange(X,Y,x0)
% X,Y :已知的插值點坐標
% x0 :插值點
% y0 :Lagrange 插值多項式在x0 處的值
%輸入格式:x=-5:2:5;y=1./(1+x.^2);x0=0.75;Lagrange(x,y,x0)
%輸入格式:n=5;k=0:n;x=cos((2*k+1)*pi/(2*(n+1)));y=1./(1+x.^2);x0=-0.05;Lagrange(x,y,x0)
%輸入格式:
x=[1,4,9];y=sqrt(x);x0=5;Lagrange(x,y,x0)
m=length(X);
l=zeros(m,1);
y0=0;
for
i=1:m
l(i)=1;
for
j=1:m
if j~=i
l(i)=l(i)*(x0-X(j))/(X(i)-X(j));
end
end
y0=y0+Y(i)*l(i);
end
示例:
>>
x=-5:2:5;
>>
y=1./(1+x.^2);
>>
x0=0.75;
>>
Lagrange(x,y,x0)
ans =
0.5290
源代碼:
function []
=Rungkuta4(f,a,b,N,y0)
%f是微分方程右端句柄
%a,b是自變量的取值區間[a,b]的端點
%N是區間等分的個數
%y0表初值y(0)
%輸入格式:a=0;b=1;N=5;y0=-1;f=@f2;Rungkuta4(f,a,b,N,y0);
%修改文件f2.m改變微分方程
x0=a;
h=(b-a)/N;
for i=1:N
k1=h*feval_r(f,x0,y0);
k2=h*feval_r(f,x0+h/2,y0+k1/2);
k3=h*feval_r(f,x0+h/2,y0+k2/2);
k4=h*feval_r(f,x0+h,y0+k3);
x1=x0+h;
y1=y0+(k1+2*k2+2*k3+k4)/6;
fprintf('x=%f
y=%f\n',x1,y1);
x0=x1;
y0=y1;
end
f2.m
function f=f2(x,y)
%修改f改變微分方程
f=-20*(y-exp(x)*sin(x))+exp(x)*(sin(x)+cos(x));
示例:
>>
f=@f2;
>>
Rungkuta4(f,0,1,5,0)
x=0.200000
y=0.360871
x=0.400000
y=0.582591
x=0.600000
y=1.250356
x=0.800000
y=1.572875
x=1.000000
y=2.672419
源代碼:
function
x1=Newton(f,e1,e2,N,x0)
%f是方程句柄,
e1,e2是兩個精度
%N為最大迭代次數,
x0為初值
%修改文件f3.m改變方程
flag=1;
for
i=1:N
[F,DF]=feval_r(f,x0);
if(abs(F)
x1=x0;
break;
end
if(abs(DF)
flag=0;
break;
end
x1=x0-F/DF;
Tol=abs(x1-x0);
if(abs(Tol)
break;
end
x0=x1;
end
if(flag==0)
fprintf('計算失敗!');
end
x1=vpa(x1,12);
f3.m
function
[f,d]=f3(x)
f=
cos(x)-x;
d1='
cos(x)-x';
d=subs(diff(d1));%求一次導
Legendre.m:
function
p2=Legendre(n)
syms
x;
p0=1;
p1=x;
for
i=0:n-2
p2=(2*i+3)*x*p1/(i+2)-(i+1)*p0/(i+2);
p0=p1;
p1=p2;
end
p2=factor(p2);
Chebyshev.m:
function
t2=Chebyshev(n)
syms
x;
t0=1;
t1=x;
for
i=0:n-2
t2=2*x*t1-t0;
t0=t1;
t1=t2;
end
t2=factor(t2);
Laguerre.m:
function
l2=Laguerre(n)
syms
x;
l0=1;
l1=-x+1;
for
i=0:n-2
l2=(2*i+3-x)*l1/(i+2)-(i+1)*l0/(i+2);
l0=l1;
l1=l2;
end
l2=factor(l2);
Hermite:
function
h2=Hermite(n)
syms
x;
h0=1;
h1=2*x;
for
i=0:n-2
h2=2*x*h1-2*(i+1)*h0;
h0=h1;
h1=h2;
end
h2=factor(h2);
示例:
>>
f=@f3;
>>
Newton(f,1e-6,1e-4,10,pi/4)
ans =
0.739085178106
總結
以上是生活随笔為你收集整理的实验matlab计算30,计算方法实验结果及Matlab程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql中一个表怎么查询多以上的信息,
- 下一篇: php通过ip查询经纬度,php 获取客