My code is just about there, but there is something not right...I can't figure it out. Help?!

Here's my code:
if true
% code
end
m=1428.8;
a_i=3;
b=1.04;
MaxSpeed= 65 * .4474;
SpeedLimit= 50 * .4474;
t=[0:.5:50];
a= NaN(length(t),1);
v= NaN(length(t),1);
x= NaN(length(t),1);
a(1)=a_i;
v(1)=0;
x(1)=0;
for k = 2:length(t)
v(k)= a.*t;
%a)
if v(k)==min(v(k),MaxSpeed);
a=0;
x(k)=v*t(k);
end
%b)
if x(k-1)<275
a(k)=(b*(v(k-1)).^2)/(2*m);
x(k)=(MaxSpeed*t)+ (.5*a(k)*(t(k)^2));
end
%c)
if x==1000
if v(k)>SpeedLimit
disp('keep coasting');
v(k)
end
end
end
subplot(1,3, 1);
plot(t, x);
xlabel('time(s)');
ylabel('distance(m)');
subplot(1,3, 2);
plot(t, v);
xlabel('time(s)');
ylabel('velocity(m/s)');
subplot(1,3, 3);
plot(t, a);
xlabel('time(s)');
ylabel('acceleration');

4 Comments

Here's the question:
You are driving a car along a straight road for t = 50s. Assume that friction is negligible but drag is not, where F .5b(v_n-1)^2 , b is a given constant, and v_n-1 is the velocity at the previous time step. For the duration of the trip as described below, calculate the position, velocity, and netacceleration of the car. Use one or more for loops to simulate the behavior of the car starting at t = 0 until t = 50s, with Δ t = 0.5s. Using subplot(), plot the position, velocity, and net acceleration as functions of time on a single figure with a 3x1 subplot and label the axes.
a. Starting from rest, where initial position of x(0) = 0 and v(0) = 0, you apply an initial, constant acceleration of a = 3m/s , until you reach 65 mph. Upon reaching 65 mph, you maintain a constant velocity.
b. At x = 275 m, you pass a speed limit 50 mph sign. Instead of braking, you let the car coast (i.e. no applied acceleration) for the rest of the drive.
c. At x = 1000 m, a police car is parked to the side of the road with a radar gun and needs to meet quota. Today, he is very strict about people not exceeding the speed limit and will pull you over if you do. If you continue to let the car coast, will you be able to slow down in time or will you get pulled over? Print the answer and the velocity just after you have passed the police car to the command window. m car = 1428.8kg (mass of car and you) b = 1.04N * s2/m2
Yes. We know. It's the 4th time you've posted it today.

Sign in to comment.

 Accepted Answer

I am pretty sure this is the correct code now. As for the sum(x>1000), the answer was 44. Is that the number of time steps it took for x to become greater than 1000? Here's the code:
m=1428.8;%mass of car
a_i=3;%initial acceleration
b=1.04;%constant
MaxSpeed= 65 * .4474;%mph - m/s
SpeedLimit= 50 * .4474;%mph - m/s
dt=.5;%time step
t=[0:dt:50];%time
N=length(t);%legthg of time
a= NaN(1,length(t));%empty acceleration vector
v= NaN(1,length(t));%empty velocity vector
x= NaN(1,length(t));%empty position vector
a(1)=a_i;%initial acceleration = 3
v(1)=0;%initial velocity = 0
x(1)=0;%initial position =0
for k=2:N
F_drag=.5*b*((v(k-1))^2);%drag force
a(k)=a(k-1)-(F_drag/m);%net acceleration
v(k)= v(k-1)+ ((a(k)+a(k-1))/2)*dt;%velocity
x(k)=x(k-1)+ dt*((v(k)+v(k-1)/2));%position
%a)
if x(k)>150%when acceleration should equal zero
a(k)=0;%acceleration = 0
v(k)=MaxSpeed;%velocity equals max speed
x(k)=x(k-1)+ dt*((v(k)+v(k-1)/2));
end
%b)
if x(k-1)>275%when you pass the 50mph speed limit sign
a(k)=-(F_drag/m);%acceleraton from the car is zero and the drag force is decelerating the car
v(k)= v(k-1)+ ((a(k)+a(k-1))/2)*dt;%new velocity
end
end
%c)
if x(k-1)>1000 && v(k)<=SpeedLimit %when you pass the police car
disp('keep coasting');%if the velocity os less than or equal to the speed limit
v(k);
speed=v(k)/.4474;
disp('speed in mph');
speed%displays velocity right after you pass the police car
else
disp('pull over');%if velocity is greater than the speed limit
v(k);
speed=v(k)/.4474;
speed
end
figure;%plots
subplot(3,1, 1)
plot(t, x)%time vs. position plot
xlabel('time(s)');
ylabel('position(m)');
subplot(3,1, 2)
plot(t, v)%time vs. velocity
xlabel('time(s)');
ylabel('velocity(m/s)');
subplot(3,1, 3)
plot(t, a)%time vs. acceleration plot
xlabel('time(s)');
ylabel('acceleration');

More Answers (3)

The very first line inside the for loop is not correct:
v(k)= a.*t;
First of all, you need to define a time increment before you even get to the for loop:
dt = 0.5;
Then, inside the for loop, you can compute the velocity at the current time step as a function of the velocity and acceleration at the previous time step:
v(k) = v(k-1) + dt*a(k-1);
and the position in a similar fashion:
x(k) = x(k-1) + dt*v(k-1);
Then, you need to compute the drag as a function of the velocity:
F_drag = ...
Then, the net force as the sum of the applied force and the drag:
F_net = F_applied + F_drag;
(assuming that the drag force is opposite in sign as the velocity; otherwise, change the '+' sign to a '-' sign in the above expression).
Finally, you can compute the net acceleration using Newton's second law:
a(k) = F_net / m ;
Does that make sense?
Please try to modify your code, and then re-post it here with specific questions if you still need help.
Rick

3 Comments

Also, I would strongly recommend that you try to get part (a) done correctly before you even attempt to solve parts (b) and (c). Crawl before you walk, walk before you run...
Jacob's comment moved here:
So now I have acceleration to equal zero when the velocity is MaxSpeed, but only in that instant of time. I know I have to change the acceleration equals from that point on, but I'm unsure how to do it...
Here's the new code:
for k=2:N
F_drag=.5*b*((v(k-1))^2);
a(k)=a(k-1)-(F_drag/m);
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
v(k)=min(v(k),MaxSpeed);
j=k(find(v(k)== MaxSpeed));
a(j)=0;
x(k)=x(k-1)+ dt*(v(k)+v(k-1));
end

Sign in to comment.

Thank you for the advice, I'm am new to the forum...obviously. I attempted part a). The problem is with the acceleration. The initial acceleration at t=0 is 3, but after that a=3-Fdrag. At the time v=65mph or MaxSpeed, a=0. Here's what I got:
if true
% code
end
m=1428.8;
a_i=3;
b=1.04;
MaxSpeed= 65 * .4474;
SpeedLimit= 50 * .4474;
dt=.5;
t=[0:dt:50];
a= NaN(length(t),1);
v= NaN(length(t),1);
x= NaN(length(t),1);
a(1)=a_i;
v(1)=0;
x(1)=0;
for k = 2:length(t)
F_drag=.5*b*(v(k-1))^2;
a(k)=a(k-1)-F_drag;
v(k)= v(k-1)+ dt*a(k-1);
x(k)=x(k-1)+ dt*v(k-1);
%a)
if v(k)==min(v(k),MaxSpeed);
a=0;
x(k)=MaxSpeed*t(k);
end

10 Comments

You are getting closer. A few changes:
First, you can delete the first three lines of code:
if true
% code
end
They are unnecessary and therefore distracting.
Next, the second line of code in the for loop is almost but not quite correct:
a(k)=a(k-1)-F_drag;
As written, it is computing one acceleration by subtracting a force from a different acceleration. Physics does not work this way. The computation must have consistent physical dimensions and units of measure. So, you need to convert the drag from a force (in Newtons) to an acceleration (in meters per second-squared). How can you do that? Hint: use Newton's Second Law.
Finally, the if block at the end of your code is not going to work. Please delete it for now, and think about how (and where) to implement it.
The :
if true
% code
end
is put in so that the code is viewed easily in the forum.
It is still not computing the acceleration correctly...
if true
% code
end
for k = 2:length(t)
F_drag=.5*b*(v(k-1))^2;
a(k)=a(k-1)-(F_drag/m);%new acceleration code
v(k)= v(k-1)+ dt*a(k);
x(k)=x(k-1)+ dt*v(k-1);
end
I'm pretty sure I have the correct equations but it will not go past k=2 in the for loop... Here's the updated code:
if true
% code
end
m=1428.8;
a_i=3;
b=1.04;
MaxSpeed= 65 * .4474;
SpeedLimit= 50 * .4474;
dt=.5;
t=[0:dt:50];
N=length(t);
a= NaN(1,length(t));
v= NaN(1,length(t));
x= NaN(1,length(t));
a(1)=a_i;
v(1)=0;
x(1)=0;
for k = 2:length(t)
F_drag=.5*b*((v(k-1))^2);
a(k)=a(k-1)-(F_drag/m);
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
x(k)=x(k-1)+ dt*(v(k)+v(k-1));
end
You are right. The problem is that a(k) should not depend on a(k-1). The driver of the vehicle is applying a constant amount of pressure on the gas pedal, resulting in a constant acceleration. So the net acceleration is the applied acceleration a_i less the acceleration resulting from the drag.
When I add the if block from before it makes the acceleration equal to zero before it is supposed to. if block:
if true
% code
end
if v(k)==min(v(k),MaxSpeed);
a(k)=0;
x(k)=MaxSpeed*t(k);
end
That's because it's not correct. Try something else.
What about:
if true
% code
end
if v(k)==min(v(k),MaxSpeed)
j=min(v(k),MaxSpeed);
p= k(find(v(k)==j));
a(p)=0;
x(k)=MaxSpeed*t(k);
end
Like Rick said, you don't need the if true lines, not even to have it appear formatted correctly here. Here, read this to find out why and to learn how to format your code. Basically you were clicking {}Code and then pasting your code in - the opposite of what you're supposed to do, which is to paste in code, then highlight it, and then and only then click {}Code.
Also, reply to Rick. Put replies as "Comments" to people whom you're answering, not as independent answers to your original question at the top.
Take a look at how Image Analyst solved this part of the problem in his answer to your earlier question. Notice that he did not use an if statement. Also notice where he put the line of code to adjust for the maximum speed.
I don't understand why the if statement to make the acceleration to equal 0 when the velocity is the MaxSpeed doesn't work.
New Code:
for k=2:N
F_drag=.5*b*((v(k-1))^2);
a(k)=a(k-1)-(F_drag/m);
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
v(k)=min(v(k),MaxSpeed);
j=k(find(v(k)== MaxSpeed));
a(j)=0;
if a(k)==0
a(k)=0;
v(k)=MaxSpeed;
end
x(k)=x(k-1)+ dt*(v(k)+v(k-1));
end

Sign in to comment.

My code is now working, but it won't display in the if statement at c) and the plots won't display either. I don't understand, does it mean the loop isn't being finished?
Code:
m=1428.8;
a_i=3;
b=1.04;
MaxSpeed= 65 * .4474;
SpeedLimit= 50 * .4474;
dt=.5;
t=[0:dt:50];
N=length(t);
a= NaN(1,length(t));
v= NaN(1,length(t));
x= NaN(1,length(t));
a(1)=a_i;
v(1)=0;
x(1)=0;
for k=2:N
F_drag=.5*b*((v(k-1))^2);
a(k)=a(k-1)-(F_drag/m);
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
x(k)=x(k-1)+ dt*(v(k)+v(k-1));
%a)
if x(k)>150
a(k)=0;
v(k)=MaxSpeed;
end
%b)
if x(k-1)>275
F_drag=.5*b*((MaxSpeed)^2);
a(k)=a(k-1)-(F_drag/m);
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
end
%c)
if x(k)>1000
if v(k)>SpeedLimit
disp('keep coasting');
disp(v(k));
else
disp('pull over');
disp(v(k));
end
end
end
subplot(1,3, 1)
plot(t, x)
xlabel('time(s)');
ylabel('distance(m)');
subplot(1,3, 2)
plot(t, v)
xlabel('time(s)');
ylabel('velocity(m/s)');
subplot(1,3, 3)
plot(t, a)
xlabel('time(s)');
ylabel('acceleration');

4 Comments

Nevermind the plots are printing...so why is won't the displays in c) display in the command window?
After you finish running the script, please type the following at the command prompt:
sum(x>1000)
What is the result? What does it mean?
BTW, the third line inside the for loop is definitely not correct:
v(k)= v(k-1)+ (a(k)+a(k-1))*dt;
If you are going to use the acceleration at both the current time step and the previous time step, you should at least divide by 2 so that you take the average rather than the sum:
v(k)= v(k-1)+ ((a(k)+a(k-1))/2)*dt;
You have made a lot of excellent progress. Don't give up. You are much closer than you were yesterday. A few minor tweaks and you can get to the right solution.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!