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
Jacob Savona
2015년 2월 23일
Image Analyst
2015년 2월 23일
Yes. We know. It's the 4th time you've posted it today.
Jacob Savona
2015년 2월 23일
Stephen23
2015년 2월 23일
Please stop posting duplicates:
채택된 답변
추가 답변 (3개)
Rick Rosson
2015년 2월 23일
편집: Rick Rosson
2015년 2월 23일
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
Rick Rosson
2015년 2월 23일
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...
Image Analyst
2015년 2월 24일
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
2015년 2월 24일
Jacob Savona
2015년 2월 23일
편집: Jacob Savona
2015년 2월 23일
댓글 수: 10
Rick Rosson
2015년 2월 23일
편집: Rick Rosson
2015년 2월 23일
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.
Jacob Savona
2015년 2월 23일
Jacob Savona
2015년 2월 24일
Rick Rosson
2015년 2월 24일
편집: Rick Rosson
2015년 2월 24일
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.
Jacob Savona
2015년 2월 24일
편집: Jacob Savona
2015년 2월 24일
Rick Rosson
2015년 2월 24일
That's because it's not correct. Try something else.
Jacob Savona
2015년 2월 24일
Image Analyst
2015년 2월 24일
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.
Rick Rosson
2015년 2월 24일
편집: Rick Rosson
2015년 2월 24일
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
2015년 2월 24일
Jacob Savona
2015년 2월 24일
댓글 수: 4
Jacob Savona
2015년 2월 24일
Rick Rosson
2015년 2월 24일
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?
Rick Rosson
2015년 2월 24일
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
2015년 2월 24일
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.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!