In order to do problem 2, 1 has to be completed Write a MATLAB function, called
ID: 3861295 • Letter: I
Question
In order to do problem 2, 1 has to be completed
Write a MATLAB function, called Lagrange_poly that inputs a set of data points (x; y) =(datx, daty), a set x of numbers at which to interpolate, and outputs the polynomial interpolant, y, evaluated at x using Lagrange polynomial interpolation. Your function header should look something like: function y = Lagrange_poly(x, datx, daty) Use the code you developed in Problem 1 to interpolate the functions f(x) = e^-x^2 f(x) = 1/1 + x^2 using the data points datx=-5:1:5. Interpolate at the points x=-5:0.001:5. Plot the results and comment on the error.Explanation / Answer
1.
function y = Lagrange_Poly(x,datx,daty)
tmp = size(datx);
n = tmp(2);
ax = datx;
ay = daty;
fy = 0;
tmp = size(x);
m = tmp(2);
for l = 1:m
a = x(l);
for i=1:n
nr=1;
dr=1;
for j=1:n
if (j ~= i)
nr = nr*(a - ax(j));
dr = dr*(ax(i) - ax(j));
end
end
fy = fy + (nr / dr)*ay(i);
y(l) = fy;
end
fprintf('Value at %f = %.4f ', x(l),fy);
fy = 0;
end
2.
Use the following
>> datx = -5:1:5
>> daty = exp.^-(datx.^2)
>> x = -5:0.001:5
>> Lagrange_Poly(x,datx,daty)
>> daty = 1./(1.+x.^2)
>> Lagrange_Poly(x,datx,daty)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.