0% found this document useful (0 votes)
290 views11 pages

MExer01 Final

Using Lagrange and Newton interpolation, the value of y at x=3.5 is calculated to be 7.7422 from the given data set. Similarly, the value of y at x=8 is calculated to be 10.7345 using both interpolation methods on a different data set centered around the point of interest. Lastly, the value of the Bessel function J1(2.1) is estimated using 3rd and 4th order interpolation and the percent relative errors are determined.

Uploaded by

johannie uka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
290 views11 pages

MExer01 Final

Using Lagrange and Newton interpolation, the value of y at x=3.5 is calculated to be 7.7422 from the given data set. Similarly, the value of y at x=8 is calculated to be 10.7345 using both interpolation methods on a different data set centered around the point of interest. Lastly, the value of the Bessel function J1(2.1) is estimated using 3rd and 4th order interpolation and the percent relative errors are determined.

Uploaded by

johannie uka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

 MExer01

1. The following data come from a table that was measured with high precision. Use the best numerical
method to determine 𝑦 at 𝑥 = 3.5. Note that a polynomial will yield an exact value. Your solution
should prove that your result is exact.

𝒙 0 1.8 5 6 8.2 9.2 12


𝑦 26 16.415 5.375 3.5 2.015 2.54 8

function fp=lagrange_interpolation(x,y,p)

n=size(x,2);
L=ones(n,size(p,2));

for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(p-x(j))/(x(i)-x(j));
end
end
end
d=zeros(n,1);
for i=1:n
d(i,:)=d(i,:)+y(i)*L(i,:);

end
disp(d)
fp=sum(d);

end

>>x = [0 1.8 5 6 8.2 9.2 12]

x=

0 1.8000 5.0000 6.0000 8.2000 9.2000 12.0000


>> y = [26 16.415 5.375 3.5 2.015 2.54 8]

y=

26.0000 16.4150 5.3750 3.5000 2.0150 2.5400 8.0000


>> lagrange_interpolation(x,y,3.5)
-0.7721 4.1981 12.0951 -6.6826 1.5516 -0.8837 0.0874
ans =

9.5937
2. Use Newton’s interpolating polynomial to determine 𝑦 at 𝑥 = 3.5 to the best possible accuracy.
Compute the finite divided differences and order your points to attain optimal accuracy and
convergence. That is, the points should be centered around and as close as possible to the unknown.

𝒙 0 1 2.5 3 4.5 5 6
𝑦 2 5.4375 7.3516 7.5625 8.4453 9.1875 12

a) Matlab

function yInt = NewtonInterpolation(x, y, xx)


% NewtonInterpolation(x, y, xx): Uses an (n-2)order Newton interpolating
% polynomial based on n data points (x,y)
% to determine a value of a dependent variable (yInt)
% at a given value of the independent variable, xx
% input:
% x - independent variable (row matrix)
% y - dependent variable (row matrix)
% xx - value of independent variable at which interpolation is calculated
% output:
% yInt - interpolated value of dependent variable

%compute
n = length(x);
if length(y) ~= n
error('x and y must have the same length');
end
b = zeros(n,n);
%assign dependent variables to the first column of b
b(:,1)=y(:); %ensures that y is a column vector
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end

fprintf('\n\t\t x\t\t\tfx');
for k = 1:n-1

if k==1
fprintf('%10.0fst',k);
end
if k==2
fprintf('%10.0fnd',k);
end
if k==3
fprintf('%10.0frd',k);
end
if k>3
fprintf('%10.0fth',k);
end

end
l = b';
v = [x;l];
result = v';
for k = 1:n
fprintf('\n');
fprintf('%10.5f\t',result(k,:));
end

%uses the finite divided difference to interpolate


xt = 1;
yInt = b(1,1);

fprintf('\n\n\ty =');
fprintf('%10.5f\t',b(1,1));

for j = 1:n-1
xt = xt*(xx-x(j));
yInt = yInt + b(1,j+1)*xt;
fprintf('+');
fprintf('%10.5f*(x-%f)',b(1,j+1),x(j));
if j == 1
fprintf('\n\t\t\t\t\t');
end
if j>1
k = 1;
p=j;
while k<p
fprintf('*(x-%f)',x(p-1));
p = p-1;
end
if j < n-1
fprintf('\n\t\t\t\t\t');
end
end
end

b) Command
>> x = [0 1 2.5 3 4.5 5 6]

x=0 1.0000 2.5000 3.0000 4.5000 5.0000 6.0000

>> y = [2 5.4375 7.3516 7.5625 8.4453 9.1875 12]

y = 2.0000 5.4375 7.3516 7.5625 8.4453 9.1875 12.0000

>> NewtonInterpolation(x,y,3.5)

X fx 1st 2nd 3rd 4th 5th 6th


0.00000 2.00000 3.43750 -0.86457 0.14581 0.00001 -0.00000 0.00000
1.00000 5.43750 1.27607 -0.42713 0.14586 -0.00001 0.00000
2.50000 7.35160 0.42180 0.08337 0.14583 -0.00000
3.00000 7.56250 0.58853 0.44793 0.14582
4.50000 8.44530 1.48440 0.88540
5.00000 9.18750 2.81250
6.00000 12.00000
y = 2.00000 + 3.43750*(x-0.000000)+ -0.86457*(x-1.000000)*(x-0.000000)
+ 0.14581*(x-2.500000)*(x-1.000000)*(x-0.000000)+ 0.00001*(x-3.000000)*(x-
2.500000)*(x-1.000000)*(x-0.000000)+ -0.00000*(x-4.500000)*(x-3.000000)*(x-
2.500000)*(x-1.000000)*(x-0.000000)+ 0.00000*(x-5.000000)*(x-4.500000)*(x-
3.000000)*(x-2.500000)*(x-1.000000)*(x-0.000000)

ans = 7.7422

Using the Newton Interpolation matlab code and command, the result presents
7.7422 as the value of y at x = 3.5.

3. Use Newton’s interpolating polynomial to determine 𝑦 at 𝑥 = 8 to the best possible accuracy.


Compute the finite divided differences and order your points to attain optimal accuracy and
convergence. That is, the points should be centered around and as close as possible to the unknown.

𝒙 0 1 2 5.5 11 13 16 18
𝑦 0.5 3.134 5.3 9.9 10.2 9.35 7.2 6.2

a) Matlab

function yInt = NewtonInterpolation(x, y, xx)


% NewtonInterpolation(x, y, xx): Uses an (n-2)order Newton interpolating
% polynomial based on n data points (x,y)
% to determine a value of a dependent variable (yInt)
% at a given value of the independent variable, xx
% input:
% x - independent variable (row matrix)
% y - dependent variable (row matrix)
% xx - value of independent variable at which interpolation is calculated
% output:
% yInt - interpolated value of dependent variable

%compute
n = length(x);
if length(y) ~= n
error('x and y must have the same length');
end
b = zeros(n,n);
%assign dependent variables to the first column of b
b(:,1)=y(:); %ensures that y is a column vector
for j = 2:n
for i = 1:n-j+1
b(i,j) = (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end

fprintf('\n\t\t x\t\t\tfx');
for k = 1:n-1

if k==1
fprintf('%10.0fst',k);
end
if k==2
fprintf('%10.0fnd',k);
end
if k==3
fprintf('%10.0frd',k);
end
if k>3
fprintf('%10.0fth',k);
end

end
l = b';
v = [x;l];
result = v';
for k = 1:n
fprintf('\n');
fprintf('%10.5f\t',result(k,:));
end

%uses the finite divided difference to interpolate


xt = 1;
yInt = b(1,1);

fprintf('\n\n\ty =');
fprintf('%10.5f\t',b(1,1));

for j = 1:n-1
xt = xt*(xx-x(j));
yInt = yInt + b(1,j+1)*xt;
fprintf('+');
fprintf('%10.5f*(x-%f)',b(1,j+1),x(j));
if j == 1
fprintf('\n\t\t\t\t\t');
end
if j>1
k = 1;
p=j;
while k<p
fprintf('*(x-%f)',x(p-1));
p = p-1;
end
if j < n-1
fprintf('\n\t\t\t\t\t');
end
end
end

b) Command
>> x=[0 1 2 5.5 11 13 16 18]

x=0 1.0000 2.0000 5.5000 11.0000 13.0000 16.0000 18.0000

>> y = [0.5 3.134 5.3 9.9 10.2 9.35 7.2 6.2]

y = 0.5000 3.1340 5.3000 9.9000 10.2000 9.3500 7.2000 6.2000


>> NewtonInterpolation(x,y,8)
x fx 1st 2nd 3rd 4th 5th 6th 7th
0.00000 0.50000 2.63400 -0.23400 0.00813 -0.00029 0.00004 - 0.00000
0.00000
1.00000 3.13400 2.16600 -0.18927 0.00493 0.00017 -0.00004 0.00001
2.00000 5.30000 1.31429 -0.13997 0.00691 -0.00046 0.00000
5.50000 9.90000 0.05455 -0.06394 0.00053 0.00112
11.00000 10.20000 -0.42500 -0.05833 0.01452
13.00000 9.35000 -0.71667 0.04333
16.00000 7.20000 -0.50000
18.00000 6.20000

y = 0.50000 + 2.63400*(x-0.000000)+ -0.23400*(x-1.000000)*(x-0.000000)


+ 0.00813*(x-2.000000)*(x-1.000000)*(x-0.000000)+ -0.00029*(x-5.500000)*(x-
2.000000)*(x-1.000000)*(x-0.000000)+ 0.00004*(x-11.000000)*(x-5.500000)*(x-
2.000000)*(x-1.000000)*(x-0.000000)+ -0.00000*(x-13.000000)*(x-11.000000)*(x-
5.500000)*(x-2.000000)*(x-1.000000)*(x-0.000000)+ 0.00000*(x-16.000000)*(x-
13.000000)*(x-11.000000)*(x-5.500000)*(x-2.000000)*(x-1.000000)*(x-0.000000)

ans = 10.7345

Using the Newton Interpolation matlab code and command, the result presents
10.7345 as the value of y at x = 8.

4. Repeat #3 using Lagrange polynomial.

function fp=lagrange_interpolation(x,y,p)

n=size(x,2);
L=ones(n,size(p,2));

for i=1:n
for j=1:n
if (i~=j)
L(i,:)=L(i,:).*(p-x(j))/(x(i)-x(j));
end
end
end
d=zeros(n,1);
for i=1:n
d(i,:)=d(i,:)+y(i)*L(i,:);

end
disp(d)
fp=sum(d);

end
>> x = [0 1 2 5.5 11 13 16 18]
>> y = [0.5 3.134 5.3 9.9 10.2 9.35 7.2 6.2]

>> lagrange_interpolation(x,y,8)
-0.1391
3.2774
-5.7359
8.5112
8.9917
-4.8821
0.8571
-0.1459

ans = 10.7345

Using the Lagrange Interpolation matlab code and command, the result presents
10.7345 as the value of y at x = 8.

5. Bessel functions often arise in advanced engineering analyses such as the study of electric fields.
Here are some selected values for the zero-0rder Bessel function of the first kind
𝒙 1.8 2.0 2.2 2.4 2.6
𝐽1 (𝑥) 0.5815 0.5767 0.5560 0.5202 0.4708

Estimate 𝐽1 (2.1) using third- and fourth-order interpolating polynomials. Determine the percent relative
error for each case based on the true value, which can be determined with MATLAB’s built-in function
besselj.

Answer:
a) Matlab
function ni = newtonInterpolation(x,y,z)

n = length(x);
a(1) = y(1);

for kk = 1:n - 1
dd(kk,1) = (y(kk+1)- y(kk))/(x(kk+1) - x(kk));
end

for j = 2:n - 1
for kk = 1:n - j
dd(kk, j) = (dd(kk+1, j-1) -dd(kk, j-1))/(x(kk+j) - x(kk));
end
end

dd

for j = 2:n
a(j) = dd(1, j-1);
end

Df(1) = 1;
c(1) = a(1);

for j = 2:n
Df(1)=(z - x(j-1)) .*Df(j-1);
c(j) = a(j) .* Df(j);
end

ni = sum(c);

b) Commands
Third Order:

>> x = [1.8 2.0 2.2 2.4]

x = 1.8000 2.0000 2.2000 2.4000

>> J_1x = [0.5815 0.5767 0.5560 0.5202]

J_1x = 0.5815 0.5767 0.5560 0.5202

>> p3 = polyfit(x,J_1x,3)

p3 = 0.0167 -0.2988 0.9306 -0.2228

>> d3 = polyval(p3,2.1)

d3 = 0.5683

>> y = 1.8:0.1:2.4

y = 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000

>> j = besselj(1,y)

j = 0.5815 0.5812 0.5767 0.5683 0.5560 0.5399 0.5202

>> E3 = abs((d3-j(4))/j(4))*100

E3 = 8.1573e-04

>> fprintf('For a 3rd order interpolation J1(2.1) = %1.4f\n', d3);


For a 3rd order interpolation J1(2.1) = 0.5683

>> fprintf('The percent relative error is %2.4f\n\n', E3);

The percent relative error is 0.0008

Fourth Order:

>> x = [1.8 2.0 2.2 2.4 2.6]

x = 1.8000 2.0000 2.2000 2.4000 2.6000

>> J_1x = [0.5815 0.5767 0.5560 0.5202 0.4708]

J_1x = 0.5815 0.5767 0.5560 0.5202 0.4708

>> p4 = polyfit(x,J_1x,4)

p4 = 0.0182 -0.1365 0.1818 0.2630 0.1237

>> d4 = polyval(p4,2.1)

d4 = 0.5683

>> y = 1.8:0.1:2.6

y = 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000

>> j = besselj(1,y)

j = 0.5815 0.5812 0.5767 0.5683 0.5560 0.5399 0.5202 0.4971 0.4708

>> E4 = abs((d4-j(4))/j(4))*100

E4 = 0.0014

>> fprintf('For a 4th order interpolation J1(2.1) = %1.4f\n', d4);

For a 4th order interpolation J1(2.1) = 0.5683

>> fprintf('The percent relative error is %2.4f\n', E4);

The percent relative error is 0.0014


The relative errors for each case in third and fourth order based on the true value are
0.0008 % and 0.0014 %, respectively.

6. Repeat #5 using Lagrange polynomial.


>> x = [1.8 2.0 2.2 2.4 2.6]

x = 1.8000 2.0000 2.2000 2.4000 2.6000

>> J_1x = [0.5815 0.5767 0.5560 0.5202 0.4708]

J_1x = 0.5815 0.5767 0.5560 0.5202 0.4708

>> p3 = polyfit(x,J_1x,3) *Returns coefficients for 3rd order*

p3 = 0.0240 -0.3444 1.0252 -0.2878

>> p4 = polyfit(x,J_1x,4) *Returns coefficients for 4th order*

p4 = 0.0182 -0.1365 0.1818 0.2630 0.1237

>> d3 = polyval(p3,2.1) *Returns value for 2.1*

d3 = 0.5683

>> d4 = polyval(p4,2.1) *Returns value for 2.1*

d4 = 0.5683

>> y = 1.8:0.1:2.6

y = 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000

>> j = besselj(1,y)

j = 0.5815 0.5812 0.5767 0.5683 0.5560 0.5399 0.5202 0.4971 0.4708

>> E3 = abs((d3-j(4))/j(4))*100

E3 = 0.0031 %

>> E4 = abs((d4-j(4))/j(4))*100
E4 = 0.0021 %

>> fprintf('For a 3rd order interpolation J1(2.1) = %1.4f\n', d3);

For a 3rd order interpolation J1(2.1) = 0.5683

>> fprintf('The percent relative error is %2.4f\n\n', E3);

The percent relative error is 0.0031.

>> fprintf('For a 4th order interpolation J1(2.1) = %1.4f\n', d4);

For a 4th order interpolation J1(2.1) = 0.5683

>> fprintf('The percent relative error is %2.4f\n', E4);


The percent relative error is 0.0021.

Therefore, the relative errors for third and fourth order are 0.0031 % and 0.0021 %,
respectively.

You might also like