I have some linear equations in a cell array like this ( The number of equations vary each time ) :
equs = { '2*X1+X2+6', '3*X2-X1' }
How can I solve these equation with Matlab? I can get my answer simply by this function :
ans = solve(equs(1), equs(2));
But, as the number of equations differ each time, I want this to be done automatically.
I am assuming that you want the equations to be equal to 0, and that no equals sign appears in the equations.
Parse the expressions to find the coefficients - put into a matrix (A). I am using here a near trick that assumes that the variables are always x1, x2, etc. Also you must write the * sign for multiplications. The FindCoeffs function finds the coefficients by assigning ones and zeros to the variables. Then, you can solve the equations using linsolve.