Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

mix achar numerical analysis matlab

%%%%%% Lagrange interpolation %%%%%%%

Sure! Here's an example of MATLAB code for Lagrange interpolation:

```matlab
function [P] = lagrange_interpolation(X, Y, x)
% X - vector containing x-coordinates of the known points
% Y - vector containing y-coordinates of the known points
% x - value at which to interpolate

n = length(X); % number of known points


P = 0; % interpolated value at x

for i = 1:n
% Calculate each term of the Lagrange polynomial
term = Y(i);
for j = 1:n
if j ~= i
term = term * (x-X(j)) / (X(i)-X(j));
end
end

% Add the term to the overall interpolated value


P = P + term;
end
```

To use this code, you can pass in your known points as vectors `X` and
`Y`, and the value at which you want to interpolate as `x`. The function
will return the interpolated value `P` at `x`. Make sure the lengths of
`X` and `Y` are the same, and that they correspond to each other in terms
of ordering.

Here's an example usage:

```matlab
X = [1 2 3 4];
Y = [6 3 8 2];
x = 2.5;

P = lagrange_interpolation(X, Y, x);
disp(P);
```

In this example, the known points are `(1, 6)`, `(2, 3)`, `(3, 8)`, and
`(4, 2)`. The code will interpolate the value at `x = 2.5`. The result
will be displayed as the interpolated value `P`.

Note that Lagrange interpolation can suffer from numerical stability


issues when using a large number of points or when the points are
clustered. In those cases, other interpolation methods like polynomial
interpolation using Newton's divided differences might be more
appropriate.

%%%%%%%%% Newton forward divided difference %%%%%%%%


Certainly! Here's an example of MATLAB code for implementing the Newton
forward divided difference formula:

```matlab
function NewtonForwardDividedDifference(x, y)
% x: vector of x-coordinates of data points
% y: vector of y-coordinates of data points

n = length(x); % number of data points


fdd = zeros(n,n); % initialize forward divided difference table

% Fill in the first column of the table with y-values


fdd(:,1) = y';

% Compute the forward divided difference table


for j = 2:n
for i = 1:n-j+1
fdd(i,j) = (fdd(i+1,j-1) - fdd(i,j-1)) / (x(i+j-1) - x(i));
end
end

% Print the forward divided difference table


fprintf('Forward Divided Difference Table:\n');
disp(fdd);

% Interpolation using Newton's forward divided difference formula


syms t; % symbolic variable
polynomial = fdd(1,1);
term = 1;

for i = 2:n
term = term * (t - x(i-1));
polynomial = polynomial + fdd(1,i) * term;
end

% Print the interpolation polynomial


fprintf('\nInterpolation Polynomial:\n');
disp(polynomial);
end
```

You can use this function by providing the vectors `x` and `y` containing
the x-coordinates and y-coordinates of your data points. The function
will compute the forward divided difference table and then perform
polynomial interpolation using Newton's formula. Finally, it will display
the forward divided difference table and the interpolation polynomial.

Please note that this code assumes that you have the Symbolic Math
Toolbox installed to work with the `syms` function.

Certainly! Here's an example of how you can call the


`NewtonForwardDividedDifference` function with some sample data:

```matlab
% Define the data points
x = [1, 2, 3, 4]; % x-coordinates
y = [5, -2, 3, -1]; % y-coordinates
% Call the NewtonForwardDividedDifference function
NewtonForwardDividedDifference(x, y);
```

This will compute the forward divided difference table and the
interpolation polynomial for the given data points (1, 5), (2, -2), (3,
3), and (4, -1). The function will display the forward divided difference
table and the resulting interpolation polynomial.

I hope this helps! Let me know if you have any further questions.

%%%%%%%%%% Newton backward divided difference %%%%%%

Certainly! Below is an example of MATLAB code for implementing the


Newton's backward divided difference interpolation:

```matlab
function result = newton_backward_divided_difference(x, y, xi)
% Perform Newton's backward divided difference interpolation
% x: array of x-values
% y: array of y-values
% xi: x-value at which to interpolate

n = length(x); % number of data points


F = zeros(n); % divided difference table

% Compute divided differences


F(:,1) = y;
for i = 2:n
for j = i:n
F(j,i) = (F(j,i-1) - F(j-1,i-1)) / (x(j) - x(j-i+1));
end
end

% Evaluate interpolating polynomial


result = F(n,n);
for i = n-1:-1:1
result = result * (xi - x(i)) + F(i,i);
end
end
```

To use this code, you can provide an array of x-values `x`, an array of
corresponding y-values `y`, and the x-value `xi` at which you want to
interpolate. The function will return the interpolated y-value at `xi`.

Here's an example usage:

```matlab
x = [1 2 3 4 5];
y = [1 4 9 16 25];
xi = 2.5;

interpolated_value = newton_backward_divided_difference(x, y, xi);


disp(interpolated_value);
```
The result will be `6.25`, which is the interpolated value of `y` at `xi
= 2.5`.

You might also like