Got this ''Empty sym: 0-by-1''

413 ビュー (過去 30 日間)
Steve H.
Steve H. 2023 年 1 月 13 日
編集済み: John D'Errico 2023 年 1 月 13 日
This is a simplified version of a code I'm doing for my school assignment. I don't know why the answer always come out as Empty sym: 0-by-1. I already tried many ways.
Thank you in advance.
syms x;
q = 1:3;
w = 2;
eqn = (3*x) + q + w == 0;
sol = solve(eqn);
vpa(sol)

回答 (4 件)

Walter Roberson
Walter Roberson 2023 年 1 月 13 日
solve() is for solving simultaneous equations. for example asking to solve [x+y==9,x-y==4] does not output a set of solutions for x+y==9 and a set of solutions for x-y==4: it finds the solution for both equations considered together.
You are passing a vector of three equations to solve() and asking for the set of values such that all of the equations are satisfied with the same value. There is no such value.
If you want to solve each equation separately you need multiple calls to solve. For example arrayfun(@solve, eqn)

Luca Ferro
Luca Ferro 2023 年 1 月 13 日
solve returns 'Empty sym: 0-by-1' when there is no explicit solution to the equation.
The equation has no solution because of the assignment q=1:3;
when you use an array like that in the line where the equation is defined it defines it as:
[3*x + 3 == 0, 3*x + 4 == 0, 3*x + 5 == 0]
Clearly the above cited set of equations has no solution.
It's not clear what you want to achieve with q=1:3; , so i can't give you an alternative solution to the code you have written.
If you could specify what you goal was we can figure something out, otherwise now that you know the issue you can solve it by yourself.

VBBV
VBBV 2023 年 1 月 13 日
syms x;
q = 1:3;
w = 2;
for k = 1:length(q)
eqn = (3*x) + q(k) + w == 0;
sol = solve(eqn,x);
S(k) = double(vpa(sol,2))
end;
S
  1 件のコメント
VBBV
VBBV 2023 年 1 月 13 日
As Walter mentioned you are trying to solve set of 3 equations by passing vector of inputs. Try calling solve on a for loop, as above

サインインしてコメントする。


John D'Errico
John D'Errico 2023 年 1 月 13 日
編集済み: John D'Errico 2023 年 1 月 13 日
I'll suggest you did things in the wrong order.
syms x
syms q % leave q as an unknown parameter for the moment.
w = 2;
eqn = (3*x) + q + w == 0;
xsol = solve(eqn,x)
xsol = 
Here, we see that x is a function of the unknown parameter q. Only NOW at the very end do we substitute the possible values of q.
subs(xsol,q,1:3)
ans = 
Your problem was you defined q in advance as a vector, and that confused MATLAB. It tried to then define 3 equations, but with only the one unknown x, and that must fail.

カテゴリ

Help Center および File ExchangeAssumptions についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by