Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8905655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:26:57+00:00 2026-06-15T02:26:57+00:00

I’m experimenting with ode45 in Matlab. I’ve learned how to pass parameters to the

  • 0

I’m experimenting with ode45 in Matlab. I’ve learned how to pass parameters to the ode function but I still have a question. Let’s suppose that I want to compute the trajectory (speed profile) of a Car and I have a function, e.g. getAcceleration, that gives me the acceleration of the car but also the right gear: [acceleration, gear] = getAcceleration(speed,modelStructure) where modelStructure represents the model of the car.

The ode function would be:

function [dy] = car(t,y,modelStructure)

dy           = zeros(2,1);
dy(1)        = y(2);
[dy(2),gear] = getAcceleration(y(1),modelStructure);

Then I call the Ode45 integrator in this way:

tInit = 0;
tEnd  = 5,
[t,y] = ode45(@car,[tInit tEnd], [speedInitial,accelerationInitial],options,modelStructure);

The problem is: how do I get the vector storing gears? Should I have something like [t,y,gear]=ode45(....) or should gear be within the y vector?


I’ve been working on my code and using the events function I’m now able to get the car ‘gears’ changes (as events).
Now I have a new problem related to the same code.
Imagine that when I evaluate de ‘dy’ vector I’m able to get a further value Z which let me to have a massive speed up calling the acceleration computation (getAcceleration):

function [dy] = car(t,y,modelStructure)

dy           = zeros(2,1);
dy(1)        = y(2);
[dy(2),Z(t)] = getAcceleration(y(1),modelStructure,Z(t-1)); 

and suppose that I’m also able to compute Z at the initial condition. The problem is that I’m not able to compute the Z derivative.

Is there a way to pass Z value throw the stepping without integrating it?

Thanks guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:26:59+00:00Added an answer on June 15, 2026 at 2:26 am

    First off: why are the initial values to the differential equation the initial speed (speedInitial) and the initial acceleration (accelerationInitial)? That means that the differential equation car will be computing the acceleration (y(1)) and the jerk (y(2)), the time-derivative of the acceleration, at each time t. That seems incorrect…I would say the initial values should be the initial position (positionInitial) and the initial speed (speedInitial). But, I don’t know your model, I could be wrong.

    Now, getting the gear in the solution directlty: you can’t, not without hacking ode45. This is also logical; you also cannot get dy at all times directly, can you? That’s just not how ode45 is set up.

    There’s two ways out I see here:

    Global variable

    DISCLAIMER: don’t use this method. It’s only here to show what most people would do as a first attempt.

    You can store gear in a global variable. It’s probably the least amount of coding, but also the least convenient outcome:

    global ts gear ii
    
    ii    = 1;
    tInit = 0;
    tEnd  = 5,
    [t,y] = ode45(...
        @(t,y) car(t,y,modelStructure), ...
        [tInit tEnd], ...
        [speedInitial, accelerationInitial], options);
    
    ...
    
    function [dy] = car(t,y,modelStructure)
    global ts gear ii
    
    dy    = zeros(2,1);
    dy(1) = y(2);
    [dy(2),gear(ii)] = getAcceleration(y(1),modelStructure);
    
    ts(ii) = t;
    ii = ii + 1;
    

    But, due to the nature of ode45, this will get you an array of times ts and associated gear which contains intermediate points and/or points that got rejected by ode45. So, you’ll have to filter for those afterwards:

    ts( ~ismember(ts, t) ) = [];
    

    I’ll say it again: this is NOT the method I’d recommend. Only use global variables when testing or doing some quick-n-dirty stuff, but always very quickly shift towards other solutions. Also, the global variables grow at each (sub-)iteration of ode45, which is an unacceptable performance penalty.

    It’s better to use the next method:

    Post-solve call

    This is also not too hard for your case, and the way I’d recommend you to go. First, modify the differential equation as below, and solve as normal:

    tInit = 0;
    tEnd  = 5,
    [t,y] = ode45(...
        @(t,y) car(t,y,modelStructure), ...
        [tInit tEnd], ...
        [speedInitial, accelerationInitial], options);
    
    ...
    
    function [dy, gear] = car(t,y,modelStructure)    
    
    dy    = [0;0];
    dy(1) = y(2);
    [dy(2),gear] = getAcceleration(y(1),modelStructure);
    

    and then after ode45 completes, do this:

    gear = zeros(size(t));
    for ii = 1:numel(t)
        [~, gear(ii)] = car(t(ii), y(ii,:).', modelStructure); 
    end
    

    That will get you all the gears the car would have at times t.

    The only drawback that I can see here is that you’ll have many more function evaluations of car than ode45 would use by itself. But this is only a real problem if each evaluation of car takes in the order of seconds or longer, which I suspect is not the case in your setup.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I want to construct a data frame in an Rcpp function, but when I
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.