Solve in Matlab.
Given the data:
x = [0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2 10.5 11.3 11.6 12 12.6 13 13.3]
y = [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95 1.4 0.9 0.7 0.6 0.5 0.4 0.25]
Draw the data and the curves of the Lagrangian polynomial, piecwise linear apporoximation, and the cubic spline (use Matlab command spline for the cubic spline)
MATLAB Code:
close all
clear
clc
X = [0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2
10.5 11.3 11.6 12 12.6 13 13.3];
Y = [1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95
1.4 0.9 0.7 0.6 0.5 0.4 0.25];
XX = min(X):0.01:max(X);
[P,R,S] = lagrange(X,Y,XX);
figure, scatter(X,Y), hold on
plot(XX, P, XX, interp1(X,Y,XX,'linear'), XX, spline(X,Y,XX))
xlabel('X'), ylabel('Y')
legend('Data Points', 'Largange Polynomial', 'Linear', 'Cubic
Spline')
function [P,R,S] = lagrange(X,Y,XX)
N = length(X);
pvals = zeros(N);
for i = 1:1:N
pp = poly(X((1:N) ~= i));
pvals(i,:) = pp ./ polyval(pp, X(i));
P = Y*pvals;
YY = polyval(P,XX);
P = YY;
R = roots(((N-1):-1:1) .* P(1:(N-1)));
S = polyval(P,R);
end
end
Plot:
Get Answers For Free
Most questions answered within 1 hours.