x=linspace(-1,1); N=length(x); f=@(x) exp(x).*sin(10*x); xn(1)=input(\'First Nod
ID: 3599570 • Letter: X
Question
x=linspace(-1,1); N=length(x); f=@(x) exp(x).*sin(10*x);
xn(1)=input('First Node ');
yn(1)=f(xn(1));
a(1,1)=yn(1);
p=a(1,1)*ones(N,1);
err = abs(f(x)-p);
hold on
plot(xn,yn,'o',x,p,x,f(x))
legend('yn','p','f(x)')
title('First Node')
%plot(x,err)
hold off
for i=1:
yn(i)=f(xn(i));
a(i,1)=yn(i);
for j=1:length(xn)
if j~=1 a(i,j)=yn(j);
end end end
Write a program that accommodates additional data points as follows: 1. Start by defining x between -1 and 1 and set N- lenght(x); 2. Define the function you are going to test the program: say, f = @(x) exp(x)..sin(10.x). 3. Using the command "input" read the zero node: xn(1) = input('first node): yn(1) = f(xn(1). 4. Define your first newton interpolating polynomial: a(1,1)-yn(1); p = a(1,1)"ones(N,1); 5. Plot f, p and yn with an 'o' for the data points (xn,yn) 6. Plot the error abs(f-p). Use subplot(2,1,") to have these two plots in one graph. 7. Start a for loop to read the subsequent data points, one at a time. After each data point is read you need to define the next term in the first column: yn(i) f(xn()ai,1)-yn(i) and then fill up the subsequent columns in an upward diagonal fashion till you get a1) as in the following table (after a(4,1) comes a(3,2) then a(a2,2) and last a(1,4): a(2,1) a(2,2) a(2,3) a(3,1) a(3,2) 8. Now p p+ a(1,i) newtix,xn,i) 9. graph again the functions f,p,.yn in one plot and the errors in a second plot (using subplot) 10. The graph should give you an idea where is best to "give" the next data point to improve the interpolation Questions? The challenging part is the building of the table one point at a time without recalculating the entire table. Program newti.m computes the i-th newton polynomial and can be found in the files. Don't attach it to your submission (I already have it).Explanation / Answer
the code is fixed and modified
x=linspace(-1,1); N=length(x); f=@(x) exp(x).*sin(10*x);
xn(1)=input('First Node ');
yn(1)=f(xn(1));
a(1,1)=yn(1);
p=a(1,1)*ones(N,1);
err = abs(f(x)-p);
hold on
plot(xn,yn,'o',x,p,x,f(x))
legend('yn','p','f(x)')
title('First Node')
%plot(x,err)
hold off
for i=1:
yn(i)=f(xn(i));
a(i,1)=yn(i);
for j=1:length(xn)
if j~=1 a(i,j)=yn(j);
end end end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.