Problem with the "Plot" function
1 次查看(过去 30 天)
显示 更早的评论
Hi, I need some help from those of you who are good at Matlab sinc I am not a profession.. I appreciate the time and effort.
I need to plot my time in x axis and my roll angle(x1) as y axis
----This is my parameters------- function dx = wheelchaircorrectversion (t,x) global m g d F h Ix w m=10; g=-9.81; F=5; d=10; h=20; Ix=30; w=20;
if t>0 && t>=0.01 F=5 elseif t>0.01 F=0 end
z=(sqrt((.5*w).^2+(.5*h).^2)).*(sin(atan((h/2)/w/2))); d=(sqrt((.5*w).^2+(.5*h).^2)).*(cos(atan((h/2)/w/2)));
dx=zeros(2,1); dx(1)=x(2); dx(2)=(-(m*g*d)+(F+z))/Ix; end
---- my main function---
clear all clc global m g d F h Ix w m=10; g=-9.81; F=5; d=10; h=20; Ix=30; w=20;
x1_ini = [0 0]'; t_dim = 1; i=1; dt = 0.01; duration = 0.05;
for t = 0.0 : dt : duration t0 = t; i=i+1; tf = (t+dt)*(1+eps); tspan = [t0 tf];
[t1,x1] = ode45('wheelchaircorrectversion',tspan,x1_ini);
n1 = length(x1); %length of a vector
tmp1 = x1(n1,:); %the last value
tmp2 = t1(t_dim)
x1_ini = tmp1; % update initial conditions
traj_p0(i,:) = x1_ini;
traj_t(i)= tmp2
end
subplot(211) %Creates axes in tiled positions plot(traj_t,x1(:,1)) xlabel('Time') ylabel('Roll Angle')
subplot(212) plot(traj_t,x1(:,2)) xlabel('Time') ylabel('Roll Velocity')
----
Whenever I try to plot these functions, it says that vectors needs to be in same length for plot(traj_t, x1(;,1)) and for plot(traj_t,x1(:,2))
I have tried everything to make the number of vectors equal to each other, but it didnt work..
could someone help?
回答(4 个)
Image Analyst
2013-1-27
编辑:Image Analyst
2013-1-27
More accurately, x1(:,1) needs to be the same number of elements at traj_t. x1(:,2) needs to be that length also. Do this before you call plot
size(traj_t)
size(x1)
Tell us what it says.
8 个评论
Youssef Khmou
2013-1-27
Try this :
1.Delete the traj_t variable that exists inside the loop.
2. create a vector time, outside the loop, of length 41 such as : time=linspace(0,duration,41).
3. replace,in the plot section, traj_t wit time'.
The code now works well, but are the results reasonable In Physic viewpoint ?
As you posted non organized code , i post the new version of the code here for further discussions by other users :
--------------------------------------------------------------------------
function dx = wheelchaircorrectversion (t,x)
global m g d F h Ix w
m=10; g=-9.81; F=5; d=10; h=20; Ix=30; w=20;
if t>0 && t>=0.01
F=5;
elseif t>0.01
F=0;
end
z=(sqrt((.5*w).^2+(.5*h).^2)).*(sin(atan((h/2)/w/2)));
d=(sqrt((.5*w).^2+(.5*h).^2)).*(cos(atan((h/2)/w/2)));
dx=zeros(2,1); dx(1)=x(2); dx(2)=(-(m*g*d)+(F+z))/Ix;
end -------------------------------------------------------------------
And the main program :
-------------------------------------------------------------------
clear all ,clc
global m g d F h Ix w
m=10;g=-9.81; F=5; d=10; h=20; Ix=30; w=20;
x1_ini = [0 0]'; t_dim = 1; i=1; dt = 0.01; duration = 0.05;
for t = 0 : dt : duration
t0 = t;i=i+1; tf = (t+dt)*(1+eps); tspan = [t0 tf];
[t1,x1] = ode45('wheelchaircorrectversion',tspan,x1_ini);
n1 = length(x1); %length of a vector
tmp1 = x1(n1,:); %the last value
tmp2 = t1(t_dim);
x1_ini = tmp1; % update initial conditions
traj_p0(i,:) = x1_ini;
%traj_t(i)= tmp2;
end
time=linspace(0,duration,41);time=time';
subplot(211), plot(time,x1(:,1)), xlabel('Time'), ylabel('Roll Angle')
subplot(212), plot(time,x1(:,2)), xlabel('Time'), ylabel('Roll Velocity')
--------------------------------------------------------------------------
Youssef Khmou
2013-1-28
编辑:Youssef Khmou
2013-1-28
Tae Yeong Kim,
so its about ordinary differential equation ! THE TIME IS ALREADY THERE !!
[t1,x1]=ode('wheelchaircorrectversion',tspan,x1_ini);
Position and velocity are in the 41x2 matrix x1 and the time axis is t1(41,1)
you can replace the vector "time" that we created with t1, ITS THE SAME .
About your last question i do not understand what you mean : you have a position X and Velocity V , you said you want to integrate the x1(:,1) to get the Velocity, i think its wrong :
YOU HAVE POSITION X , VELOCITY V and ACCELERATION A
DX/Dt= V(t) , DV/Dt= A, INTEG(V(t))=X(t), INTEG(A)= V(t)
so lets see :
---------------------------------------------------------------------------
plot(t1,x1(:,2)) % This is the velocity
hold on
Velocity2=diff(x(:,1))./diff(t1); % Diff function truncates to n-1
Velocity2(end+1)=Velocity2(end); % add the last element
plot(t1,Veclocity2,'r'),
-----------------------------------------------------------------------
Numerically its the same .
Advice :
1.do not declare the variables again the testing script .
2. try to comment every variable to explain to the reader , for example :
------------------------------------------------------------------------
m=10; % mass in Kg
g=-9.81; % Gravity acc in m/s²
F=5; % Force in Newton
d=10; % Distance between ..and .. in meteres....
I hope that helps
KHMOU Youssef,
Youssef Khmou
2013-1-28
Hi
i think there is a problem, based on what you wrote :
Z/d=h/w. and based on numerical values u gave h=w=20. in that case z=d which is wrong ,
what is the point in the center of the rectangle? what is its dynamic ?
4 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!