필터 지우기
필터 지우기

Vector with symbolic variable

조회 수: 8 (최근 30일)
Lilted
Lilted 2024년 5월 4일
댓글: Lilted 2024년 5월 7일
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
  댓글 수: 1
Lilted
Lilted 2024년 5월 7일
Great, thank you for your answer!

댓글을 달려면 로그인하십시오.


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