How to find solution to system of 2 circle equations in Matlab?

13 visualizaciones (últimos 30 días)
Aman
Aman el 7 de Jun. de 2024
Editada: John D'Errico el 13 de Jun. de 2024
x^2+y^2=25;
x^2+y^2=36;
%%how to find values of x and y??
%Please help someone
  4 comentarios
Kalhara
Kalhara el 12 de Jun. de 2024
Hello aman, Hope this will help you to clear this out. Read carefully.
First look at the 2 equations you are trying to solve. These are 2 circles with the same center and different radie. Can you imagine a point where these two circles intersect (which you ask to solve means that values for x and y which satisfies both the equations.) Probable there are no points we can find which intersect two circles with different radie with same center. This is a simple math logic.
% Define symbolic variables
syms x y
% Define the circle equations
eq1 = x^2 + y^2 == 25;
eq2 = x^2 + y^2 == 36;
% Solve the system of equations
solutions = solve([eq1, eq2], [x, y]);
% Convert solutions to double (if any exist)
xSol = double(solutions.x);
ySol = double(solutions.y);
% Display the solutions
if isempty(xSol)
disp('No intersection points.')
else
disp('Intersection Points:')
for i = 1:length(xSol)
fprintf('(%f, %f)\n', xSol(i), ySol(i));
end
end
when you run this code, it will output " No intersection point"
John D'Errico
John D'Errico el 13 de Jun. de 2024
Editada: John D'Errico el 13 de Jun. de 2024
@Kalhara NO. You are yourself mistaken.
While the problem you posed to solve failed to find a solution, that was not so much that solve could not find a solution. In my answer, I show a case where two circles do not intersect, yet solve DOES find a solution! The problem most of the time is the solution has imaginary components when no real solution can be found.
In the example problem given by @Aman, yes, this is a case where no solution can possibly exist, for real or complex values of x and y. But that is only because the circles are perfectly concentric. But even if we change the problem in a very subtle way:
syms x y
% Define the circle equations
eq1 = (x-0.1)^2 + (y+0.2)^2 == 25;
eq2 = x^2 + y^2 == 36;
fimplicit(eq1)
hold on
fimplicit(eq2)
xy = solve([eq1,eq2],[x,y])
xy = struct with fields:
x: [2x1 sym] y: [2x1 sym]
xy.x
ans = 
xy.y
ans = 
Do you see that now, solve did find a solution, even though one circle was entirely and completely contained inside the other? There can be no REAL solutions, but there is a complex solution to the system of equations. And those are indeed exact solutions to the equations posed.
simplify(subs(eq1,xy))
ans = 
simplify(subs(eq2,xy))
ans = 
So both sets of solutions for x and y do mathematically solve the problem.
Most of the time, when no solution exists, solve will still return a solution. The trick is to recognize when the solution returned has real vaues. Could I have made this tiny change instead?
syms x y real
% Define the circle equations
eq1 = (x-0.1)^2 + (y+0.2)^2 == 25;
eq2 = x^2 + y^2 == 36;
xy = solve([eq1,eq2],[x,y])
xy = struct with fields:
x: [0x1 sym] y: [0x1 sym]
Do you see the difference? Now when solve fails to find a solution, it is because there are no REAL solutions.

Iniciar sesión para comentar.

Respuestas (2)

Torsten
Torsten el 7 de Jun. de 2024
Movida: Torsten el 7 de Jun. de 2024
x^2+y^2=25;
is a circle around 0 with radius 5,
x^2+y^2=36;
is a circle around 0 with radius 6.
Can you think of a point that has simultaneously distance 5 and distance 6 from the origin ?
For an analytical solution, see

John D'Errico
John D'Errico el 8 de Jun. de 2024
Torsten has answered your question perfectly well, I think. No solution exists for that problem as posed. But suppose you had a slightly different problem? How would you go about it? First, an example where a pair of solutions exists.
syms x y
C1 = (x - 2)^2 + (y - 3)^2 == 12;
C2 = (x + 1)^2 + (y + 2)^2 == 8;
fimplicit(C1)
hold on
fimplicit(C2)
axis equal
hold off
We can see two solutions where the circles cross.
xy = solve([C1,C2],[x,y])
xy = struct with fields:
x: [2x1 sym] y: [2x1 sym]
[xy.x,xy.y]
ans = 
double([xy.x,xy.y])
ans = 2x2
1.3317 -0.3990 -0.6847 0.8108
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Each row corresponds to a solution. If no solution exists, then the solutions will be complex.
D1 = (x - 2)^2 + (y - 3)^2 == 5;
D2 = (x + 1)^2 + (y + 2)^2 == 8;
fimplicit(D1)
hold on
fimplicit(D2)
axis equal
hold off
xyD = solve([D1,D2],[x,y])
xyD = struct with fields:
x: [2x1 sym] y: [2x1 sym]
[xyD.x,xyD.y]
ans = 
Do you see what happens? Now the solutions are complex. No real solutions exist where the circles intersect.
Could you have done this using pencil and paper? Well, in fact yes. Hint: subtract the two equations, thus C1-C2, and simplify. The result will be linear. You can use that to eliminate one of the variables from one of the circles.

Categorías

Más información sobre Mathematics and Optimization en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by