MATLAB Answers

How to add plot in the outside with 2 for loop ?

6 views (last 30 days)
Fahmy Shandy
Fahmy Shandy on 17 Dec 2019
Commented: darova on 17 Dec 2019
Suppose i have 2 for loop in my script. The last for loop is continue the previous for loop and so does the plot.
And i know i can do this
for i=1:3
% some code
plot(x,y);
hold on
end
for i=4:n %n is the last iteration
% some code
plot (x,y);
hold off;
end
My plot is shows nicely, But it makes my code running slow.
And if i do this :
for i=1:3
% some code
end
plot(x,y);
hold on
for i=4:n %n is the last iteration
% some code
end
plot (x,y);
hold off;
My graph is messed up when i'm doing that and my code is running fast. But there some missing plot in a certain interval.
I want my code running fast and shows up a complete graph.
Please help me. Thanks And let me know if it's not clear.

Answers (2)

ME
ME on 17 Dec 2019
When you are plotting inside the for loop, I'd bet you are plotting collections of 7 data points each time around the loop (i.e. the ones used in your Adams-Bashforth method). Then, when you take the plot command outside your for loop, you are only plotting the last 7 points you had. That would mean you aren't saving the old data points properly as you go along.
It is extremely hard to help you find out where exactly you have gone wront without being able to see the whole code you have used though. That means we are unable to try to run it ourselves and find out what will fix your issue.
This is also likely to be what is slowing your code down in all of your previous questions too!

Thuso Gaosenngwe
Thuso Gaosenngwe on 17 Dec 2019
Hello i would like to think that the reason your second graphs not coming as expected is because its only plotting the last points that it calculated in the loop. im not sure if theres any other way to do this unless maybe you optimize the calculation but that can sometimes not give true results .

Products


Release

R14SP1

Community Treasure Hunt

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

Start Hunting!