Need help with the cope (Lagrange interpolation)
16 views (last 30 days)
Show older comments
clc;
%a
x =[1 2 3 5 7 8];
fx =[3 6 19 99 291 444];
f1 = interp1(x,fx,2.8,'nearest');
f2 = interp1(x,fx,4.4,'nearest');
f3 = interp1(x,fx,7.1,'nearest');
fprintf("\nf(x) at x = 2.8: %d",f1)
fprintf("\nf(x) at x = 4.4: %d",f2)
fprintf("\nf(x) at x = 7.1: %d",f3)
%b
x =[1 2 3 5 7 8];
fx =[ 3 6 19 99 291 444];
sum = 0;
a = 2;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (a - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
sum = 0;
b = 3;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (b - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
c = 4;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (c - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
%c
x = [ 2.8, 4.4 ,7.1];
fx = [19, 99, 291];
sum = 0;
a = 2;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (a - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
sum = 0;
b = 3;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (b - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
c = 4;
for i = 1:length(x)
u = 1;
l = 1;
for j = 1:length(x)
if j ~= i
u = u * (c - x(j));
l = l * (x(i) - x(j));
end
end
sum= sum + u / l * fx(i);
end
disp(sum);
I need to double check that AM I doing it right?
You are given the following errorless dataset:x= [1 2 3 5 7 8]
f(x) = [3 6 19 99 291 444 ]
a.Use nearest neighbour interpolation to find f(x) for the following x values: 2.8, 4.4, and 7.1
b.Implement a MATLABfunctionor scriptto perform interpolation based on the closest 2, 3, and 4 points using Lagrangian interpolating polynomials
c.Run your function/scripton the x values given in a)for the closest 2-, 3-, and 4-point Lagrangian interpolating polynomia
0 Comments
Answers (0)
See Also
Categories
Find more on Interpolation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!