Plotting results of for loop on one graph

Hi, I am relatively unexperienced with MATLAB, so bear with me! I created a for loop where two of the values in my matrix are functions of r, and then further operations are performed with each iteration of the matrix. The values are generated but then when I try to graph them, I get either an error or no graph. Here's my code:
for r=0:0.001:1.1;
M=[-1*(r.^2)+1.3 -0.3;-0.3, -0.3*(r.^2)+0.3];
f=[1;0];
z_1=M\f;
plot(r,z_1)
end
Thank you in advance!
**As an added comment, if I try to express z_1 as z_1(r), or do this operation without the for loop, I get the error "Subscript indices must either be real positive integers or logicals" and I also don't know how to fix that...

 Akzeptierte Antwort

Jon
Jon am 17 Sep. 2015
Bearbeitet: Jon am 17 Sep. 2015

8 Stimmen

Replace your plot(r,z_1) with this:
plot(r,z_1,'.'); hold on
If you don't turn the "hold" to "on" then the figure is overwritten with each iteration.
You can also achieve this by adding these commands before the loop:
figure; hold on

5 Kommentare

Kelly Berry
Kelly Berry am 17 Sep. 2015
Thanks Jon! I am still getting the "Subscript indices must either be real positive integers or logicals" error-any idea how to fix that?
Jon
Jon am 17 Sep. 2015
Bearbeitet: Jon am 17 Sep. 2015
When you reference a vector, you must use an integer position. For example, if you want the third element of
ex = [3 5 6 9 2];
you would type
ex(3)
if you tried
ex(.001)
You would get the error that you're seeing. You are trying to reference using non-integers, since r takes on decimal values. The example of Jae Song below shows a nice way around this problem.
Kelly Berry
Kelly Berry am 17 Sep. 2015
Thank you! I finally got it!!!
Realy thank you.
faith
faith am 3 Nov. 2023
can we use the same solution to plot more than one graph in each iteration of the loop? for example, I want to sketch the graph of two circles, and one line further. I, also want to shrink one of the circles in each iteration of a loop. thanks in advance

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jae Song
Jae Song am 17 Sep. 2015

1 Stimme

The z_1 matrix variable needs to have index; Please see the following code
x = 0:0.001:1.1;
s = size(x,2); %: get the array size
for i=1:s
r = x(i);
M=[-1*(r.^2)+1.3 -0.3;-0.3, -0.3*(r.^2)+0.3];
f=[1;0];
z_1(:,i)=M\f;
end
figure(1); plot(x,z_1(1,:));
figure(2); plot(x,z_1(2,:));

2 Kommentare

Kelly Berry
Kelly Berry am 17 Sep. 2015
Thanks Jae!! I used a combination of your answer and Jon's, and I finally got it to work! I definitely appreciate your help!
Nasir Mehmood
Nasir Mehmood am 25 Sep. 2020
Hello dear Jae Song! I'm new to Matlab.
When I change
x = 0:0.001:1.1;
to
x = 0:0.01:1.1;
no plot is obtained. How to handle this issue?
Also can you describe a little about these lines:
z_1(:,i)=M\f;
and
figure(1); plot(x,z_1(1,:));

Melden Sie sich an, um zu kommentieren.

Hansa Prasad
Hansa Prasad am 15 Feb. 2019

0 Stimmen

T0=24;
m0=1;
eta=4.2;
T(1)=T0;
m(1)=m0;
C=216;
R=2;
Ta=26;
P=2;
Ts=21;
for t=1:300:1;
T(t+1)=(-1/(C*R))*(T(t)-Ta+m(t)*R*P*eta)+T(t);
plot(t,T(t))
end

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 17 Sep. 2015

Kommentiert:

am 3 Nov. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by