Why does my red lines on the graph stop?
1 view (last 30 days)
Show older comments
Charles Steyn
on 16 Jun 2020
Commented: Charles Steyn
on 19 Jun 2020
For code, the red lines (vl and rvr) on the graph need to reach a certain point, which I have defined as 50, then it should remain constant. When I get the graph, the red lines stop when it reaches 50 instead of continuing constantly for the remainder of the time. How can I get the lines to show that it is constant until the end?
Any help would be greatly appreciated.
%Constants
m = 300
rw = 0.356
Iw = 2.7
N = m*9.81
Td1 = 1.134826021738053e+03
Td2 = 1200
%Magic Formula
B_1 = 10
C_1 = 1.9
D_1 = 1
E_1 = 0.97
t = [0:0.1:10]
al1= NaN(1,length(t));%empty acceleration vector
al2= NaN(1,length(t));%empty acceleration vector
vl1= NaN(1,length(t));%empty velocity vector
vl2= NaN(1,length(t));%empty velocity vector
rar1= NaN(1,length(t));%empty acceleration vector
rar2= NaN(1,length(t));%empty acceleration vector
rvr1= NaN(1,length(t));%empty velocity vector
rvr2= NaN(1,length(t));%empty velocity vector
slip1 = NaN(1,length(t));
slip2 = NaN(1,length(t));
CF_1 = NaN(1,length(t));
CF_2 = NaN(1,length(t));
vl1(1) = 0
vl2(1) = 0
rvr1(1) = 0
rvr2(1) = 0
slip1(1) = 0.16
slip2(1) = 0.16
for i = 2:100
CF_1(i) = D_1*sin(C_1*atan(B_1*slip1(i-1)-E_1*(B_1*slip1(i-1)-atan(B_1*slip1(i-1)))))
CF_2(i) = D_1*sin(C_1*atan(B_1*slip2(i-1)-E_1*(B_1*slip2(i-1)-atan(B_1*slip2(i-1)))))
al1(i) = (1/m)*(CF_1(i)*N)
al2(i) = (1/m)*(CF_2(i)*N)
vl1(i)=vl1(i-1)+al1(i)*0.1;
vl2(i)=vl2(i-1)+al2(i)*0.1;
rar1(i) = (1/Iw)*(Td1-(CF_1(i)*N*rw))*rw;
rar2(i) = (1/Iw)*(Td2-(CF_2(i)*N*rw))*rw;
rvr1(i)=rvr1(i-1)+rar1(i)*0.1;
rvr2(i)=rvr2(i-1)+rar2(i)*0.1;
if rar1(i)<0
rar1(i)=0
end
if vl1(i)>50
al1(i) = 0
vl1(i) = vl1(i-1)
rar1(i) = 0
rvr1(i) = rvr1(i-1)
end
slip1(i) = (rar1(i)-al1(i))/rar1(i)
slip2(i) = (rar2(i)-al2(i))/rar2(i)
end
figure,plot(t,vl1,'r',t,rvr1,'r',t,vl2,'b',t,rvr2,'b')
% figure,plot(t,rar1)
% figure,plot(t,slip)
% figure,plot(t,CF_1)
2 Comments
Accepted Answer
Karthik Malisetty
on 18 Jun 2020
Hi Charles Steyn,
My understanding is that in the ‘if’ section, you are setting the values of al1(i) and rar1(i) to zero.
if vl1(i)>50
al1(i) = 0
vl1(i) = vl1(i-1)
rar1(i) = 0
rvr1(i) = rvr1(i-1)
end
This would make the expression
slip1(i) = (rar1(i)-al1(i))/rar1(i)
as 0/0 which is ‘NaN’ in Matlab.
So, slip1(i) is set to NaN (precisely at i=53) which in turn will set vl1(i) = ‘NaN’ (at i=54). So, the if condition fails at i = 54.
Therefore, the values of vl1 will remain as NaN for i=55 to 100. These NaN values can’t be reflected in plot.
Hence, the red line plots in the graph are stopped.
Removing the lines al1(i)=0 and rar1(i)=0 would give you those horizontal lines. Although, you should check that it wouldn’t effect any other program (if any)
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!