Info
この質問は閉じられています。 編集または回答するには再度開いてください。
How can I fix my error and plot the data from the for loop?
2 ビュー (過去 30 日間)
古いコメントを表示
The code below is used to calculate the stopping distance between two vehicles. How can I plot the data points from the for loop? Currently, I get an error that says (Undefined function or variable 't').
I would like to plot the v0 values (50-100) on the x-axis, and the distance as the y-axis.
clc
clear all
for v0=50:1:100
% vb=v0b-ab*tb
vb=v0-9*t;
% Calculate the displacement of the [Car B] as a function of time with
% constant acceleration
% sb=s0b+v0b*tb+0.5*ab*t^2
s0b=v0*t;
sb=s0b+v0*tb+0.5*(-9)*t^2;
% Next calculate the velocity of [Car A] as a function of time with constant
% acceleration
% va=v0+aa*ta
va=v0-12*(t-0.75);
% Calculate the displacement of the [Car A] as a function of time with
% constant acceleration
s0a=v0*t;
% sa=s0a+v0*ta+0.5*aa*ta^2
sa=s0a*(0.75)+v0*(t-0.75)+0.5*(-12)*(t-0.75)^2;
% Next calculate the time taken for the moment of closest approach by
% equating the velocity of [Car B] to [Car A]
syms v0 t
eqn1 = v0-9*t == v0-12*(t-0.75);
time=solve(eqn1,t);
% Calculate the minimum distance between the cars to avoid collision by
% equating the displacement of [Car A] and [Car B]
syms v0 time d
eqn2 = d+v0*time-0.5*(9)*time^2==(v0*0.75)+(v0*time)-(v0*0.75)+0.5*(-12)*(time-0.75)^2
distance=solve(eqn2,d)
end
3 件のコメント
Adam Danz
2020 年 10 月 18 日
編集済み: Adam Danz
2020 年 10 月 18 日
Toward the top of your code you have this line,
vb=v0-9*t;
At that line, t is not defined.
If it's supposed to be defined, then you need to figure out where that value is supposed to come from. Maybe you're mssing some code or mabe you're supposed to load some data.
If 't' isn't supposed to be there, remove it.
Instead of thinking about how to remove the error, think about what your code is supposed to be doing and why it's not doing that. Note that 't' appears in many line before it's defined so the problem is bigger than that 1 line.
For example, you could add t=1 to the top and that would get rid of the error but it probably wouldn't fix the problem. You've got to understand the code, line by line, to understand what's wrong with it.
回答 (0 件)
この質問は閉じられています。
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!