# Help with ODE45 and Plotting in MATLAB Loop

• MATLAB
• FlvD
In summary, To save multiple values of t and h in a loop, you can use the lines:ts(i,:) = t;hs(i,:) = h;However, make sure to preallocate the vectors before the loop to avoid errors. To plot all the values at once, use the command plot(t,hs) outside the loop.
FlvD
Hey i need help in ODE45...i'm new to PF...pls help me

i have the following code

>>for i=1:5
>>tspan=[0 30]
>>h0=Hv(i)
>>[t,h]=ode45['a ODEfunction',tspan,h0]
>>plot(t,h)
>>end

in this loop how can i save 5 values of [t,h],
as t, h produces each loop run like follows
t=
1
2
3
4
.
.
.
h=
5
6
7
8..

also i need to plot all of them n i could view the 5 graphs them..pls help..

You can add a line in the loop, after [t,h] = ode...

that goes:

ts(i,:) = t;
hs(i,:) = h;

but you might want to preallocate those vectors before the loop:

ts = zeros(5,n);
hs = zeros(5,n);

where n is the number of points in each set.The way you're plotting now, you wipe out your last plot every time. You can either add a "hold on" command that keeps the old data up when it plots the new data, or you can plot the whole set at once (hs).

also, ts, might won't be different every time as is, so you would have plot(t,hs) at the end OUTSIDE the for loop and it will plot all five cases at once, each a different color

Thanks for the replies..

@ Pythagoran..I tried the same way...But another problem arises

As in each go through the loop the length(t) & length(h) varies in the for loop.
Hence the command t(i,:) produces one result but then shows error as :

'Subscripted assignment dimension mismatch'...What to do..pls help me...?

## 1. How do I use the ODE45 function in MATLAB?

The ODE45 function in MATLAB is used to solve ordinary differential equations (ODEs) numerically. It takes in the ODE, initial conditions, and a time span as inputs, and returns the solution at specified time points. To use ODE45, you need to define your ODE as a function and then call the ODE45 function with the appropriate inputs.

## 2. How do I plot the results from ODE45 in a loop?

To plot the results from ODE45 in a loop, you can use a for loop to iterate through the different time points and plot the corresponding solution values. You can also use the hold on command to prevent previous plots from being cleared when plotting in a loop.

## 3. Can I customize the plot generated by ODE45?

Yes, you can customize the plot generated by ODE45 by using various MATLAB commands such as title, xlabel, ylabel, and legend. You can also change the color, line style, and marker of the plot using the plot command.

## 4. How do I add multiple ODEs to my plot using ODE45?

You can add multiple ODEs to your plot by defining them as separate functions and then using the hold on command to plot them together. Alternatively, you can also use the subplot function to create a grid of plots and plot each ODE separately in a different subplot.

## 5. What are some common errors when using ODE45 and plotting in a loop?

Some common errors when using ODE45 and plotting in a loop include not properly defining the ODE as a function, not specifying the correct input arguments for ODE45, and not using the hold on command when plotting in a loop. It is also important to ensure that the time span specified for ODE45 matches the time points used for plotting.

• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
473
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K