Reputation: 1675
I have a system of 12 linear equations in 13 unknowns. I would like to solve this system using Matlab and choose the variable that parameterizes the solution.
I am following an example showing how to use linsolve that would seem to be what I am looking for. In particular, I was expecting the solution to be parameterized by the unknown "i". However, this call to linsolve:
>> syms a b c d e f g h i w1 w2 w3 w4
>> linsolve([i = w1, f = w1, c = -4*w1, g+i = w2, d+f = -0.5*w2, a+c = -1.5*w2, h+i = w3, e+f = -0.5*w3, b+c = 0.5*w3, g+h+i = w4, d+e+f = w4, a+b+c = 0], [a, b, c, d, e, f, g, h, w1, w2, w3, w4, i])
is returning this error message:
Error: The expression to the left of the equals sign is not a valid target for an assignment.
where the "equals sign" indicated is for the final equation: "a+b+c = 0".
I am a total newb at Matlab. I was just hoping not to have to solve this system by hand. Advice is appreciated.
Upvotes: 0
Views: 542
Reputation: 8401
The error "expression to the left of the equals sign is not a valid target for an assignment" is because =
is reserved for assignment and can't be overloaded; therefore, symbolic equations use the equality operator ==
to express a left- and right-hand side. So all of the =
need to be ==
.
linsolve
solves the equation A*X = B
for X
given a coefficient matrix A
and a right-hand side B
.
Since your problem is linear, you could do this, but the coefficient matrix may be large due to the number of unknowns.
An alternative is to use the solve
function, which solves a list of equations for the given unknowns:
eqns = {i == w1, f == w1, c == -4*w1, g+i == w2, d+f == -0.5*w2,...
a+c == -1.5*w2, h+i == w3, e+f == -0.5*w3, b+c == 0.5*w3,...
g+h+i == w4, d+e+f == w4, a+b+c == 0};
vars = {a b c d e f g h w1 w2 w3 w4};
sol = solve(eqns{:},vars{:});
sol
is a struct whose fieldnames match the unknowns and whose entries contain the solution for that unknown:
>> sol.a
ans =
i
>> sol.b
ans =
3*i
You'll notice that I removed the i
from the variable list since it is the parameter and not something to be solved for.
Upvotes: 3