matlab数值分析程序--高等数学,数值代数的matlab实现-文字版, matlab电子书, 和matlab 有关的电子书:

8.2.4 牛顿(Newton)多项式求导及其MATLAB程序

8.2.4  牛顿(Newton)多项式求导及其MATLAB程序

利用牛顿插值多项式求导的MATLAB主程序

function [df,A,P]=diffnew(X,Y)

n=length(X);

A=Y;

for  j=2:n

    for i=n:-1:j

       A(i)=(A(i)- A(i-1))/(X(i)-X(i-j+1));

    end

  end

x0=X(1);df=A(2);chsh=1;m=length(A)-1;

  for  k=2:m

    chsh=chsh*(x0-X(k)); df=df+chsh*(A(k+1));

end

         P=poly2sym(A);

 

例8.2.9  根据下表给定的一组数据(X ,Y)写出 (8.30)式的具体形式及其精度和名称,并用它计算的近似值,取4位小数计算.

X

3.135 2    3.335 2    3.535 2    3.735 2    3.935 2    4.135 2    4.335 2    4.535 2   

Y

0.126 6    -0.060 2   -0.603 2   -0.998 0   -0.119 4    0.995 3    -0.654 2    0.158 1  

解 因为表中所给数据(X ,Y)是等差数列,公差为h=0.2, 即

x0=3.135 2, x1= x0+h, x2= x0+2h, x3= x0+3h, x4= x0+4h, x5= x0+5h, x6= x0+6h, x7= x0+7h.

输入程序

>>X=[3.1352,3.3352,3.5352,3.7352,3.9352,

4.1352,4.3352,4.5352 ];

Y=[0.1266,-0.0602,-0.6032,-0.9980,-0.1194,

0.9953,-0.6542  0.1581];

[df,A,P]=diffnew(X,Y)

运行后屏幕显示的近似值df和阶牛顿多项式P及其系数向量A如下

df = -0.2428

A =

0.1266  -0.9340  -4.4525  10.5083  16.1667  -72.4818  64.7309  108.6155

P=

633/5000*x^7-467/500*x^6-1781/400*x^5+1478916440133901/140737488355328*x^4+4550512123488957/281474976710656*x^3-27833/384*x^2+4555032337958703/70368744177664*x+7643132912526197/70368744177664

 

例8.2.10  设函数.利用(8.23)式的精度为阶前差公式、阶后差公式和阶中心差商公式计算的近似值,并与精确值比较,取14位小数计算,h=0.001.

解 (1)输入计算程序

>> x0=3.1352;h=0.001;

Xq=[3.1352, x0+h, x0+2*h, x0+3*h, x0+4*h];

Xh=[3.1352, x0-h, x0-2*h, x0-3*h, x0-4*h];

Xz=[3.1352, x0+h, x0-h, x0+2*h,x0-2*h];

Yq=sin(5*Xq.^2-21);

Yh=sin(5*Xh.^2-21); Yz=sin(5*Xz.^2-21);

[dfq,Aq,Pq]=diffnew(Xq,Yq),

[dfh,Ah,Ph]=diffnew(Xh,Yh), [dfz,Az,Pz]=diffnew(Xz,Yz)

syms x, f=sin(5.*x.^2-21) ; dy=diff(f,x)

运行后屏幕显示①阶前差公式Pq和它的系数向量Aq及其的近似值dfq;②阶后差公式Ph和它的系数向量Ah及其的近似值dfh;③阶中心差商公式Pz和它的系数向量Az及其的近似值dfz,导数的符号形式分别如下

dfq =

-31.09973888240658

Aq =

1.0e+003 *

  Columns 1 through 4

0.00012659805395 -0.03116184195521 -0.05190522279697  5.11405450158927

  Column 5

5.04316638931140

Pq=

4561175588748639/36028797018963968*x^4-8771278738603423/281474976710656*x^3-7305010688969305/140737488355328*x^2+2811481194788801/549755813888*x+5545020085856995/1099511627776

dfh =

-31.09973905786443

Ah =

1.0e+003 *

  Columns 1 through 4

0.00012659805395  -0.03102749818686  -0.08234817664867  5.02372948127046

  Column 5

8.97444729902917

Ph=

4561175588748639/36028797018963968*x^4-8733464329536931/281474976710656*x^3-5794737776087263/70368744177664*x^2+345228061216123/68719476736*x+5483510392960745/549755813888

dfz =

-31.09974389667719

Az =

1.0e+003 *

  Columns 1 through 4

0.00012659805395  -0.03116184195521  -0.06717188417315  5.08888712539359

  Column 5

7.53074172148027

Pz=

4561175588748639/36028797018963968*x^4-8771278738603423/281474976710656*x^3-4726801133312021/70368744177664*x^2+5595290566809831/1099511627776*x+2070034522136353/274877906944

dy =

10*cos(5*x^2-21)*x

(2)为了将的三种近似值与精确值比较,输入程序

>>  x=3.1352; dy =10*cos(5*x^2-21)*x, wuq=dfq- dy , wuh=dfh- dy, wuz=dfz- dy,

运行后屏幕显示值及其三种近似值分别与精确值的差如下

dy =                             wuq =

-31.09974488361178       6.001205203887139e-006

wuh =                            wuz =

5.825747354748501e-006   8.869345909407912e-007

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/3/3080204.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1