Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Newton's Backward Interpolation in MATLAB

Newton's Backward Interpolation in MATLAB

X=[2 2.1 2.2 2.3];
Y=[1.7314 1.7811 1.8219 1.8535];
x=2.28;  
n=4;   
h=X(2)-X(1);
t=(x-X(n))/h;
y=Y(n);
for i=1:n-1
    d(i,1)=Y(i+1)-Y(i);
end
for j=2:n-1
    for i=1:n-j
        d(i,j)=d(i+1,j-1)-d(i,j-1)
    end
end
s=1;q=1;
for k=1:n-1
    s=s*(t+k-1);
    q=q*k;
    y=y+(s/q)*d(1,k);
end
y

Output :

>> back.m

d =

    0.0497   -0.0089   -0.0003   45.0000
    0.0408   -0.0092   10.0000         0
    0.0316  -10.0000         0         0

y =

    1.8443



Newton's Forward Interpolation in MATLAB

Newton's Forward Interpolation in MATLAB


X=[10 20 30 40 50];
Y=[600 512 439 346 243];
x=12;  
n=5;   
h=X(2)-X(1);
t=(x-X(1))/h;
y=Y(1);
for i=1:n-1
    d(i,1)=Y(i+1)-Y(i);
end
for j=2:n-1
    for i=1:n-j
        d(i,j)=d(i+1,j-1)-d(i,j-1)
    end
end
s=1;q=1;
for k=1:n-1
    s=s*(t-k+1);
    q=q*k;
    y=y+(s/q)*d(1,k);
end
y



Output :

>> forw.m

d =

   -88    15   -35    45
   -73   -20    10     0
   -93   -10     0     0
  -103     0     0     0


y =

  578.0080

Simpson's 1/3rd Rule in MATLAB

Below is the matlab code for simpson's 1/3rd rule :

x=[0 .2 .4 .6 .8 1]
n=6;
h=(x(6)-x(1))/5
for i=1:n
y(i)=1/(1+x(i)*x(i));
end
sum=h/3*(y(1)+y(6)+4*(y(2)+y(4))+2*(y(3)+y(5)))

Output :

>> simp

x =

         0    0.2000    0.4000    0.6000    0.8000    1.0000


h =

    0.2000


sum =

    0.7487

Trapezoidal Rule in MATLAB

Below is the matlab code for Trapezoidal Rule :

x=[0 .2 .4 .6 .8 1]
n=6;
h=(x(6)-x(1))/5
for i=1:n
y(i)=1/(1+x(i)*x(i));
end
sum=h/2*(y(1)+y(6)+2*(y(2)+y(3)+y(4)+y(5)))


Save the file with a .m extension.
Then on  the command window run the file by typing filename.m

The output the above program will be something like this :

x =

         0    0.2000    0.4000    0.6000    0.8000    1.0000


h =

    0.2000


sum =

    0.7837

Top