How can I use the solve command in Matlab to find the intersection of two equations?

4 views (last 30 days)
eqn1 = x^2 + y^2 ==9;
eqn2 = 2*x + 3*y == 4;
S = solve(eqn2==eqn1)

Answers (1)

DGM
DGM on 27 Nov 2021
Something like this:
% solve system of equations
syms x y
eqn1 = x^2 + y^2 == 9;
eqn2 = 2*x + 3*y == 4;
S = solve([eqn2 eqn1],[x y]);
sx = double(S.x)
sx = 2×1
2.9346 -1.7038
sy = double(S.y)
sy = 2×1
-0.6231 2.4692
% just plot things numerically and see if the solutions make sense
N = 50;
x = linspace(-4,4,N);
y = x.';
surf(x,y,sqrt(x.^2+y.^2),'facealpha',0.8); hold on % a circle
surf(x,y,3*ones(N,N),'facealpha',0.8) % with radius 3
plot3(x,(4-2*x)/3,3*ones(1,N)) % intersects with a diagonal line
plot3(sx,sy,[3 3],'o','linewidth',2) % mark calculated solution points
colormap(parula)
shading flat

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!