Question: % Define the function to interpolate f = @(x) exp(-x.^2); % Define the data points datx = -3:1:3; daty = f(datx); % Interpolate using Newton's

 % Define the function to interpolate f = @(x) exp(-x.^2); %

% Define the function to interpolate

f = @(x) exp(-x.^2);

% Define the data points

datx = -3:1:3;

daty = f(datx);

% Interpolate using Newton's divided differences

x = -3:0.01:3;

y = Newtons_divided_differences(x,datx,daty);

P5 = y.'; % Save the interpolant

% Plot the results

plot(x,f(x),'b',x,P5,'r')

legend('f(x)','P5(x)')

xlabel('x')

ylabel('y')

title('Interpolation of f(x)=e^{-x^2} using Newton''s divided differences')

% Calculate the error

error = abs(f(x) - P5);

max_error = max(error);

fprintf('The maximum error is %f ',max_error);

function y = Newtons_divided_differences(x,datx,daty)

% Calculates the polynomial interpolant using Newton's divided differences

% Inputs: x - the set of numbers at which to interpolate

% datx - the x-values of the data points

% daty - the y-values of the data points

% Output: y - the polynomial interpolant evaluated at x

n = length(datx);

F = zeros(n,n);

F(:,1) = daty';

for j = 2:n

for i = j:n

F(i,j) = (F(i,j-1) - F(i-1,j-1))/(datx(i) - datx(i-j+1));

end

end

y = F(1,1)*ones(size(x));

for j = 2:n

y = y.*(x - datx(j-1)) + F(j,j);

end

end heres my code, the error i get is

Variable P5 has an incorrect value.

but it also wants me create a column vector of size [601 1] which i did by interpolating but i get the Variable 5 error

tinitinn apials galutian S: 1 of 2 tasks pasead

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!