Vector with symbolic variable

4 次查看(过去 30 天)
I have created a 8x1 sym (vector?) called psi and a 1x1000 double (vector?) called time. When I try to plug in a value from the double into the psi I use the syntax:
psi_eval = psi(time(2));
since this is the way I would have done it in python, however it does not work. I get an error saying:
Array indices must be positive integers or logical values.
Error in sym/subsref (line 909)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in BachelorThesis (line 65)
psi_eval = psi(time(2));
The array index I have given is a positive value so I don't understand the error I get. What am I doing wrong?

采纳的回答

Steven Lord
Steven Lord 2024-5-4
If you have a symbolic variable that contains a function of another symbolic variable and you want to substitute values for that "inner" symbolic variable, use the subs function.
syms t
y = t.^2;
subs(y, t, 1:5)
ans = 
If it's a symbolic function then you can use parentheses like you tried.
syms x(t)
x(t) = t.^3;
x(1:5)
ans = 

更多回答(2 个)

Walter Roberson
Walter Roberson 2024-5-4
编辑:Walter Roberson 2024-5-4
Your time vector contains at least one value that has a fraction or is not a positive value.
For example, your time vector might start from 0, or might increment by 1/10th of a second.
mask = find(time ~= floor(time) | time <= 0);
if isempty(mask)
fprintf('times check out okay!\n');
else
fprintf('invalid times!\n');
disp(compose("index %03d, value %g", mask(:), time(mask)));
end

Torsten
Torsten 2024-5-4
移动:Torsten 2024-5-4
Works for me.
psi = sym('psi',[8 1]);
t = 1:1000;
psi(t(5))
ans = 
  2 个评论
Lilted
Lilted 2024-5-4
I don't find that helpful. I have 8x1 sym object, the elements are something that depends on t. When I write:
psi_eval = psi(time(2));
I want the code to plug in the second value in the list time into my sym object and evaluate what it will be.
Paul
Paul 2024-5-4
Why not show a simple, but complete, example that makes shows the full code, in partcular the defintion of psi, and clearly shows what you're trying to do? As it stands, we can only guess. Maybe something like this:
syms t
psi = [t;2*t];
time = rand(1,2000);
subs(psi,t,time(2))
ans = 
vpa(ans)
ans = 
[time(2);2*time(2)]
ans = 2x1
0.2286 0.4573
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by