solve the point of intersections. save the array of solutions as roots. in matlab

To solve for the point of intersection of two functions or equations in Matlab, we can use the solve() function to find the roots of the system of equations. The roots() function is then used to extract the roots from the solution.

Here is an example code snippet that solves for the point of intersection of two functions:

main.m
syms x y
eq1 = x^2 + y^2 == 25;  % equation of a circle with radius 5
eq2 = y == x + 2;        % equation of a line with slope 1 and y-intercept 2

% solve for the roots (x,y) of the system of equations
sol = solve(eq1, eq2, 'Real', true);

% extract the roots from the solution
roots = [double(sol.x), double(sol.y)]

% plot the circle and line
t = linspace(-10, 10);
xc = 5*cos(t);
yc = 5*sin(t);
plot(xc,yc)
hold on
plot(t,t+2)
axis equal
grid on
450 chars
20 lines

In this example, we define two equations eq1 and eq2, representing a circle and a line. We then use the solve() function to find the roots (intersections) of these equations. The 'Real', true option tells Matlab to only consider real solutions.

Finally, we extract the roots from the solution using double() function, and store them in the roots array. The plot() function is used to visualize the circle and line, and the intersections are marked on the plot.

gistlibby LogSnag