Numerical Analysis Assignments

question:

Calculate the value of the function using the Lagrange and Newton interpolation formulas, respectively.

For Newton interpolation, it is required to output the difference quotient table first, and the value of the 4th interpolation polynomial when calculating x=7.5, and compare it with the result of the 4th Lagrange interpolation

x

4

5

6

7

8

y

2.0000

2.23607

2.44949

2.64575

2.82843

Lagrange

function y = Lagrange(a,b,x,n)
%Lagrange插值法 a,b分别为x以及对应的f(x),x为插入数据,n为插值多项式次数
% a=4:8
% b=[2 2.23607 2.44949 2.64575 2.82843]
if length(a)~=length(b)
    error('输入数据不同维')
end
if n>=length(a)
    error('n值太大')
end
d=abs(x-a);
[~,idx]=sort(d);
S=0;
for i=1:n+1
    t1=1;
    t2=1;
    for j=1:n+1
        if j~=i
            t1=t1*(x-a(idx(j)));
            t2=t2*(a(idx(i))-a(idx(j)));
        end
    end
    S=S+t1/t2*b(idx(i));
end
y=S;
end

Newton

  •  Chashang_List
function A = Chashang_List(a,b)
%Newton插商计算函数
if length(a)~=length(b)
    error('输入数据不同维')
end
N=length(a);
A=zeros(N,N+1);
A(:,1)=a;
A(:,2)=b;
for j=3:N+1
    for i=j-1:N
        A(i,j)=(A(i,j-1)-A(i-1,j-1))/(A(i,1)-A(i-(j-2),1));
    end
end
ChaShang_List=A
end

function y = Newton(a,b,x,n)
% Newton插值法 a,b分别为x以及对应的f(x),x为插入数据,n为插值多项式次数
% a=4:8
% b=[2 2.23607 2.44949 2.64575 2.82843]
if n>=length(a)
    error('n值太大')
end
A=Chashang_List(a,b);
S=A(1,2);
X=cumprod(x-a);
for i=1:n
    S=S+A(i+1,i+2)*X(i);
end
y=S;
end

Euler 

function y = Euler (fun, x0, y0, h, b)
n = (b - x0)/h;
A = zeros (n+1, 4);
A(1,1) = x0;
A(1,2) = y0;
A(1,1) = y0;
for i = 1 : n 
A(i+1,1) = A (i,1) + h;
A(i+1,2) = A (i,2) + h*fun (A(i,1), A(i,2));
A(i+1,3) = A (i,2) + h*fun (A(i+1,1), A(i+1,2));
yp = A(i,4) + h*fun (A (i,1), A(i,4));
yc = A(i,4) + h*fun (A (i+1,1), yp);
A (i+1,4) = 1/2*(yp + yc);
end 
y = A;


fun=@(x,y)-y+x+1
y = Euler(fun, 0, 1, 0.5, 1)

y =

    1.0000    1.0000         0         0
    1.5000    1.5000    1.5000    0.8750
    2.0000    2.0000    2.0000    1.6094

Romberg

function y = Runge_Kutta (fun, x0, y0, h, b)
n = (b-x0)/h;
A = zeros (n+1, 6);
A(1,1) = x0;
A(1,6) = y0;

for i = 1 : n 
A(i+1,1) = A (i,1) + h;  
A(i+1,2) = fun (A(i,1), A(i,2));  %k1
A(i+1,3)=fun (A(i,1)+h/2, A(i, 6)+h/2* A(i+1,2));  %k2
A(i+1,4)=fun (A(i,1)+h/2, A(i, 6)+h/2* A(i+1,3));  %k3
A(i+1,5)=fm(A(i,1)+h, A(i,6)+h* A(i+1,4));  %k4
A(i+1,6)=A(i,6)+h/6*(A(i+1,2)+2*A(i+1,3)+2*A(i+1,4)+A(i+1,5));
end 
y = A;


fun=@(x,y)-3*y+2*x+2
y = Euler(fun, 0, 1, 0.1, 1)

y =

    1.0000    1.0000         0         0
    1.1000    1.1000    1.0900    0.3500
    1.2000    1.1900    1.1830    0.6278
    1.3000    1.2730    1.2681    0.8517
    1.4000    1.3511    1.3477    1.0355
    1.5000    1.4258    1.4234    1.1894
    1.6000    1.4980    1.4964    1.3211
    1.7000    1.5686    1.5675    1.4362
    1.8000    1.6380    1.6372    1.5390
    1.9000    1.7066    1.7061    1.6326
    2.0000    1.7746    1.7742    1.7193

 Use the iterative method to find the approximate root of the equation e^x  +10 x - 2 = 0, take the initial value x = 0, the iterative formula x_{_{k+1}}=\frac{2-e^{x_k}}{10}, the precision is required to be 0.5 *10^-3;

X0=0;
errf=1;
i=0;
while errf>0.5*10^(-3)
i=i+1;
x=(2-exp(X0))/10;
errf=abs((x-X0)/X0);
x0=x;
end

 

Guess you like

Origin blog.csdn.net/qq_53011270/article/details/131144045