Problem with if else loop

Hi dear, I am trying to model a car accident by analysing skid marks over a track. There is a patch of wet ice present, and the friction coefficient is give. Between 0 to 14 metres, the car is on asphalt, between 14-31m, there is a patch of ice, and 31 to 48 there is still skid marks. I think my model have some issues with executing the if statement after the 14-31m condition. How do i fix this?
for k= 2:length(t)
if s<14
a=-u*g;
u=0.7;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif 14<s<31
u=0.12+0.07*exp(0.06*v(k-1));
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif 31<=s
u=0.7;
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif v(k)<0.0
break;
end
end
disp(v);
figure

 Accepted Answer

Cris LaPierre
Cris LaPierre on 17 Mar 2020
Edited: Cris LaPierre on 17 Mar 2020
There are a couple issues I see.
  1. You have too many end keywords. You only need two here.
  2. To test multiple conditions, you have to separate them and as single conditions and join them using & (and) or } (or)
dt=0.01; %change in time increments
g=9.81; %acceleration due to gravity
u=0; %variable u is the kinetic friction constant, for asphalt is 0.7, for ice is 0.07*exp(0.06*v)
% a=-u*g acceleration, calculated from the free body diagram
t=0:dt:10.0;%time in seconds taken for the car to fully stop (v=0)
v=zeros(1,length(t));
s=zeros(1,length(t));
v(1)=25;%velocity
s(1)=0;%displacement
for k= 2:length(t)
if s(k)<14
a=-u*g;
u=0.7;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif s(k)>14 & s(k)<31
u=0.12+0.07*exp(0.06*v(k-1));
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif 31<=s(k)
u=0.7;
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif v(k)<0.0
break;
end
end
disp(v);
figure
subplot(2,1,1)
plot(t(1:k),v(1:k));
title('Velocity vs time');
ylabel('Velocity(m/s)');
xlabel('Time(s)');
subplot(2,1,2)
plot(t(1:k),s(1:k));
xlabel('Time(s)');
ylabel('Displacement(m)');
title('Displacement vs time');

8 Comments

Hello dear, I realised that if I change the code into the above, the 2nd elseif statement does not execute and takes the velocity straight to zero..
Kind regards. (I have fixed all of the other mistakes though thanks!!!!)
Cris LaPierre
Cris LaPierre on 17 Mar 2020
Edited: Cris LaPierre on 17 Mar 2020
I've corrected my reply - your conditions around 31 were correct.
You can only check one value in an if statement. I missed this the first time, but s is a vector. Assuming you want to check the current value, change your conditionals to use s(k) instead of s (e.g. s(k)<14). If you want to instead check the previous value, use k-1.
ok! thanks!
I have tried this, but now the output graph does not look correct and the v(k) exceeds 0 to be like -45...
This is probably where your familiarity with the problem needs to take over from my best guessing. My last thought is that you probably need to use k-1, since s(k) is always 0, right? Also, keep in mind the code has no concept of reality. It is computing 10 seconds of data (your last condition about stopping if the velocity is negative is never used because one of the earlier conditions is always true.
If I were going to write this, I might do something more like this
dt=0.01; %change in time increments
g=9.81; %acceleration due to gravity
u=0; %variable u is the kinetic friction constant, for asphalt is 0.7, for ice is 0.07*exp(0.06*v)
t=0
v=25;%velocity
s=0;%displacement
k=1;
while v(k) >= 0
k = k+1;
if s(k-1)<14
u=0.7;
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif s(k-1)>14 & s(k-1)<31
u=0.12+0.07*exp(0.06*v(k-1));
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
elseif 31<=s(k-1)
u=0.7;
a=-u*g;
v(k)=v(k-1)+a*dt;
s(k)=s(k-1)+v(k)*dt;
end
t(k) = t(k-1)+dt;
end
figure
subplot(2,1,1)
plot(t,v);
title('Velocity vs time');
ylabel('Velocity(m/s)');
xlabel('Time(s)');
subplot(2,1,2)
plot(t,s);
xlabel('Time(s)');
ylabel('Displacement(m)');
title('Displacement vs time');
Ah ok, I see what you did. Thanks for that! cheers
You should be using double & right? To get rid of the warning
if s(k-1)>14 && s(k-1)<31
Cris LaPierre
Cris LaPierre on 17 Mar 2020
Edited: Cris LaPierre on 17 Mar 2020
Yes. I had tried that before I realized s was a vector so it gave an error. Works now.

Sign in to comment.

More Answers (0)

Categories

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!