Help with ODE45 and Plotting in MATLAB Loop

Click For Summary
SUMMARY

The discussion focuses on using MATLAB's ODE45 function within a loop to solve ordinary differential equations and plot the results. The user seeks assistance in saving multiple outputs from the ODE45 function and plotting them simultaneously. The recommended solution involves preallocating arrays for time and solution values, using the commands ts(i,:) = t; and hs(i,:) = h; within the loop, and employing hold on to retain previous plots. Additionally, the user is advised to plot all results at once outside the loop to avoid overwriting previous graphs.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of ODE45 function for solving ordinary differential equations
  • Knowledge of matrix operations and preallocation in MATLAB
  • Basic plotting techniques in MATLAB
NEXT STEPS
  • Learn about MATLAB's ODE45 function and its parameters
  • Explore MATLAB matrix preallocation techniques for efficiency
  • Study MATLAB plotting functions, specifically hold on and multi-plotting
  • Investigate handling variable-length outputs in MATLAB
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working with numerical methods for solving differential equations, as well as students and professionals looking to enhance their plotting skills in MATLAB.

FlvD
Messages
4
Reaction score
0
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..
 
Physics news on Phys.org
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...?
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K