I am having an issue with a for loop plot. The error is returned on the line that says "plot(x,y)" on the code which follows. It states "Subscript indices must either be real positive integers or logicals." Also, if there is any way to label the plots in a legend as they are created that would be great. Thanks in advance for any help.
for N=30:10:50
g=9.81;tau = 0.0076;
tmin = 0; tmax = 10;
v0=50*sin(deg2rad(N));y0=0;
t=linspace(tmin,tmax, 1000);
dt=t(2)-t(1);
a= zeros(1,numel(t));
v= zeros(1,numel(t));
y= zeros(1,numel(t));
v(1)=v0;
y(1)=y0;
for i=2:1000
a(i-1)= -g+(tau*abs(v(i-1))*v(i-1));
v(i)= v(i-1)+a(i-1)*dt;
y(i)= y(i-1)+v(i-1)*dt+(1/2)*a(i-1)*dt^2;
end
vx0=50*cos(deg2rad(N));x0=0;
ax= zeros(1,numel(t));
vx= zeros(1,numel(t));
x= zeros(1,numel(t));
vx(1)=vx0;
x(1)=x0;
for i=2:1000
ax(i-1)= -tau*(vx(i-1))^2;
vx(i)= vx(i-1)+ax(i-1)*dt;
x(i)= x(i-1)+vx(i-1)*dt+(1/2)*ax(i-1)*dt^2;
end
plot(x,y);
ylim([0,65]);
end

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Okt. 2017

1 Stimme

In some code not shown here, you accidentally assigned a value to a variable named plot

5 Kommentare

Jay
Jay am 28 Okt. 2017
Bearbeitet: Jay am 28 Okt. 2017
Thanks. I tried to alter the above code to generate my legend. It gave me an error, so I changed it back to the above code and tried to run it again, but it continued to give me the same error. I thought that I might have deleted something important, but I even cancelled all changes and opened the original document above and it still gave me the error. I opened it on another computer and it worked. This is strange.
Stephen23
Stephen23 am 30 Okt. 2017
"This is strange."
Not at all. You created a variable named plot.
Jay
Jay am 30 Okt. 2017
I was under the impression that in livescript, every time I click "run all" it generates new variables. If I changed the code to the above script and clicked "run all," would it not delete the old variable and treat it as a plot? This is just for future reference.
Walter Roberson
Walter Roberson am 30 Okt. 2017
No, anything that is in your base workspace remains in your base workspace, and any variables you assign in the live script get assigned in the base workspace. Live scripts do not have their own workspace.
Jay
Jay am 31 Okt. 2017
Understood. Thank you. You have been very helpful.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

Jay
am 27 Okt. 2017

Kommentiert:

Jay
am 31 Okt. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by