Conversion from logical to sym is not possible

7 ビュー (過去 30 日間)
Ryan
Ryan 2023 年 5 月 2 日
編集済み: Walter Roberson 2023 年 5 月 3 日
clc; clear;
k = 10000 ; % N/m
m = 331 ; % kg
v_3 = 45 * 1609.34 * (1/3600) ; % mph to m/s
t_reach = 25/v_3 ; % s
% Using Hooke's law because the distance and stiffness are known, F = kx
F_step = k*0.05 ;
syms t
F_impulse = F_step*heaviside(t-t_reach) ;
% Make plot
% Say plot for 10 seconds like 3.2
figure(21)
fplot(F_impulse,[0,10])
title('Force Profile')
xlabel('Time (s)')
ylabel('Force (N)')
xlim([0,10])
ylim([0,550])
c_3 = 300 ;
m = 331 ;
k = 10000 ;
t_reach = t_reach ;
v_3_simple = 45 .* 1609.34 .* (1./3600) ;
% define the time interval and the number of integration steps
n = 100000;
t_3 = zeros(n,1);
tf = 10;
t_3(1) = 0;
delt = (tf-t_3(1))/n;
% initializing variables
x = zeros(n,1);
v = zeros(n,1);
% assign initial conditions
x(1) = 0 ;
v(1) = v_3_simple ;
% start the integration directly from the second step (with i=2)
for i=2:1:n
% update the velocity and acceleration for x at step i
if t(i-1) >= t_reach
F = 500;
else
F = 0 ;
end
dx_3 = v(i-1) ;
dv_3 = -(c_3./m).*dx_3 - ((k./m).*x) ;
% integrate x and v
x(i) = x(i-1) + dx_3.*delt ;
v(i) = v(i-1) + dv_3.*delt ;
% integrate time
t_3(i) = t_3(i-1) + delt ;
end % end of each integration step
Conversion to logical from sym is not possible.
Please advise on the error within the for loop.

回答 (1 件)

Walter Roberson
Walter Roberson 2023 年 5 月 3 日
編集済み: Walter Roberson 2023 年 5 月 3 日
syms t
That creates t as a 1 x 1 variable at the MATLAB level that contains information that links the MATLAB-level t to a variable named t that lives inside the Symbolic Engine of the Symbolic Toolbox
for i=2:1:n
% update the velocity and acceleration for x at step i
if t(i-1) >= t_reach
At that point, t at the MATLAB level is still the 1 x 1 scalar reference to the symbolic t . i is 2, so i-1 is 1, and so you are indexing the 1 x 1 symbolic t at index 1, getting back a 1 x 1 reference to the scalar symbolic t variable in the symbolic engine.
So t(i-1) at the MATLAB level resolves to the symbolic variable t that lives in the Symbolic Engine. Comparing that symbolic variable to t_reach gives a symbolic expression as a result, since symbolic engine t has no particular value, so you cannot say whether t > t_reach
Now you have if trying to figure out whether t >= t_reach is true or not. But that is not something that can be decided since t does not have a particular value. So the if fails with the error message you get.
Note that you never assign to t(i) so on the second iteration when t = 3, t(i-1) is not going to exist.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by