Got this ''Empty sym: 0-by-1''
208 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(4 个)
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)
0 个评论
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.
0 个评论
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)
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)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!