I need help fixing an error that pops up when running my code. "Undefined function or variable 't'.

1 次查看(过去 30 天)
Hello,
I am writing a code to help calculate the stopping distance d between two autonomous vehicles.
I have two questions.
How can I remove the error "Undefined function or variable 't'?
How can I adjust the code to solve for distance d under one for loop?
Here is my code below:
% [Car B] is traveling a distance d ahead of [Car A]. Both cars are traveling
% at the same initial velocity [50 km/h to 100 km/h] until the autonomous vehicle
% [Car B] suddenly applies the brakes. This is due to a pedestrian walking onto the
% street, causing the car to decelerate at 9 m/s^2. It takes the autonomous vehicle
% [Car A] 0.75 s to react. When [Car A] applies it's breaks, it decelerates at
% 12 m/s^2.
% Determine the minimum distance d between the cars to avoid a collision.
% velocity is v
% initial velocity is v0
% initial position is s0
% position of object after time is s
% First calculate the velocity of [Car B] as a function of time with constant
% acceleration
% initial velocity will run from 50 km/h to 100 km/h
for v0=50:1:100
% vb=v0b-ab*tb
vb=v0-9*t;
end
% Calculate the displacement of the [Car B] as a function of time with
% constant acceleration
for v0=50:1:100
% sb=s0b+v0b*tb+0.5*ab*t^2
s0b=v0*t;
sb=s0b+v0*tb+0.5*(-9)*t^2;
end
% Next calculate the velocity of [Car A] as a function of time with constant
% acceleration
for v0=50:1:100
% va=v0+aa*ta
va=v0-12*(t-0.75);
end
% Calculate the displacement of the [Car A] as a function of time with
% constant acceleration
for v0=50:1:100
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;
end
% Next calculate the time taken for the moment of closest approach by
% equating the velocity of [Car B] to [Car A]
for v0=50:1:100
syms v0 t
eqn1 = v0-9*t == v0-12*(t-0.75);
time=solve(eqn1,t);
end
% Calculate the minimum distance between the cars to avoid collision by
% equating the displacement of [Car A] and [Car B]
for v0=50:1:100
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

回答(1 个)

Stephan
Stephan 2020-10-18
Seee the code below:
% [Car B] is traveling a distance d ahead of [Car A]. Both cars are traveling
% at the same initial velocity [50 km/h to 100 km/h] until the autonomous vehicle
% [Car B] suddenly applies the brakes. This is due to a pedestrian walking onto the
% street, causing the car to decelerate at 9 m/s^2. It takes the autonomous vehicle
% [Car A] 0.75 s to react. When [Car A] applies it's breaks, it decelerates at
% 12 m/s^2.
% Determine the minimum distance d between the cars to avoid a collision.
% velocity is v
% initial velocity is v0
% initial position is s0
% position of object after time is s
% Declare the needed symbolic variables at the start
syms v0 time d t tb
% First calculate the velocity of [Car B] as a function of time with constant
% acceleration
% initial velocity will run from 50 km/h to 100 km/h
for v0=50:1:100
% vb=v0b-ab*tb
vb=v0-9*t;
end
% Calculate the displacement of the [Car B] as a function of time with
% constant acceleration
for v0=50:1:100
% sb=s0b+v0b*tb+0.5*ab*t^2
s0b=v0*t;
sb=s0b+v0*tb+0.5*(-9)*t^2;
end
% Next calculate the velocity of [Car A] as a function of time with constant
% acceleration
for v0=50:1:100
% va=v0+aa*ta
va=v0-12*(t-0.75);
end
% Calculate the displacement of the [Car A] as a function of time with
% constant acceleration
for v0=50:1:100
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;
end
% Next calculate the time taken for the moment of closest approach by
% equating the velocity of [Car B] to [Car A]
for v0=50:1:100
eqn1 = v0-9*t == v0-12*(t-0.75);
time=solve(eqn1,t);
end
% Calculate the minimum distance between the cars to avoid collision by
% equating the displacement of [Car A] and [Car B]
for v0=50:1:100
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
Maybe consider to save the results of the loop in an symbolic array to work with them later.

类别

Help CenterFile Exchange 中查找有关 Statics and Dynamics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by