Philosophical question: How to get a vector from the output of solve? Why isn't it a vector already?
5 views (last 30 days)
I am solving an equation of several variables, and the result is a struct.
SNvec = solve(nvecEqabcd,[a1,b1,c1,d1]);
I want to write it to my latex file. So I sprintf a string that will eventually get printed to my latex file (after some processing to make it pretty):
string.steadyn=sprintf('The steady state of the nvec equation is: $rupup = %f, rdowndown = %f, rupdown = rdownup = %f, r00 = %f, rbb = %f.$',struct2array(SNvec),1-sum(struct2array(SNvec)))
But today, for some reason, struct2array doesn't work. I look that up and apparently it works for some versions of matlab but it isn't really meant to be used. So I could change my sprintf to use the struct variables, SNvec.a1, etc or I could set the output of 'solve' to four different variables, then assign them to a vector:
[Eqa1, Eqb1, Eqc1, Eqd1] = solve(nvecEqabcd,[a1,b1,c1,d1]);
Equilnvec = [Eqa1, Eqb1, Eqc1, Eqd1] ;
But in my mind, the solution to my equation is a single object, a vector, with whatever dimension it happens to have, it might be twenty dimensions next time. The fact that matlab, which was invented to deal with matrices and vectors, doesn't naturally treat this thing like a vector, suggests to me that I'm thinking about something the wrong way. Why do I have to manually turn it into a vector? Is there a smart way to turn the struct into a vector? Should I be doing something else?
Obviously, low priority here, I can get my code to run and am just curious if anybody has insight about how to think about this.
Steven Lord on 20 Mar 2023
syms x y
sol = solve(x == 5, y^2 == 4)
How exactly would you want those two solutions to the system of equations to be represented as one vector? I could potentially see storing the solutions as one matrix but I'd be worried about ambiguity with a vector representation.
As an example, if the solution vector was [5 5 -2 2] and just looking at that vector, does it represent the four solutions to a univariate problem, the two solutions to a system of two equations in two unknowns, or the unique solution to a system of four equations in four unknowns?
More Answers (1)
John D'Errico on 20 Mar 2023
Edited: John D'Errico on 20 Mar 2023
I tried it with a vector, wondering if that might make solve work more easily. And it does work.
A = magic(3);
X = sym('X',[3,1]);
B = [1;2;3];
Xsol = solve(A*X == B)
But struct2array has no problem.
syms a b c
abc = solve(A*[a;b;c] == B)
Which also seems to work. So as long as a unique solution is returned, it works nicely. The vector was not necessary. The problem for you was surely that solve is returning multiple solutions.
In the case where there are multiple solutions, maybe you need to use struct2cell.
syms x y
xy = solve(x^2 + y^2==2,x+y==1)
xycell = struct2cell(xy)
And now with some more manipulations, convert those results into an array as you might desire. Perhaps like this:
Hope this helps.