You are on page 1of 16

1).

Lagrange Polynomial Interpolation:


n=3

n =5

n= 7

n =9

2). Newton Divided Difference


n =3

n=5

n =7

n =9

3). Natural Cubic Spline Interpolation


n =3
M = [0, 1.6292, 0]';
Max Error = 3.4914 e -004
rms error = 1.7457 e-004
n =5
M = [0, 15.8585, -9.9531, 5.0133, 0]
Max Error = 0.0050
rms error = 0.0026
n =7
M = [0, -122.0025, 81.8532, -33.5118, 9.7083, 0.5217, 0]
n=9
M = [0. -410.0398, 273.8347, -113.6068, 30.8816, -0.6481, -3.4242, 4,0481, 0]

Codes
1). Lagrange
clc
% Lagrange
n = input('enter the number of intermediate points, n :');
%
%
%
%
%

X is the array containing the n points


here for simplicity of the program, these points are taken
uniform distance
otherwise,uncomment the following command, if points need to be put
manually

% X = input('enter the data points : ');


X = zeros(n,1);
for i = 1:1:n
X(i) = -1 + (i-1)*(2/n);
end
% Y contains the value of actual function at above defined n points
Y = zeros(n,1);
for i = 1:1:n
% exp is the function to be approximated
Y(i) = exp(X(i));
end
% evaluating the values directly
x = -1;
p=1;
while x<=1;
% L is an array containing the lagrange coefficients
L = ones(n,1);
for i = 1:1:n
for j = 1:1:n
if i ~=j
L(i) = L(i)* (x- X(j))/(X(i)- X(j));
end
end
end
% F is the value of function approximate at x
F=0;
for k =1:1:n
F = F + Y(k)*L(k);
end
g = exp(x);
% plots the variation of function approximate F and value x
plot(x,F,'.')
hold on
plot(x,g,'.')
hold on
% error in approximate value
e = abs(g - F);
% plot the error

%
%

plot(x,e,'.')
hold on
x
F
e
% increment the p to increment x
p =p+1;
if x<=1
x = -1+(2/999)*(p-1);
else
break

end
end

2). Divided Difference


clc
% Divided Difference
n = input(' enter the number of points : ');
% X = input(' enter the points : ');
% Y = input(' enter the function values at the input points : ');
% x = input(' enter the point of evaluation : ');
% X = input('enter the data points : ');
% X is taken at uniform points for simplicity
X = zeros(n,1);
for i = 1:1:n
X(i) = -1 + (i-1)*(2/n);
end
% Y contains the value of actual function at above defined n points
Y = zeros(n,1);
for i = 1:1:n
% exp is the function to be approximated
Y(i) = exp(X(i));
end
F = ones(n,n);
for
end

i = 1:1:n
F(i,1) = Y(i);

x = -1;
t =1;
while x<=1

% finding the divided differences


for i = 2:1:n
for j = 2:i
F(i,j)= (F(i,j-1)- F(i-1,j-1))/(X(i)- X(i-j+1));
end
end
% finding the value fo function at x
fx = F(n,n);
for i = n-1:-1:1
fx = fx*(x - X(i)) + F(i,i);
end
g = exp(x)
% error
e= abs(g-fx);
% plots
plot(x,fx,'.')
hold on
plot(x,g,':')
hold on
plot(x,e,'.')
hold on
x
fx
e

t =t+1;
if x<=1
x = -1+(2/999)*(t-1);
else
break
end
end

3). Cubic Spline


clc
% cubic spline
n = input(' enter the number of points : ');
% generating the array of points
X = zeros(n,1);
for i = 0:1:n-1
X(i+1) = -1+i*2/(n-1);
end
% generating an array Y containing the value at X
Y = zeros(n,1);
for i = 1:1:n
Y(i) = exp(X(i));
end

Q = X;
H = Y;
b = zeros(n,1);
M = zeros(n,1);
% defining a matrix A containing coefficients of Array
% of double derivative value
A = zeros(n,n);
A(1,1)= 1;
A(n,n) = 1;
for i = 2:1:n-1
% A is a tridiagonal system
A(i,i-1)= (X(i)-X(i-1))/6;
A(i,i)= (X(i+1)- X(i-1))/3;
A(i,i+1)= (X(i+1)-X(i));
end
A;
% generating the RHS vector b
for i = 2:1:n-1
i;
b(i,1)= ((Y(i+1,1)- Y(i,1))/(X(i+1,1)-X(i,1))) - ((Y(i,1)- Y(i-1,1))/(X(i,1)X(i-1,1)));
end
b;
% Applying gaussian eleimination

to solve for M

a = size(A,1);
for i=1:1:a-1
[col index] = max(abs(A(i:a,i)));
col;
index = index+i-1;
if A(i,i)<col
C = A(i,:);
A(i,:)= A([index],:);
A([index],:)=C;
tem = b(i,:);
b(i,:) = b([index],:);
b([index],:) = tem;
end
for j = i+1:1:a
m(j) = A(j,i)/A(i,i);
A(j,:) = A(j,:) - A(i,:)*m(j);
b(j,:)= b(j,:)- b(i,:)*m(j);
end
end

K = zeros(a,1);
K(a)= b(a)/A(a,a);
for i= a-1:-1:1
s= b(i);
for j=i+1:1:a
s=s-A(i,j)*K(j);
end
K(i)=s/A(i,i);
end
M = zeros(n,1);
M = K
% Using the obtained M to get the values at various intervals
% in the defined function
k =1;
h=-1;
for x = -1:0.002:1
if(h<=x)
h=h+(2/(n-1));
k=k+1;
if(h==1)
break
end
end
s = ((((Q(k)-x)^3)*M(k-1) +((x - Q(k-1))^3)*(M(k)))/(6*(Q(k)- Q(k-1)))) +
(((Q(k)-x)*H(k-1) + (x-Q(k-1))*(H(k)))/(Q(k)- Q(k-1))) - (1/6)*(Q(k)-Q(k1))*((Q(k)-x)*M(k-1)+ (x-Q(k-1))*M(k));
e(k) = (exp(x))- s;
g = exp(x);
plot(x,g)
hold on
plot(x,s,'.')
hold on
plot(x,e(k))
hold on
hold on
end
disp('maximum error')
error = max(abs(e))
% root mean square error
E = e*e';
rms = (sqrt(E))/2

4). Least Square Fit


clc
% least square fit
n = input('enter the number of points : ');

A = zeros(n,n);
X = zeros(n,1);
b = zeros(n,1);
% generating the coefficient matrix A
for i =1:1:n
for j =1:1:n
A(i,j) = 1/((i-1)+(j-1)+1);
end
end
A
% generating the rhs vector b, by intergration
syms x;
for i =1:1:n
b(i,1) = int((x^(i-1))*exp(x),x,-1,1);
end
% solving for alphas by gauss elimination
for i=1:1:n-1
[col index] = max(abs(A(i:n,i)));
col;
index = index+i-1;
if A(i,i)<col
C = A(i,:);
A(i,:)= A([index],:);
A([index],:)=C;
tem = b(i,:);
b(i,:) = b([index],:);
b([index],:) = tem;
end
for j = i+1:1:n
m(j) = A(j,i)/A(i,i);
A(j,:) = A(j,:) - A(i,:)*m(j);
b(j,:)= b(j,:)- b(i,:)*m(j);
end
end
X = zeros(n,1);
X(n)= b(n)/A(n,n);
for i= n-1:-1:1
s= b(i);
for j=i+1:1:n
s=s-A(i,j)*X(j);
end
X(i)=s/A(i,i);
end
X;
% error

fy =0;
for y = -1:0.01:1;
for i =1:1:n
end
fy
end

fy =fy+(X(i)*y^(i-1));

You might also like