0% found this document useful (0 votes)
93 views3 pages

Lagrange Newton Polynomial Code

The document contains code to calculate Lagrange and Newton polynomials for interpolation, as well as code to model the trajectory of a projectile. The projectile code takes x-y position data, uses Newton polynomial interpolation to determine the projectile path, and plots the path along with indicating the initial launch velocity, launch angle, and maximum height achieved.

Uploaded by

Joy Bhadhan Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views3 pages

Lagrange Newton Polynomial Code

The document contains code to calculate Lagrange and Newton polynomials for interpolation, as well as code to model the trajectory of a projectile. The projectile code takes x-y position data, uses Newton polynomial interpolation to determine the projectile path, and plots the path along with indicating the initial launch velocity, launch angle, and maximum height achieved.

Uploaded by

Joy Bhadhan Roy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Codes:

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:

x 0.0 0.5 1.5 2.5 3.75 4 4.5 5 5.5 6


y 0.0 11 27 35 33.75 32 27 20 11 0
Draw the traveling path of the projectile, the initial velocity, the initial angle with the
ground, and the maximum height achieved by the projectile.

Code:
clear all;
clc;
close all;

xData=[0 0.5 1.5 2.5 3.75 4 4.5 5 5.5 6];


yData=[0 11 27 35 33.75 32 27 20 11 0];

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;

plot(xData, yData, 'o')


hold on;
g=9.8;
a=px(length(px)-2);
b=px(length(px)-1);
vx=(-g/(2*a))^(0.5);
vy=24*vx;
v=(vx^2+vy^2)^(0.5);
theta=atan2(vy, vx)*180/pi;
line([0 vx], [0 vy], 'color', 'g')
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)

You might also like