ODE45 projectile angle input

4 views (last 30 days)
Emmanuelle Harper
Emmanuelle Harper on 12 May 2022
Commented: Jon on 12 May 2022
Hi! Im trying (like many others have done previously) to plot the course of a projectile with ODE45.
After reading quite a few different examples, I still cant figure out how to intergrate an angle at which my projectile will be launched at, in my code.
Please would someone give an example of how to do it with my code, and an explination because Im struggling a lot even after having read lots!
Much appreciated!!
clf,clear,clc;
initial_conditions = [0, 40.96, 0, 28.68]'; %order of variables. (x start displacement = 0, x komponent initial speed = 40.96, y start displacement=0, y component initial speed = 28.68)
tspan = [0,7];
[t,Y] = ode45(@fun1, tspan, initial_conditions);
subplot(2,1,1)
plot(Y(:,1), Y(:,3)) %plotting x dispacement (1st var) with y displacement (3rd var)
ylim([0 50])
[t,Y] = ode45(@fun2, tspan, initial_conditions);
subplot(2,1,2)
plot(Y(:,1), Y(:,3)) %plotting x dispacement (1st var) with y displacement (3rd var)
ylim([0 50])
hold on
f = @(p) p;
x = linspace(0,250);
plot(x, f(x))
grid on
title('txt')
xlabel('xlabel')
legend('path','jhg')
function dX = fun1(t, X) %X is initial conditions, then stepped
g = 9.81;
dX = zeros(4,1);
dX(1) = X(2); %dx/dt = vx
dX(2) = 0; %dx^2/dt^2 = 0
dX(3) = X(4); %dy/dt = vy
dX(4) = -g; %dy^2/dt^2 = -g
end
function dX = fun2(t, X)
g = 9.81;
drag = 0.0;
dX = zeros(4,1);
dX(1) = X(2); %dx/dt = vx
dX(2) = -drag*X(2); %dx^2/dt^2 = 0
dX(3) = X(4); %dy/dt = vy
dX(4) = -g-drag*X(4); %dy^2/dt^2 = -g
end
  2 Comments
Sam Chak
Sam Chak on 12 May 2022
@Emmanuelle Harper, What exactly do you mean by the following?
drag = 0.0;
Emmanuelle Harper
Emmanuelle Harper on 12 May 2022
On one graph I'm plotting with drag, and one without. The one without I have set drag to 0 :)

Sign in to comment.

Accepted Answer

Jon
Jon on 12 May 2022
Edited: Jon on 12 May 2022
Using ode45 you integrate (solve the differential equations) for the x and y components of the velocity and position.
The initial launch angle is determined by the initial x and y velocity components.
If you only know the initial velocity, v0, and you want to specify the launch angle, then set the initial x and y components of the velocity to
vx0 = v0*cos(theta); % launch angle theta in radians
vy0 = v0*sin(theta); % launch angle theta in radians
You would then use those values in you initial condition vector, so maybe something like this:
theta = pi/4; % initial launch angle [rad], put in your value here I just used pi/4 as example
v0 = 20; % initial launch velocity [m/s], put in your value here I just used 20 as example
x0 = 0; % initial x position
y0 = 0; % initial y position
% calculate initial velocity components
vx0 = v0*cos(theta); % launch angle theta in radians
vy0 = v0*sin(theta); % launch angle theta in radians
% build the initial condition vector
initial_conditions = [x0,vx0,y0,vy0]
% continue with rest of calculations
  3 Comments
Jon
Jon on 12 May 2022
Yes exactly. In fact you can calculate your launch angle using
launchAngle = atan(28.68/40.96)*180/pi % convert to degrees
Looks like it is around 35 deg
Jon
Jon on 12 May 2022
If this answered your question please accept the answer. This will take it off of the list of unanswered questions and also will let others with a similar question know that an answer is available

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!