ODE45 projectile angle input

6 visualizaciones (últimos 30 días)
Emmanuelle Harper
Emmanuelle Harper el 12 de Mayo de 2022
Comentada: Jon el 12 de Mayo de 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 comentarios
Sam Chak
Sam Chak el 12 de Mayo de 2022
@Emmanuelle Harper, What exactly do you mean by the following?
drag = 0.0;
Emmanuelle Harper
Emmanuelle Harper el 12 de Mayo de 2022
On one graph I'm plotting with drag, and one without. The one without I have set drag to 0 :)

Iniciar sesión para comentar.

Respuesta aceptada

Jon
Jon el 12 de Mayo de 2022
Editada: Jon el 12 de Mayo de 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 comentarios
Jon
Jon el 12 de Mayo de 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 el 12 de Mayo de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by