You are on page 1of 8

Numerical Methods

and
Analysis Laboratory

Engr. Romulo C. Cruz Jr.


Professor

CEIT-04-501A

BY :

Kyle Hedrian Esplanada


MACHINE PROBLEM # 5
Title

LEARNING OUTCOMES
1. Develop a working program in Scilab that will determine the value of an unknown function by
Lagrange Interpolation Formula.

2. Develop a working program in Scilab that will determine the value of an unknown function by
Newton's Divided Difference Interpolation Formula

INTRODUCTION
We will now begin to discuss various techniques of interpolation. Given a set of discrete points, we
sometimes want to construct a function out of polynomials that is an approximation of another known (or
possibly unknown) function. Interpolations can be useful as the original function may not be readily
integrable or nicely differentiable, while polynomials are relatively easy to integrate and differentiate.
Sometimes we use interpolations because they're easier to work with in general for computations. Using a
straight line interpolating polynomial to approximate a function is generally not very practical because
many functions are curved. The accuracy of approximating the values of a function with a straight line
depends on how straight/curved the function is originally between these two points, and on how close we
are to the points (x0,y0) and (x1,y1). Interpolation is an estimation of a value within two known values in
a sequence of values. Newton’s divided difference interpolation formula is an interpolation technique
used when the interval difference is not same for all sequence of values. Suppose f(x0), f(x1), f(x2)
………f(xn) be the (n+1) values of the function y=f(x) corresponding to the arguments x=x0, x1, x2…xn,
where interval differences are not same

INSTRUCTION

Develop programs that will determine the equation of the best fit curve and plot its graph by (a) linear
regression, and (b) nonlinear regression.

The laboratory report shall include but is not limited to, the following: flow chart, source code of the
working program, sample output, individual member's contribution to the final output, etc.

Save your work in PDF with file name format ‘MP#-SECTION CODE-GROUP CODE’

FLOWCHART
Figure 1. Lagrange Interpolation Flowchart
Figure 2: Newton’s Divided Difference Interpolation Flowchart

SOURCE CODE
Lagrange Interpolation Source Code
clc
clear
printf('\t\tTHIS PROGRAM APPROXIMATES THE VALUE OF \n\t\t\tAN UNKNOWN
FUNCTION BY\n\t\t LAGRANGE INTERPOLATION FORMULA\n\t\t(Prepared by: Kyle
Hedrian Esplanada)\n\n')
x=input('Enter data points of x: ');
y=input('Enter data points of y: ');
xn=input('Enter the interpolating point x: ')
n=length(x);
printf('\n\tTHE TABULAR REPRESENTATION OF DATA SET:\n\n\t\ti\t x\t y\n\t
------------------------------\n')
for z=1:n
printf('\t\tx%i\t%0.1f\t %0.4f\n',z-1,x(z),y(z))
end
P=0
p1=1
p2=1
for j=1:n
pi=1;
for i=1:n
if i~=j
p1=p1*(xn-x(i));
p2=p2*(x(j)-x(i));
end
end
L(j)=p1/p2
p1=1
p2=1
end
printf('\n\tTHE LAGRANGE POLYNOMIAL VALUES ARE:\n')
for z=1:n
printf('\n\t\tL%i(x) = %0.4f',z-1,L(z))
P=P+y(z)*L(z)
end
printf('\n\n\tTHEREFORE, THE INTERPOLATED VALUE OF x(%0.2f) IS:\n\t\tp(x) = %0.4f\
n',xn,P)
printf('\n\n\t Thank you\n\n')
Newton’s Divided Difference
clc
clear
printf('\t\tTHIS PROGRAM APPROXIMATES THE VALUE OF \n\t\t\tAN UNKNOWN FUNCTION BY\n\t
NEWTON DIVIDED DIFFERENCE INTERPOLATION FORMULA\n\t\t\t(Prepared by: Kyle Hedrian
Esplanada)\n\n')
x=input('Enter data points of x: ');
y=input('Enter data points of y: ');
xn=input('Enter the interpolating point x: ')
n=length(x)
for i=1:n
d(i,1)=y(i)
end
for j=2:n
for i=1:(n-j+1)
d1=d(i+1,j-1)-d(i,j-1)
d2=(x(i+j-1)-x(i))
d(i,j)=d1/d2
end
end
d(n,1)=0;
printf('\n\t\t THE TABULAR REPRESENTATION OF DATA SET:\n\n\t x \ty\t f(x0,x1)\tf(x0,x1,x3)
f(x0,x1,x2,x3)\n ---------------------------------------------------------------\n')
for j=1:n
printf('\t%0.2f %0.4f\t %0.4f\t %0.4f\t %0.4f\n',x(1,j),y(1,j),d(j,2),d(j,3),d(j,4))
end
for i=1:n-1
F(i)=prod(xn-x(:,1:i))*d(1,i+1)
end
f=y(1)+sum(F)
printf('\n\n\t\tTHEREFORE, THE APPROXIMATED VALUE OF x(%0.2f) IS:\n\t\t\tf(x) = %0.4f\n',xn,f)
printf('\n\n\t\tThank You\n\n')
SAMPLE OUTPUT

Figure 3: Lagrange Interpolation Sample Output


Figure 4: Newton’s Divided DIfference Sample Output

You might also like