# How to solve two variable equations using matlab?

24 views (last 30 days)
Harith Karunaratne on 2 Jul 2015
Answered: Walter Roberson on 2 Jul 2015
I have three equations need to solve using Matlab. i tried this code
solve( rs1==a01+a11*xp+a21*yp+a31*xp*yp, rs2==a02+a12*xp+a22*yp+a32*xp*yp,rs3==a03+a13*xp+a23*yp+a33*xp*yp );
but i cant get xp and xy values and this error "Error using mupadengine/feval (line 157) MuPAD error: Error: Invalid variable to solve for. [solve]" comes. what is the way to solve this? and im using 2014a
Torsten on 2 Jul 2015
No. You can easily calculate xp and yp from the first two equations, but this solution will in general not satisfy the third equation.
Best wishes
Torsten.

B.k Sumedha on 2 Jul 2015
It looks like MATLAB versions compatibility issue:

Walter Roberson on 2 Jul 2015
No, three equations in two variables makes it harder to solve, not easier.
We need to know which of those names you give are "syms" and which are numeric, and we need to know whether any of the numeric variables are non-integral.
The first two equations together define xp and yp in terms of the roots of a quadratic equation. There are therefore exactly two closed-form solutions, to the limits of round-off.
The solution pairs can be substituted into the third equation, producing a moderately long result that includes sqrt() in more than one place. Chances that an evaluation given floating point numbers would happen to exactly equal rs3 are small.
Use
syms A01 A02 A11 A12 A21 A22 A31 A32 Rs1 Rs2 xp yp
sols = solve(Rs1 == A31*xp*yp+A11*xp+A21*yp+A01, Rs2 = A32*xp*yp+A12*xp+A22*yp+A02], [xp, yp]);
xvals = double(subs(sols.X, {A01 A02 A11 A12 A21 A22 A31 A32 Rs1 Rs2}, {a01 a02 a11 a12 a21 a22 a31 a32 rs1 rs2}));
yvals = double(subs(sols.Y, {A01 A02 A11 A12 A21 A22 A31 A32 Rs1 Rs2}, {a01 a02 a11 a12 a21 a22 a31 a32 rs1 rs2}));
now use xvals(1) with yvals(1), or xvals(2) with yvals(2). And if neither of them are what you were expecting then your expectations would be wrong for those formulae.