it does not plot V1,V2 untill I put * in plot order. why?

1 view (last 30 days)
Hi guys, I want to plot s/th in matlab. But my matlab does not give me the right answer until I put * in plot in V1,V2,x1 plot. why?
My code is:
clc;
clear all;
% syms x1(t)
% syms x2(t)
% syms x3(t)
% syms x4(t)
x1(1)=57.5;
x2(1)=8.5;
x3(1)=0.051;
x4(1)=4.8;
deltat=10^-3;
B=[0.0015 -0.0015 -6.9*10^-12;-5.9*10^-5 -9*10^2 5.9*10^-11;3.46*10^-5 5.2*10^-5 3.24*10^-11;-0.0167 0.0239 -1.0733*10^-9];
u=[32;32;80];
for i=1:100
% (x1(i+1)-x1(i))/deltat=-1.5*x1(i)+x2(i);
% (x2(i+1)-x2(i))/deltat=(x1(i)+x2(i))*sin(x1(i))-3*x2(i);
x1(i+1)=(3.7*10^-15*x1(i)+7.6*10^-6*x2(i))*deltat+x1(i);
x2(i+1)=(-4*10^-16*x1(i)-6.5*10^-5*x2(i)-2.8)*deltat+x2(i);
x3(i+1)=(2.37*10^-16*x1(i)-0.142*x3(i))*deltat+x3(i);
x4(i+1)=(-8.15*10^-14*x1(i)-0.5535*x2(i)+18.21*x3(i)+0.0833*x4(i))*deltat+x4(i);
V1(i)=0.5*(x1(i).^2+x2(i).^2+x3(i).^2+x4(i).^2);
V2(i)=0.5*x1(i).^2+x2(i).^2+x3(i).^2+x4(i).^2;
end
for i=1:100
figure(1)
plot(x1(1,:),x2(1,:))
hold on;
plot(x1(1,1),x2(1,1),'bo') % starting point
hold on;
plot(x1(1,end),x2(1,end),'ks') % ending point
hold on;
figure(2);
plot(i,x1(1,i),'*-')
hold on;
figure(3)
plot(i,V1(1,i),'*-')
hold on
figure(4)
plot(i,V2(1,i),'*-')
hold on;
end

Accepted Answer

azam ghamari
azam ghamari on 14 Apr 2019
yes it may works, thanyou, but I said about difference between using *- and not using. I said why it plot just when we use *-?
Thanks
  1 Comment
Walter Roberson
Walter Roberson on 14 Apr 2019
The default is not to use any marker. When you use '*-' you tell it to use * as a Marker. plot will plot markers when you tell it to plot individual points. plot() needs at least two adjacent finite points in order to plot a line, but it only needs isolated finite points in order to plot markers.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 12 Apr 2019
In MATLAB, type this control-a (to select all), control-i (to fix indenting), control-c (to copy). Then paste back here, highlight and click the code icon.
You say "I want to plot s/th". What are s and th? I don't see them in the code. The rest of the code runs, but it's a bit unnecessary to have it plot every single point one at a time unless you were going to put a break point in the code to watch it slowly.
When you plot a single element of V1 or V2, at a particular i value.....
If you don't use * to place a marker, and only use - (which connects two data points with a line), then you will see nothing because you're plotting only a single point, not two points that can be connected by a line.
  3 Comments
Image Analyst
Image Analyst on 14 Apr 2019
Yes, using '-' will connect the points, but you're not plotting multiple points in any of your calls to plot V1 or V2. Like I said, you're plotting only ONE point, NOT two or more:
plot(i,V1(1,i),'*-') % Plot one single value, V1(1,i)
The above will plot a marker * but no line since only one point is being plotted, not two or more.

Sign in to comment.


azam ghamari
azam ghamari on 14 Apr 2019
Dear Walter
I read that, it says taht save the items in a vector and then plot, it isn't matter hear, my problem is that why with putting '*-' in plot order it shows nut without that it does not show any thing. By the way, I have the values of V2 in workspace. they are for 1000 points, not just one point. Why do you say that it plots only a single point?!
Thanks
  1 Comment
Walter Roberson
Walter Roberson on 14 Apr 2019
Your i is a scalar because you are in a for i loop. plot(i,V2(1,i),'*-') is plot() of a scalar i value and a scalar value extracted from V2. This is a request to plot() only one point in that one call. plot() never creates a line unless you plot() at least two adjacent finite values in the same call . For example, plot(i:i+1, V2(1,i:i+1), '*-') would be a request to plot two adjacent points and it would be willing to create a line between the two.
You should remove the
figure(4)
plot(i,V2(1,i),'*-');
hold on
from the loop, and then after the loop you should put in
figure(4)
plot(1:100, V2(1,1:100), '*-')

Sign in to comment.

Tags

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!