# Interpolation by erajayathilake

VIEWS: 113 PAGES: 12

• pg 1
```									NUMERICAL METHODS
ASSIGNMENT #3

JAYATHILAKE K.G.J.M E/06/117 11/05/2008

Interpolation-Assignment 3
Problem 01
a) Matlab function to form the forward difference table. function diftab=fordiff1(y) j=2; for k=1:length(y) diftab(1,k)=y(k); end while (length(y)~=1) y1=(y (2: length(y))); y2=(y(1:length(y)-1)); y=y1-y2; for i=1:length(y) diftab(j,i)=y(i); end j=j+1; end diftab=diftab; y=the vector that is given as input b) Matlab function to compute y values function result1=fordiff2(x,dif,x1) result=1; result1=0; h=abs(x(1)-x(2)); %finding the step size for i=2:length(dif)-1 result=1; for j=1:i-1 result=(x1-x(j))*result; end result=((result*dif(i))/(factorial(i-1)*h)); result1=result1+result; end result1=result1+dif(1); x=the x vector that is given; dif =the required column of forward difference table result1=the final value of y %assigning the computed values to the diftab matrix %computing the differences %assigning the y vector to the first column of diftab matrix

x1=the x value to calculate corresponding y value

2

c) Matlab function to obtained the values for equation y=exp(-x2) function gener(k) %generating the data set x1=[-10:1:10]; y1=exp(-(x1.^2)) x2=[-10:0.1:10]; %calling the fordiff1 function diftab=fordiff1(y1); diff=diftab(:,k); i=1; %taking one column from the forward difference table

%computing y values for each x value for x=-10:0.1:10 result1(i)=fordiff2(x1,diff,x); i=i+1; end %plotting the obtained results plot(x2,result1,'b+-',x1,y1,'r*-'); grid on axis([-12 12 -12 12]); legend('Variation of x & y(Newtons Forward Difference Method)','y=exp(-x^2)'); title('Plot of x and y'); xlabel('values of x'); ylabel('value of y'); y2=exp(-(x2.^2)); % computing the errors a=1; for i=-10:0.1:10 error(a)=result1(a)-y2(a); a=a+1; end

plot(x2,error,'c*-'); %plotting the error vs. x values axis([-12 12 -12 12]); title('Plot of x and Interpolation errors at each point of x'); xlabel('values of x'); ylabel('Error'); grid on

k=position of xi

3

i.

Table 01: y=exp(-x2)
x -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 y 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0183 0.3679 1.0000 0.3679 0.0183 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000

 The above table shows the y values corresponding to each x value, obtained using the given function.

4

ii.

The graph generated using the values y and x, obtained from the developed Newton’s forward difference interpolation. Taking the Δy1 .(k=1)

Plot of x and y Variation of x & y(Newtons Forward Difference Method) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 1

Plot of x and Interpolation errors at each point of x

10

5

Error

0

-5

-10

-10

-5

0 values of x

5

10

Figure 2

5

 The figure2 illustrates that the error in the range of -5<x<5 is very small. But out of that range the error becomes very huge. iii. The below graphs plotted using the forward differences obtained from different positions of xi. When k=2,
Plot of x and y Variation of x & y(Newtons Forward Difference Method) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 3

When k=4,
Plot of x and y Variation of x & y(Newtons Forward Difference Method) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 4

6

When k=8,
Plot of x and y Variation of x & y(Newtons Forward Difference Method) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 5

When k=12,
Plot of x and y Variation of x & y(Newtons Forward Difference Method) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 6

7

 

When k=1 the interpolated y values have less errors. But, when k increases (i.e. with different position of xi) we get y values which are deviates from the required y value. It can be seen clearly from the figures3, 4, 5, and 6. The reason for this is decreasing the number of forward differences for the higher position of xi of forward difference table.



Problem 02 a. Matlab function to implement the Lagrange interpolation method function y=lagran(x) x1=[-10:1:10]; y1=exp(-(x1.^2)); n=length(x1); re=0; for i=1:n L=1; for j=1:n if (i~=j) L=L*(x-x1(j))/(x1(i)-x1(j)); end end re=re+y1(i)*L; end y=re;
 The matlab function to generate the x and y vector and to plot the graphs.
function gener1 x1=[-10:1:10]; i=1; for x=-10:0.1:10 y(i)=lagran(x); i=i+1; end y1=exp(-(x1.^2)); x2=[-10:0.1:10];

%generating the x and y vector

8

plot(x2,y,'b+-',x1,y1,'r*-'); grid on axis([-12 12 -12,12]); legend('Variation of x & y(Lagrangian Interpolation)','y=exp(-x^2)'); title('Plot of x and y'); y2=exp(-(x2.^2)); a=1; for i=-10:0.1:10 error(a)=y(a)-y2(a); a=a+1; end plot(x2,error,'b-'); xlabel('values of x'); grid on axis([-12 12 -12 12]); ylabel('Error'); title('Plot of x and Interpolation errors at each point of x'); xlabel('values of x'); ylabel('value of y');

b. See Table 01
Plot of x and y Variation of x & y(Lagrangian Interpolation) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 7

 The above figure7 shows plot of the x values in [-10 10] with a spacing of 0.1 and the corresponding y values obtained using the developed matlab function.

9

Plot of x and Interpolation errors at each point of x

10

5

Error

0

-5

-10

-10

-5

0 values of x

5

10

Figure 8

 The above figure is the plot of x values in [-10 10] with a spacing of 0.1 and the computed interpolation errors at each value of x. c. The values obtained for a data set which is not equally spaced. Table 02:y=exp(-x2) x -10 -8.5 -6.0 -4.1 -2.3 -1.9 -1.5 -1.1 -0.8 y 0.0000 0.0000 0.0000 0.0000 0.0050 0.0271 0.1054 0.2982 0.5273

10

-0.5 -0.1 -0.06 0.0 0.06 0.1 0.5 0.8 1.1 1.5 1.9 2 3 4.1 6.0 8.5 10

0.7788 0.9900 0.9964 1.0000 0.9964 0.9900 0.7788 0.5273 0.2982 0.1054 0.0271 0.0183 0.0001 0.0000 0.0000 0.0000 0.0000

11

Plot of x and y Variation of x & y(Lagrangian Interpolation) 10 y=exp(-x 2)

5

value of y

0

-5

-10

-10

-5

0 values of x

5

10

Figure 9

Plot of x and Interpolation errors at each point of x

10

5

Error

0

-5

-10

-10

-5

0 values of x

5

10

Figure10

 The above figure9 is the plot of x values of Table02 and the corresponding y values obtained using Lagrangian Interpolation.  The figure10 is interpolation errors plot.

12

```
To top