Variable assigns value but wont plot

3 次查看(过去 30 天)
clc, clear , close all
%Establish given conditions
dt= .001; %time interval (sec)
e=.05; %damping ratio 5%
H=30; %post height (m)
A=1.82; %cross sectional area (m^2)
S= 1.67; %Section Modulus (m^3)
m=10; %mass of water tank (Mg)
k=5; %Beam stiffness coefficient (MN/m)
timevec= 0:dt:3; % 3 sec time vector
a=.5; %alpha constant
B=.25; %beta constant
ui=0; %initial displacement
vi=0; %initial velocity
%Applied force
%F= [400,0,400,0,400,0];
%t= [0,.06,.15,.21,.30,.36];
wn= sqrt(k*10^3/m); %natural frequency
ccrit=2*m*wn; %critical damping
c=ccrit*e; %System damping coefficient
%Initial accel calculation
ai= 400/(m*10^3); %m/s^2
figure()
for i= timevec
if i>=0 && i<.06
Fi1=400;
elseif i>=.06 && i<.15
Fi1=0;
elseif i>=.15 && i<.21
Fi1=400;
elseif i>=.21 && i<.30
Fi1=0;
elseif i>=.30 && i<.36
Fi1=400;
elseif i>=.36
Fi1=0;
end
%displacement at ti+1
ui1term1= ( ( 1/(a*dt^2)*m + B/(a*dt)*c + k )^-1 ) ;
ui1term2= m * (ui/(a*dt^2) + vi/(a*dt) + ai*(1/(2*a) -1));
ui1term3= c * ( B*ui/(a*dt) + (B/a -1)*vi + (B/a -2)*(dt/2)*ai);
ui1= ui1term1 * (Fi1 +ui1term2 +ui1term3 ) ;
%acceleration at ti+1
ai1term1= 1/(a*dt^2)*(ui1-ui);
ai1term2= vi/(a*dt);
ai1term3= ai*(1/(2*a) - 1);
ai1= ai1term1 - ai1term2 - ai1term3;
%velocity at ti+1
vi1= vi +(1-B)*dt*ai + B*dt*ai1;
plot(i,ui,'b')
hold on
ui=ui1;
ai=ai1;
vi=vi1;
end
So the problem is my code will assign values while to ui (displacement variable) but it wont plot the graph representing the displacement curve of the system. Any help please?

采纳的回答

Adam Danz
Adam Danz 2019-11-21
Instead of this line,
plot(i,ui,'b')
define a marker type
plot(i,ui,'bo') %or b. b^ bs etc....
The reason the first line doesn't show any results is that you're asking for a line without any markers but i and ui are only scalar values (single numbers) and you need at least 2 points to draw a line.
  2 个评论
Javier Alvarez
Javier Alvarez 2019-11-21
Ok. I thought by putting the plot in the for loop i would be plotting down the data points as they were assigned into the ui variable so it would progressively plot the function while the loop ran. I made the ui variable a vector which added the values as the loop ran and put the plot outside the loop and got an answer so I guess im good now. Thank you!!
P.s: If you could, please explain why putting plots in loop wont work? Thankss :)
Adam Danz
Adam Danz 2019-11-21
编辑:Adam Danz 2019-11-21
"Ok. I thought by putting the plot in the for loop i would be plotting down the data points as they were assigned into the ui variable so it would progressively plot the function while the loop ran."
Yes, that's true. On each iteration of your loop, i and ui are overwritten with a new value that is added to the plot. The old values remain on the plot due to 'hold on' but they are replaced in your variables.
"I made the ui variable a vector which added the values as the loop ran and put the plot outside the loop"
That's an even better approach because you keep your i and ui values. Just make sure you are pre-allocating those variables prior to the loop.
"If you could, please explain why putting plots in loop wont work? Thankss :) "
As I explained above, keeping the plot() lines in the loop will work if you provide a marker type (as in my answer). On each iteration of your loop, i and ui are single values. When you don't specify a marker, matlab just draws a line but you need 2 points to draw a line. Your variables only contain 1 point on each iteration.
Here's a demo
figure()
plot(0,0,'b') %nothing is plotted
figure()
plot(0,0,'bo') % now you have a dot
figure()
plot([0,1],[0,1],'b') %now you have a line

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2019-11-21
If you want to add points to your line one by one as your loop iterates you could create an animatedline before entering your for loop and addpoints to it inside the loop.

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by