Files
OctaveFunctions/Homework/LagrangePolynomialSymbolic.m

28 lines
1.6 KiB
Matlab

function [ P01, P12, P2 ] = LagrangePolynomialSymbolic(x_list, f)
syms x;
format long;
warning('off','OctSymPy:sym:rationalapprox');
%Evaluate P0,1
%P01 = (((x - x_list(2))/(x_list(1) - x_list(2))) * double(subs(f, x_list(1)))) + (((x - x_list(1))/(x_list(2) - x_list(1))) * double(subs(f, x_list(2))))
P01x = (double(subs(f, x_list(1)))/(x_list(1) - x_list(2))) + (double(subs(f, x_list(2)))/(x_list(2) - x_list(1)));
P01c = ((double(subs(f, x_list(1)))/(x_list(1) - x_list(2))) * -x_list(2)) + ((double(subs(f, x_list(2)))/(x_list(2) - x_list(1))) * -x_list(1));
disp(['P01 = ' num2str(P01x, '%9.9g') 'x + ' num2str(P01c, '%9.9g')])
P01 = (P01x * x) + P01c;
%Evaluate P1,2
%P12 = (((x - x_list(3))/(x_list(2) - x_list(3))) * double(subs(f, x_list(2)))) + (((x - x_list(2))/(x_list(3) - x_list(2))) * double(subs(f, x_list(3))))
P12x = (double(subs(f, x_list(2)))/(x_list(2) - x_list(3))) + (double(subs(f, x_list(3)))/(x_list(3) - x_list(2)));
P12c = ((double(subs(f, x_list(2)))/(x_list(2) - x_list(3))) * -x_list(3)) + ((double(subs(f, x_list(3)))/(x_list(3) - x_list(2))) * -x_list(2));
disp(['P12 = ' num2str(P12x, '%9.9g') 'x + ' num2str(P12c, '%9.9g')])
P12 = (P12x * x) + P12c;
%Evaluate P2
%P2 = (P2x * x) + P2c;
P2x2 = (P01x - P12x) / (x_list(1) - x_list(3));
P2x = ((P01c) + (P01x * -x_list(3)) - ((P12x * -x_list(1)) + (P12c))) / (x_list(1) - x_list(3));
P2c = ((P01c * -x_list(3)) - (P12c * -x_list(1))) / (x_list(1) - x_list(3));
disp(['P02 = ' num2str(P2x2, '%9.9g') 'x^2 + ' num2str(P2x, '%9.9g') 'x + ' num2str(P2c, '%9.9g')])
P2 = (P2x2 * x^2) + (P2x * x) + P2c;
end