MExer01 Final
MExer01 Final
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.
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=
y=
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
%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
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]
>> NewtonInterpolation(x,y,3.5)
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.
𝒙 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
%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
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]
ans = 10.7345
Using the Newton Interpolation matlab code and command, the result presents
10.7345 as the value of y at x = 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 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:
>> p3 = polyfit(x,J_1x,3)
>> d3 = polyval(p3,2.1)
d3 = 0.5683
>> y = 1.8:0.1:2.4
>> j = besselj(1,y)
>> E3 = abs((d3-j(4))/j(4))*100
E3 = 8.1573e-04
Fourth Order:
>> p4 = polyfit(x,J_1x,4)
>> d4 = polyval(p4,2.1)
d4 = 0.5683
>> y = 1.8:0.1:2.6
>> j = besselj(1,y)
>> E4 = abs((d4-j(4))/j(4))*100
E4 = 0.0014
d3 = 0.5683
d4 = 0.5683
>> y = 1.8:0.1:2.6
>> j = besselj(1,y)
>> E3 = abs((d3-j(4))/j(4))*100
E3 = 0.0031 %
>> E4 = abs((d4-j(4))/j(4))*100
E4 = 0.0021 %
Therefore, the relative errors for third and fourth order are 0.0031 % and 0.0021 %,
respectively.