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

4 Ansichten (letzte 30 Tage)
Jacob Savona am 23 Feb. 2015
Beantwortet: Jacob Savona am 24 Feb. 2015
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 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Jacob Savona am 23 Feb. 2015
Alright. thanks for the help.
Stephen23 am 23 Feb. 2015

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jacob Savona am 24 Feb. 2015
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');
0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

Rick Rosson am 23 Feb. 2015
Bearbeitet: Rick Rosson am 23 Feb. 2015
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 Kommentare1 älteren Kommentar anzeigen1 älteren Kommentar ausblenden
Image Analyst am 24 Feb. 2015
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
Rick Rosson am 24 Feb. 2015

Melden Sie sich an, um zu kommentieren.

Jacob Savona am 23 Feb. 2015
Bearbeitet: Jacob Savona am 23 Feb. 2015
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 Kommentare8 ältere Kommentare anzeigen8 ältere Kommentare ausblenden
Rick Rosson am 24 Feb. 2015
Bearbeitet: Rick Rosson am 24 Feb. 2015
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.
Jacob Savona am 24 Feb. 2015
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

Melden Sie sich an, um zu kommentieren.

Jacob Savona am 24 Feb. 2015
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 Kommentare2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
Rick Rosson am 24 Feb. 2015
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;
Rick Rosson am 24 Feb. 2015
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by