Search code examples
matlabplotodedifferential-equations

Combining two ode45 function answers into one plot


I am trying to use Matlab to solve a problem which has two separate differential equations that I want to return the values for over time in one giant plot.

For Example the first portion I want to do is:

ainitial = 0;
arange=[0 2];
[a,A] = ode45(@rkfunc, arange, ainitial);

Then I would like to start the next ode45 portion based on the last A value, so I try to set it as binitial.

binitial = A(end);
brange=[2 4];
[b,B] = ode45(@rkfunc, brange, binitial);

Then I would like to combine and plot the answers from [a,A] and [b,B] together into one giant plot, but I'm not sure how to go about doing that.

Any help would be appreciated.

Note: Edited to show binitial = A(end) instead of B(end) which fixes the numbers not overlapping from A(end) and the start of B.


Solution

  • To concatenate two differently sized vectors you should use the syntax:

    [A;B]
    

    not

    [A B]