Goal
I have been attempting to solve and plot the following coupled differential equation belonging to quadratic drag:
The variables from the equations are defined as:
c = 0.0004
m = 0.0027
starting position of projectile= (0,0.3)
velocity on horizontal component = 2.05
velocity on vertical component = 0.55
g = 9.81
The problem
I cannot seem to solve the equation correctly and have some errors in programming
What I have tried
I have tried using the code from online on MatLab and I have tried on Matematica but none of them is able to program this equation. I have also tried looking at SciPy on Python but it does not seem to work.
Could anybody please set me in the right direction on how to code this properly?
You can use a number of MATLAB built-in ODE solvers. ode45
is usually a good place to start.
You have two positions and two velocities (4 states total), so you need to pass 4 ODEs to the solver ode45
(one derivative for each state).
If x(1)
is the x-position, x(2)
is the y-position, x(3)
is the x-velocity, and x(4)
is the y-velocity, then the derivative of x(1)
is x(3)
, the derivative of x(2)
is x(4)
and the derivatives of x(3)
and x(4)
are the ones given by your two drag equations.
In the end, the MATLAB implementation might look like this:
c = 0.0004;
m = 0.0027;
p0 = [0; 0.3]; % starting positions
v0 = [2.05; 0.55]; % starting velocities
g = -9.81; % gravitational acceleration
tspan = [0 5];
x0 = [p0; v0]; % initial states
[t_sol, x_sol] = ode45(@(t,x) drag_ode_fun(t,x,c,m,g), tspan, x0);
function dxdt = drag_ode_fun(t,x,c,m,g)
dxdt = zeros(4,1);
dxdt(1) = x(3);
dxdt(2) = x(4);
dxdt(3) = -c/m*x(3)*sqrt(x(3)^2+x(4)^2);
dxdt(4) = g-c/m*x(4)*sqrt(x(3)^2+x(4)^2);
end
And you can plot the results as follows:
figure;
subplot(3,1,1); grid on;
plot(x_sol(:,1), x_sol(:,2))
xlabel('x (m)'); ylabel('y (m)')
subplot(3,1,2); grid on;
plot(t_sol, x_sol(:,3))
xlabel('time'); ylabel('v_x (m/s)')
subplot(3,1,3); grid on;
plot(t_sol, x_sol(:,4))
xlabel('time')
ylabel('v_y (m/s)')