Chapter6 Prob25
Chapter6 Prob25
6.25 Write a MATLAB user-defined function for spline interpolation that uses third-order Lagrange poly-
nomials. Name the function Yint = CubicLagSplines(x,y,xint), where the input arguments x
and y are vectors with the coordinates of the data points, and xint is the x coordinate of the interpolated
point. The output argument Yint is the y value of the interpolated point. The function uses the following
scheme for the interpolation. If xint is in the first interval of the data points, the function uses a second-
order polynomial that passes through the first three data points. If xint is in the last interval of the data
points, the function uses a second-order polynomial that passes through the last three data points. If xint
is in any other interval, lets say interval i between point x i and point x i + 1 , the function uses a third-order
polynomial for the interpolation. The third-order polynomial is written such that it passes through the data
points: x i – 1 , x i , x i + 1 , and x i + 2 .
(a) Use the CubicLagSplines function with the data in Problem 6.13 to calculate the power at wind
speeds of 26 mph and 42 mph.
(b) Use the CubicLagSplines function with the data in Example 6-3 to calculate the stress at strains
of 0.2 and 3.
Solution
The listing of the user-defined function CubicLagSplines is:
function Yint=CubicLagSplines(x,y,Xint)
n=length(x);
for i=2:n
if Xint < x(i)
break
end
end
if i == 2
xa=x(1:3);
ya=y(1:3);
Yint=LagrangeINT(xa,ya,Xint);
elseif i == n
xa=x(n-2:n);
ya=y(n-2:n);
Yint=LagrangeINT(xa,ya,Xint);
else
xa=x(i-2:i+1);
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
2
ya=y(i-2:i+1);
Yint=LagrangeINT(xa,ya,Xint);
end
n = length(x);
for i = 1:n
L(i) = 1;
for j = 1:n
if j ~= i
L(i)= L(i)*(Xint-x(j))/(x(i)-x(j));
end
end
end
Yint = sum(y.*L);
clear; clc
S=[14 22 30 38 46];
P=[320 490 540 500 480];
% Interpolation for 26 mph
Pfor26=CubicLagSplines(S,P,26)
% Interpolation for 42 mph
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.
3
Pfor42=CubicLagSplines(S,P,42)
When executes the following results are displayed in the Command Window:
Pfor26 =
528.1250
Pfor42 =
487.5000
clear; clc
Strain=[0 0.4 0.8 1.2 1.6 2 2.4 2.8 3.2 3.6 4 4.4 4.8 5.2 5.6 6];
Stress=[0 3 4.5 5.8 5.9 5.8 6.2 7.4 9.6 15.6 20.7 26.7 31.1 35.6 39.3 41.5];
% Interpolation for strain 0.2
Stress02=CubicLagSplines(Strain,Stress,0.2)
% Interpolation for strain 3
Stress3=CubicLagSplines(Strain,Stress,3)
When executes the following results are displayed in the Command Window:
Stress02 =
1.6875
Stress3 =
8.200
Excerpts from this work may be reproduced by instructors for distribution on a not-for-profit basis
for testing or instructional purposes only to students enrolled in courses for which the textbook
has been adopted. Any other reproduction or translation of this work beyond that permitted by
Sections 107 or 108 of the 1976 United States Copyright Act without the permission of the
copyright owner is unlawful.