Newbie Asks: Plotting MATLAB Commands Output in PF

Click For Summary
SUMMARY

The discussion focuses on plotting multiple outputs from MATLAB commands using the ODE45 solver. The user seeks assistance in generating individual plots for each result from a loop iterating over a vector. Key solutions include utilizing the subplot command to create subplots for each iteration and storing results in a matrix for simultaneous plotting. The final recommendation is to avoid using 'i' as a loop variable to prevent conflicts with the imaginary unit in MATLAB.

PREREQUISITES
  • Basic understanding of MATLAB syntax and commands
  • Familiarity with the ODE45 function for solving ordinary differential equations
  • Knowledge of plotting functions in MATLAB, specifically plot and subplot
  • Understanding of matrix operations in MATLAB
NEXT STEPS
  • Learn how to use the subplot function in MATLAB for creating multiple plots
  • Research the syntax and usage of the ode45 function in MATLAB
  • Explore matrix manipulation techniques in MATLAB, particularly for storing and plotting data
  • Investigate best practices for naming variables in MATLAB to avoid conflicts with built-in functions
USEFUL FOR

Students and professionals using MATLAB for numerical simulations, particularly those working with differential equations and data visualization techniques.

FlvD
Messages
4
Reaction score
0

Homework Statement


Hello all, I'm new to PF..Pls help..
I'm stuck in the following query.

I've MATLAB commands as follows:

Hv=[1,2,3,4,5]
for i=1:5
tspan=[1 30]
h0=Hv(i)
[t,h]=ode45[expression]
plot(t,h)
end

And here i need to see induvidual plots of my 5 results..I donno to save and then plot. Pls help.



Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
Hello.

First of all, when putting code in the posts, there's a nifty little command of ['code']insertcodehere[/'code'] that you can use that really tidies things up (without the apostrophes of course). So it'd look like this:

Code:
Hv=[1,2,3,4,5]
for i=1:5
tspan=[1 30]
h0=Hv(i)
[t,h]=ode45[expression]
plot(t,h)
end

Okay, so I'm not exactly sure what you're asking... but first of all, are you getting any errors? What is your output right now? It's difficult to help without knowing the expression you're putting into the ode solver.

However, if this helps there are some commands that could help you "save and plot results." One is the subplot command. Look into using your iteration variable (i) to move to the next subplot. Or you could simply have the figure number be dependent on i as well.

So something like:

Code:
figure(1)
Hv=[1,2,3,4,5]
for i=1:5
tspan=[1 30]
h0=Hv(i)
[t,h]=ode45[expression]

subplot(5,1,i)
plot(t,h)
end

Like I said, let me know if I'm not addressing your question, as I'm a bit unsure as to what you're asking.
 
Hi Flvd, you need to put all of your "h" vector solutions into a single matrix if you want a single plot command to plot them all simultaneously. The ":" syntax is really useful here. For example, if h is a row vector then "y(k,:) = h" sets "h" as the whole k-th row of the y matrix.

So you need something like this :

Code:
Hv=[1,2,3,4,5]
tspan=[1 30]               % No need to put this in the loop

for k=1:5                  % Best to leave "i" as pre-defined constant = sqrt(-1)
  h0 = Hv(k)
  [t,h] = ode45[expression] % Presumably "expression" includes the function an init conditions
  y(k,:)=h
end                         % You need to end the for loop here

plot(t,y)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
2
Views
1K