Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

% 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

image text in transcribed

% 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions