Professional Documents
Culture Documents
Lagrange Polynomial:
function [px]=LagrangePolynomial(xData, yData)
px=0;
for i=1:length(xData)
p=1;
for j=1:length(xData)
if(j~=i)
p=conv(p, [1 -xData(j)])/(xData(i)-xData(j));
end
end
px=px+p*yData(i);
end
end
Newton Polynomial:
function [px]=NewtonPolynomial(xData, yData)
D=zeros(length(xData),length(xData));
D(:,1)=yData;
for j=2:length(xData)
for k=j:length(xData)
D(k,j)=(D(k,j-1)-D(k-1, j-1))/(xData(k)-xData(k-j+1));
end
end
px=[];
for i=1:length(xData)
p=D(i,i);
for j=1:i-1
p=conv(p, [1 -xData(j)]);
end
px=[0 px];
px=px+p;
end
end
Projectile:
A projectile, thrown at an angle with the ground travels through the following points:
Code:
clear all;
clc;
close all;
px=NewtonPolynomial(xData, yData);
% Modification of px
for i=1:length(px)
if(abs(px(i))<0.005)
px(i)=0;
end
end
x=0:0.5:6;
y=polyval(px,x);
plot(x,y)
xlabel('Time(s)')
ylabel('Height(m)')
legend('path')
hold on;
maxHeight=max(polyval(px,0:0.1:6));
px1=px;
px1(length(px1))=-maxHeight;
xPoint=roots(px1);
plot(xPoint, [maxHeight maxHeight], '*')
title('Plot of the projectile for given data points.')
legend('Path of the projectile','Data points', 'Initial velocity', '
Maximum height')
Plot of the projectile for given data points.
40
Path of the projectile
Data points
Initial velocity
35 X: 3
Maximum height
Y: 36
30
25
Height(m)
20
15
10
0
0 1 2 3 4 5 6
Time(s)