Mix Achar Numerical Matlab
Mix Achar Numerical Matlab
```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
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
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.
```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`.
```matlab
function NewtonForwardDividedDifference(x, y)
% x: vector of x-coordinates of data points
% y: vector of y-coordinates of data points
for i = 2:n
term = term * (t - x(i-1));
polynomial = polynomial + fdd(1,i) * term;
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.
```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.
```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
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`.
```matlab
x = [1 2 3 4 5];
y = [1 4 9 16 25];
xi = 2.5;