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
채택된 답변
추가 답변 (1개)
Kevin Holst
2012년 2월 6일
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
Phong Pham
2012년 2월 7일
Kevin Holst
2012년 2월 7일
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.
Phong Pham
2012년 2월 7일
Phong Pham
2012년 2월 7일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!