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

It is required to use a program to finish this question. Only programs written i

ID: 3935867 • Letter: I

Question

It is required to use a program to finish this question. Only programs written in C/C++/Matlab/Octave is acceptable. Hand in the code via CCLE (please put all of them in one file, perhaps ZIP if there are more than one). Please also print the code out and hand in the hard copy of the code.) Let a number of points (x_i, f(x_i)) be given, 0 lessthanorequalto i lessthanorequalto n. Let h(x) be its Lagrange interpolation polynomial interpolating the points (x_i, f(x_i)), 0 lessthanorequalto i lessthanorequalto n. Write a program which allow inputs {(x_i, f(x_i))} and a value a, and calculate the value of h(a).

Explanation / Answer

inp2 = input('Enter number of points:','s');

n = str2double(inp2);
ax = zeros(n);
ay = zeros(n);
for i=1:n
fprintf('Point: %d',i);
x = input('Enter value of x:','s');
y = input('Enter value of f(x):','s');
ax(i) = str2double(x);
ay(i) = str2double(y);
end
fy = 0;
inp3 = input('Enter the value of a to compute h(a): ','s');
a = str2double(inp3);

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);
end

fprintf('Value of h(a) = %.4f ', fy);

%------------------------Sample run------------------------

Enter number of points:3
Point: 1Enter value of x:1
Enter value of f(x):1
Point: 2Enter value of x:2
Enter value of f(x):8
Point: 3Enter value of x:3
Enter value of f(x):27
Enter the value of a to compute h(a): 4
Value of h(a) = 58.0000

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote