Filter löschen
Filter löschen

ode45 command matlab help

4 Ansichten (letzte 30 Tage)
Abdullaziz Errayes
Abdullaziz Errayes am 9 Mai 2018
Bearbeitet: Jan am 12 Mai 2018
1000*dV/dt+cd*1.225*V^2*A+200-T=0 this is the ode i have to currently solve for assignment due on the 11th of this month where T is defined by the for loop added below. Heres the question "Write a function into which you pass the end time and time step size that returns the time and speed array for a car initially at rest, speed equals 0.0 m/s, is accelerated along a runway " cd=0.5 and A=2
for t=0:time_step:time_end
if t<1
elseif 1<=t<=50
  8 Kommentare
Abdullaziz Errayes
Abdullaziz Errayes am 9 Mai 2018
time_end=input('Input end time');
time_step=input('Input time step size');
function main
time_step =input('Input end time');
time_end= input('Input time step size');
[t,v]=ode45(@ode2 ,tspan,0);
function dvdt=ode2(t,v)
T=10000*(t*(t<1) + (t>=1)*(t<=50));
cd = 0.5;
here is my current code but when i run it theres no error but it doesnt do anything
Jan am 11 Mai 2018
Bearbeitet: Jan am 12 Mai 2018
@Abdullaziz Errayes: If you have saved this code to a script file, nothing happens, because the function main() is not called at all. Remove the first two lines and the code will run.
"a function into which you pass the end time and time step size" sounds like your function should take 2 input arguments, and not two input commands.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan am 9 Mai 2018
Bearbeitet: Jan am 12 Mai 2018
Remember that Matlab's ODE integrators (as many other tools also) can handle smooth functions only: The automatic step size control gets deeply confused if the function to be integrated is not differentiable. See If you are lucky, the integrator stops with an error, but without luck, you get a result, which is dominated by the accumulated rounding error, because the step size is reduced until the discontinuity is hidden by the truncation error.
The numerically correct method is to stop the integration at the discontinuity and restart it. If the time is known in advance, e.g. as Tstop:
[t1, y1] = ode45(fcn1, [0, Tstop], y0);
[t2, y2] = ode45(fcn2, [Tstop, Tend], y1(end, :));
t = [t1; t2];
y = [y1; y2];
If the discontinuity depends of a certain position, use an event function to stop the integration and a while loop until t(end) is the wanted final time.
[EDITED] The question of your homework is not precise. Maybe a simple Euler method is sufficient already instead of a smart ODE integrator or symbolic integrations:
function [t, v] = YourFcn(tEnd, dt)
t = 0:dt:tEnd;
v0 = 0;
cd = 0.5;
A = 2;
v = zeros(size(t));
v(1) = v0; % Actually not needed because it is 0 already
for it = 2:numel(t)
% if t < 1, T = 10000*t; else, T = 10000; end -- easier:
T = 10000 * min(t(it), 1);
accel = (T - 200 - (cd * 1.225 * v(it-1) ^ 2 * A)) / 1000;
v(it) = v(it - 1) + accel * dt;
  2 Kommentare
John BG
John BG am 11 Mai 2018
you went for ode45
then I posted my answer, no need for ode45.
and now you add that 'maybe' (!?) a simple euler method suffices ..
either you need ode45, or you don't, what's going to be?
Jan am 12 Mai 2018
Bearbeitet: Jan am 12 Mai 2018
@John BG:
"you went for ode45" - wrong. The OP Abdullaziz Errayes has mentioned ODE45 and I've explained the common pitfall of integrating a discontinuous function with a step size controlled method.
"either you need ode45, or you don't" - wrong. The OP did not mentioned, if the homework question specified the kind of integration. Then "maybe" a simple Euler method is enough. By the way, the Euler method and the ODE45 approach posted by the OP compute equivalent velocities (in spite of the numerical instability):
The velocity calculated by your suggested symbolic integration looks different. A short test: The final velocity is reached, when the acceleration is 0:
T = 10000;
cd = 0.5;
A = 2;
accel = (T - 200 - (cd * 1.225 * v_end ^ 2 * A)) / 1000 = 0 ==>
v_end = sqrt((T - 200) / (cd * 1.225 * A)) = 89.4427
The final v with the Euler method is 89.4393, with ODE45 it is 89.4113, your value is about 810 according to the diagram. I suggest to cross-check your suggestion instead of posting a pointless comment.

Melden Sie sich an, um zu kommentieren.

John BG
John BG am 11 Mai 2018
Bearbeitet: John BG am 11 Mai 2018
Hi Abdullaziz
the ODE is simple and it can be split into 2 parts:
1000*dV/dt+cd*1.225*V^2*A+200 - T
a basic 1st degree ODE that doesn't really need any solver to solve it, its just the integration of a polynomial:
The Torque applied
- T
How does the Torque and its integral look like?
clear all;close all;clc
time_end=120 % 2 minute
cd = 0.5;
% time_step =input('Input end time');
% time_end= input('Input time step size');
% tspan=[0:time_step:time_end];
T=10000*(t.*(t<1) + (t>=1).*(t<=50));
yyaxis left
plot(t,T);grid on
axis([-10 130 -100 10500])
yyaxis right
title(' Torque and int(Torque)')
axis([-5 125 -100 5e6])
An example of real Torque is available in page 26 of Balaji Kamalammannan's Master Degree thesis
'Modelling and Simulation of Vehicle Kinematics and Dynamics (WITH MATLAB)'
Just split the needed integration
int(f1 + f2)= inf(f1) + int(f2)
syms dvdt v
v_without_T =
(49*v^3)/240000 + v/5
v=v_without_T + T1
T1 being cumsum(T)
Since the integral of the Torque is already null at v=0, let's find the integration constant to meet the initial condition
v=(49*v^2)/80000 + 1/5+T1 + kv0
kv1=-.5 % to set v(0)=0
fv3=@(u) (49*u^2)/80000 + 1/5+T1k+ kv1 - u
for k=1:1:numel(t)
fv3=@(u) (49*u^2)/80000 + 1/5+T1k+ kv1 - u;
v=[v real(d2(1))];
figure;plot(v);grid on
axis([0 100 -100 900])
As expected the dominant term of the velocity, with the equation you use, is the Torque.
Torque is roughly proportional to fuel (or battery charge) consumption.
But your equation is for an ideal model, with the vehicle on flat terrain. In reality, only with certain minimum downhill angle, and not even with brand new bearings and everything working well, air drag, friction continuously reduces gained velocity thus further torque is required to keep speed constant.
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG


Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by