Creating a loop of a script
7 次查看(过去 30 天)
显示 更早的评论
Hi,
I need to loop a script but i dont know how with using the for function
If i copy the script down it will continue on and work but this isn't very productive
The script is:
t_step=0.1
s_step=0.5 % Distance step of 0.5m
s_Zero=0
s=s_Zero+s_step
t=0
v_Zero=v_Lat
a=0.1; % Long Acceleration
% Loop starts Here
v=v_Zero+(a*t_step);
Tractive_Force_Tyre=mu_Long*((2.006*v*v*wdR)+(g*m*wdR)+(m*a*H/L)); % Zero Acceleration
if v<Max_First
Tf=Tf_First;
elseif (Max_First<v) && (v<Max_Second)
Tf=Tf_Second;
elseif (Max_Second<v) && (v<Max_Third)
Tf=Tf_Third;
elseif (Max_Third<v) && (v<Max_Fourth)
Tf=Tf_Fourth;
elseif (Max_Fourth<v) && (v<Max_Fifth)
Tf=Tf_Fifth;
else (Max_Fifth<v) && (v<Max_Sixth)
Tf=Tf_Sixth;
end
if Tractive_Force_Tyre>Tf;
disp('Engine Limited');
a_end=Tf/m;
else Tf>Tractive_Force_Tyre;
disp('Traction Limited');
a_end=Tractive_Force_Tyre/m;
end
v_Zero=v;
s_Zero=s;
a=a_end;
t_step=((sqrt((2*a.*s)+(v_Zero^2))-v_Zero)/a);
v=v_Zero+(a*t_step);
t=t+t_step;
s=s_step+s;
% Loop Finish Here
3 个评论
dpb
2017-3-28
What have you tried? For what condition do you want to loop? It would seem likely that while might be more suitable than for here to the overall time.
采纳的回答
Jan
2017-3-28
I do not understand "loop in back to the start t recalculate for the new values of a and s."
To create a loop start with defining, what should be changed fro iteration to iteration. Perhaps you want:
for t = 0:t_step:t_end
... your code
end
with a matching definition of t_end. Please try this and explain the occurring problems. This might help to get a better understanding of your problem.
0 个评论
更多回答(1 个)
John BG
2017-3-28
Hi Conor
lower_lim=.001; % avoid null division
gear=[lowerLim Max_First Max_Second Max_Third Max_Fourth Max_Fifth Max_Sixth upper_lim]
Tf=gear(min(find(v/gear)<1))
then you can proceed with the Tractive_Force_Tyre sorting with obtained Tf threshold above
if Tractive_Force_Tyre>Tf;
disp('Engine Limited');
a_end=Tf/m;
else Tf>Tractive_Force_Tyre;
disp('Traction Limited');
a_end=Tractive_Force_Tyre/m;
end
..
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
2 个评论
John BG
2017-3-28
编辑:John BG
2017-3-28
Mr Connor
After a bit of guessing on not supplied parameters got your loop to spin,
since the many guessed values, can't tell if it's the Columbia taking off or a pop-corn jumping off the pot.
Please note, not plotting s v a, but the logged values in respective vectors vs vv va:
.

.
Also note the distance is also plot, but it's stuck on x axis.
quick-fix:
.
clear all; clc
mu_Long=0.001 % whatever friction or elasticity coeff it is
wdR=0.001 % please define this other coeff
g=9.81 % m/s^2 sea level, Earth
m=.5 % test mass
H=.5 % chassis distance to road surface? feet? metres??
L=1; % parking pillar?
Max_First=.1
Max_Second=.5
Max_Third=1
Max_Fourth=1.5
Max_Fifth=2
Max_Sixth=2.5
upper_lim=3
dt=0.1
ds=0.5 % Distance step of 0.5m
s_Zero=0
v_Zero=0 %v_Lat
lowerLim=.001; % avoid null division in gear
vs=s_Zero % space start value
vv=v_Zero % velocity start value
vs=s_Zero+ds % init log vector vs
a0=0.1;
vv=[v_Zero+(a0*dt)]; % init log vector vv
va=.1
for t=0:dt:60
a =va(end) % read a
v=vv(end) % read v
Tractive_Force_Tyre=mu_Long*((2.006*v*v*wdR)+(g*m*wdR)+(m*a*H/L)); % obtain Tractive_Force_Tyre Zero Acceleration
gear=[lowerLim Max_First Max_Second Max_Third Max_Fourth Max_Fifth Max_Sixth upper_lim];
Tf=gear(min(find(v./gear)<1)); % obtain Tf from Tractive_Force_Tyre
if Tractive_Force_Tyre>Tf; % obtain a, a_end removed
disp('Engine Limited');
a=Tf/m;
else Tf>Tractive_Force_Tyre; % if satured acceleration then a_end is not used, must be used
disp('Traction Limited');
a=Tractive_Force_Tyre/m;
end
s=vs(end) % read
s=ds+s; % update log vector
vs=[vs s] % save
% v_Zero=v;
% s_Zero=s;
% a=a_end;
a=((sqrt((2*a.*s)+(v_Zero^2))-v_Zero)/dt); % update a
va=[va a] % save a
v=v+(a*dt);% update v
vv=[vv v] % save v
% v=v_Zero
% t=t+dt;
end % LOOP ends here
hold all;plot(vs) % distance
plot(vv) % velocity
plot(va) % acceleration
grid on
since I got it to spin, the loop, I kindly ask if you would consider marking my answer as accepted answer to your question by clicking on the blue button you should see, top left corner?
Appreciating time and attention
regards
John BG
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Performance 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!