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

422 visualizzazioni (ultimi 30 giorni)
Steve H.
Steve H. il 13 Gen 2023
Modificato: John D'Errico il 13 Gen 2023
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)

Risposte (4)

Walter Roberson
Walter Roberson il 13 Gen 2023
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 il 13 Gen 2023
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 il 13 Gen 2023
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 Commento
VBBV
VBBV il 13 Gen 2023
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

Accedi per commentare.


John D'Errico
John D'Errico il 13 Gen 2023
Modificato: John D'Errico il 13 Gen 2023
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.

Tag

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by