My timeSpan is considered a nonscalar operator when using if statement. Help.
2 views (last 30 days)
Show older comments
Marcos Dominguez
on 3 May 2021
Commented: Marcos Dominguez
on 3 May 2021
Hello,
I am having trouble with my code. I tried experimenting with other statements other than if but I can't seem how to make my code work. I want the values to change after 15 seconds in the scenario. I am still a novice to Matlab.
%Differential Equation Parachute
%Terminal Velocity Differential Equation
m = 80;
S = 0.65;
Cd = 0.45;
rho = 1.225;
v0 = 0;
g = 9.8;
timeSpan = [0 50];
if timeSpan > 15
Cd = 1.75;
S = 11.6127;
end
[Time,Vout] = ode45(@(t,v)(g-(rho/(2*m))*v^2*S*Cd),timeSpan,v0);
f2 = figure;
plot(Time,Vout);
title('Free Fall Terminal Velocity')
xlabel('Time (s)')
ylabel('Velocity v(t)')
0 Comments
Accepted Answer
Cris LaPierre
on 3 May 2021
Edited: Cris LaPierre
on 3 May 2021
Your if statement is not doing what you think it is doing. To learn more about how an if statement works, I suggest going through Ch 13 of MATLAB Onramp.
To do what you want, you are going to have to create a function for your odefun and place the if statement inside the function. Ode45 solves the equation one time step at a time. You can use this example as a starting point.
Your final code might look something like this.
v0 = 0;
timeSpan = [0 50];
[Time,Vout] = ode45(@odefun,timeSpan,v0);
plot(Time,Vout);
title('Free Fall Terminal Velocity')
xlabel('Time (s)')
ylabel('Velocity v(t)')
function [dvdt] = odefun(t,v)
m = 80;
rho = 1.225;
g = 9.8;
if t > 15
Cd = 1.75;
S = 11.6127;
else
S = 0.65;
Cd = 0.45;
end
dvdt = (g-(rho/(2*m))*v^2*S*Cd);
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!