Solve 2 quadratic equations

I have 2 quadratic equations with 2 known and 2 unknown. Ho do I write a script file to solve this equation. The equations are: (x1-xc)^2+(y1-yc)^2=R^2 (x2-xc)^2+(y2-yc)^2=R^2 I know the values for x1,x2,y1,y2 and R, these can be input manually in the script. I need to find xc and yc. How do I write a code to solve it in Matlab. Any help would be greatly appreciated.

 Accepted Answer

syms xc;
syms yc;
[x y] = solve('(x1-xc)^2+(y1-yc)^2=R^2 ,(x2-xc)^2+(y2-yc)^2=R^2')
put the values of R ,x1 and y1 in above equation you will get your answer hopefully

2 Comments

Hello, Thank you for the reply. I tried this and got the following error message ??? Undefined function or method 'syms' for input arguments of type 'char'.
Error in ==> myquadratic at 1 syms xc;
which version you are using ?? try this:
syms x y integer
[x y] = solve('(x1-xc)^2+(y1-yc)^2=R^2 ,(x2-xc)^2+(y2-yc)^2=R^2')

Sign in to comment.

More Answers (1)

Undefined function or method 'syms' for input arguments of type 'char'.
indicates that either you do not have the Symbolic Toolbox licensed or you do not have it installed. If you have the Student Version then it includes a license for the Symbolic Toolbox, and you should install it and try again with
make your assignments to x1, y1, x2, y2 and R above this point
syms xc yc
[XC, YC] = solve((x1-xc)^2+(y1-yc)^2 - R^2, (x2-xc)^2+(y2-yc)^2 - R^2, xc, yc);
If you do not have the symbolic toolbox available then there are method involving the optimization toolbox and fsolve() . And if you do not have that toolbox then you can do things like
initialvals = rand(1,2);
XCYC = fminsearch( @(xyc) (x1-xyc(1))^2+(y1-xyc(2))^2 - R^2 + (x2-xyc(1))^2+(y2-xyc(2))^2 - R^2, initialvals);
Be careful: there are two solutions.
In code, with the two solutions being xc1, yc1 and xc2, yc2:
t1 = -y1 + y2;
t2 = (x1 - x2);
t3 = (t2 ^ 2);
t4 = (x1 ^ 2);
t6 = 2 * x2 * x1;
t7 = x2 ^ 2;
t8 = (y1 ^ 2);
t9 = (y1 * y2);
t10 = 2 * t9;
t11 = (y2 ^ 2);
t14 = (R ^ 2);
t18 = sqrt((t3 * (t4 - t6 + t7 + t8 - t10 + t11) * (4 * t14 - t4 + t6 - t7 - t8 + t10 - t11)));
t22 = -t1;
t23 = (t22 ^ 2);
t24 = t4 - t6 + t7 + t23;
t25 = t2 * (x1 + x2) * t24;
t27 = 1 / t2;
t29 = 1 / t24;
t31 = t8 * y1;
t32 = t8 * y2;
t36 = (-x2 + x1 - y2) * (-x2 + x1 + y2) * y1;
t37 = t11 * y2;
t38 = t3 * y2;
t45 = 1 / (2 * t8 - 4 * t9 + 2 * t11 + 2 * t3);
xc1 = (t1 * t18 + t25) * t27 * t29 / 2;
yc1 = (t31 - t32 + t36 + t37 + t38 + t18) * t45;
xc2 = (t18 * t22 + t25) * t27 * t29 / 2;
yc2 = (t31 - t32 + t36 + t37 + t38 - t18) * t45;

2 Comments

Hello Thank you for your suggestion. I do not have syms function in my version of Matlab. I tried the 2 solution code, when I run this I do not get an answer nor an error message. I have attached the code. Could you please let me know what's wrong in the code.
The first answer would be x=xc1, y=yc1

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!