It is required to use a program to finish this question Only

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).

Solution

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

 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site