Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i need MATLAB code for this Problem. Use the following pseudocode to plot the La

ID: 2260967 • Letter: I

Question

i need MATLAB code for this

Problem. Use the following pseudocode to plot the Lagrange polynomial Pi for f(x) = 3e +2 sin(z)on 0, 3 through 4 points (i, f(i))0,1,2,3. Also plot the points and f on the sam figure. Explain steps by commenting on them. Algorithm Lagrange-Polynomial-Interpolation Input: /(z) = 3e-r + 2 sin(!), X = [0, 1, 2, 3], Y [/(0)/(1), f(2), f(3)] Output: Lagrange polynomial Pl, Graphs off and Ps through (x(i),y(i), i = 0, .. . 3 set n length of X: P=0: L is a zero vector of length n; for i = 1 to n (i) = 1; for j to n if(ji) end if end for end for print P plot P in blue plot f in red plot (X(i), Y(i), i=0 , 3 in red Useful commands: (See Matlab 0) plot(X, Y'Or) .To plot points in red and f on [0, 3 in red: hold on fplot (f, [0,3],'r') % define f before this .To define P as a "symbolic" function of the variable, use syms r Then before the for loop write L as follows: L = sym(zeros(size(X)))

Explanation / Answer

X = 0:3;
fx = 3*exp(-X)+2*sin(X);
Y = fx;
n = length(X);
P = 0;
syms x;
L = sym(zeros(size(X)));
for i = 1:n
L(i) = 1;
for j = 1:n
if j ~= i
L(i) = L(i)*(x - X(j)) / (X(i) - X(j));
end
end
P = P+Y(i)*L(i); % Lagrange polynomial
end
P % print P
plot(P,'*b'); % plot lagrange polynomial
hold on; grid on;
f = @(x)(3*exp(-x)+2*sin(x));
fplot(f,[0 3],'r');
hold on;
plot(X,Y,'Or');