Write a loop that can generate all data in one M-file with one variable change

dt= a variable that is going to change.
% loop
q= 0
for i = 0:dt:10
q=q+1
for a= 1:3
for b = 1:3
if a~=b
then
.....calculate x, y ,
..... calculate h ( h depends on x, y)
.... hh(q)= h
.....tt(q) = i+dt;
I want to write the loop for the whole thing where it accounts dt is changing , and save data for each run then plot hh vs tt for any given dt change , dt can be [.1, .01, .001, .0001, .00001]. Know the loop to calculate h cant change. Anybody knows how to write loop as i describe above and still can plot hh vs tt @ dt = .1 , dt = .01 , etc in the same graph . Thanks

 Akzeptierte Antwort

To answer your comments:
After looking back at the code, I made a big assumption that's not the best to make I think. I assumed that you're only interested in positive hh values. Try this code instead:
for j = -1:-1:-5
q = 0;
dt = 10^j;
for i = 0:dt:10
q=q+1
for a = 1:3
for b = 1:3
if a~=b
then
.....calculate x, y ,
.....calculate h ( h depends on x, y)
.... hh.(['dt' num2str(abs(j))])(q) = h
.....tt.(['dt' num2str(abs(j))])(q) = i+dt;
This will create two structures, hh and tt, with fields dt1, dt2, etc. To access the data you can plot it as:
plot(tt.dt1,hh.dt1,tt.dt2,hh.dt2)
You mentioned that it's only plotting the first column of data, is it actually only plotting the first column, or are the two sets of data overlapping exactly? This function appears to be a parametric study of dt, studying how dt size affects the plot. I would expect the plots to be on top of one another.

3 Kommentare

It shown all 3 graphs on the same plot now, Thanks so much for your helps . I appreciate that.
I was choosing fixed step number to collect the data like some dt collects at every 100 steps or 10 steps or 1 steps.
In my case, dt = .1 step to collect data is 1, dt = .01 step = 10. and dt =0.001 with step = 100.
There is a way to do that ?
Thanks again
I'm not sure I understand what you're asking there. When dt=0.1 data is collected at every 1 step, dt=0.01 data is collected at every 10th step? Is that correct?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

You could just add another dimension to your hh and tt calculations like this:
for j = -1:-1:-5
q = 0;
dt = 10^j;
for i = 0:dt:10
q=q+1
for a = 1:3
for b = 1:3
if a~=b
then
.....calculate x, y ,
..... calculate h ( h depends on x, y)
.... hh(q,abs(j))= h
.....tt(q,abs(j)) = i+dt;
Your hh and tt matrices would have a lot of zeros that you wouldn't want to plot, so your plot functions would need to be something like:
plot(tt(tt(:,1)>0),hh(tt(:,1)>0))
I don't have access to matlab right now so I can't say for 100% that that plot call will work as expected, but hopefully that'll point you in the right direction.

4 Kommentare

why do you put hh (q,abs(p))? what does it mean?
It's an extra dimension for that variable. I'm storing all of the values for dt = 0.1 in hh(1,1), hh(2,1), hh(3,1), etc. I'm storing all of the values for dt = 0.01 in hh(1,2), hh(2,2), hh(3,2), etc. And so on.
It works fine in the calculation but when i plot it is only show one plot. I want to have all 5 data of hh, tt on the same plot to see the different and how dt affects to hh vs time. The funny part is even I plot( tt(tt(:,1)>0),hh(tt(;,1)>0), tt(tt(:,2)>0),hh(tt(:,2)>0)), It still show the the graph of first column. Then i tried hold on hold off , it still shows the same. Then I tried to plot 2 data on the different figure it does the same thing. Do you have any idea what is wrong or how to plot all of data on the same figure. Thanks alot
I still dont know why the code gave me a lot of zero , if i use the code to calculate one by one then the zero will not show up.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 6 Feb. 2012

Bearbeitet:

dpb
am 3 Okt. 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by