Got this ''Empty sym: 0-by-1''
418 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (4)
Walter Roberson
am 13 Jan. 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)
0 Kommentare
Luca Ferro
am 13 Jan. 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.
0 Kommentare
VBBV
am 13 Jan. 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 Kommentar
VBBV
am 13 Jan. 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
John D'Errico
am 13 Jan. 2023
Bearbeitet: John D'Errico
am 13 Jan. 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)
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 Kommentare
Siehe auch
Kategorien
Mehr zu Assumptions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!