Clear Filters
Clear Filters

What is the function to use to find the intersection of two graphs?

6 views (last 30 days)
I tried browsing for the right function and came across interx and intersection, but somehow they are not available for use in my MATLAB program. Can someone help me with the issue?
f = @(x,y) y-2 - x.^2 .* (cos(x));
fimplicit(f), grid on
hold on
g = @(x,y) y-x.^4-x-1;
fimplicit(g), grid on
hold off
xlabel('x'), ylabel('y')
legend ('y = 2 + cos(x)','y = x^4 + x + 1','location','northeast')
  4 Comments
Lê
on 19 Nov 2023
@Stephen23 Where do I place the INTERX file if I may ask? Also @Sam Chak I figured it out on my own.
Stephen23
Stephen23 on 20 Nov 2023
"Where do I place the INTERX file if I may ask?"
It is exactly like your own MATLAB code files: either in the current directory or somewhere on the MATLAB search path (of course avoiding the installation folders of any application, including MATLAB).

Sign in to comment.

Answers (1)

Sam Chak
Sam Chak on 20 Nov 2023
Hi @Lê
It is possible to find intersections using the fsolve() command.
f = @(x,y) y - 2 - (x.^2).*cos(x);
fimplicit(f), grid on
hold on
g = @(x,y) y - x.^4 - x - 1;
fimplicit(g), grid on
hold off
xlabel('x'), ylabel('y')
legend('y = 2 + x^{2} cos(x)','y = x^4 + x + 1','location','northeast')
%% Finding intersection points
x0 = [-1, 1]; % initial guesses
for j = 1:2
fun = @(x) x^4 + x + 1 - 2 - (x.^2).*cos(x); % f(x) = g(x)
x = fsolve(fun, x0(j))
y = x.^4 + x + 1
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = -1.2879
y = 2.4630
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 0.8843
y = 2.4956
  3 Comments
Sam Chak
Sam Chak on 21 Nov 2023
Hi @Lê
Maybe when calculating the volume, it should be placed outside the loop.
%% Plotting the graphs
f = @(x,y) y-2 - x.^2 .* (cos(x));
fimplicit(f), grid on
hold on
g = @(x,y) y-x.^4-x-1;
fimplicit(g), grid on
hold off
xlabel('x'), ylabel('y')
legend ('y = 2 + cos(x)','y = x^4 + x + 1','location','northeast')
%% Finding intersection points
x0 = [-1, 1]; % initial guesses
for j = 1:2
fun1 = @(x) x^4 + x + 1 - 2 - (x.^2).*cos(x); % f(x) = g(x)
x(j) = fsolve(fun1, x0(j))
end
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = -1.2879
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
x = 1×2
-1.2879 0.8843
%% Calculating the volume
fun2 = @(x) (2 + (x.^2).*cos(x)).^2 - (x.^4 + x + 1).^2;
V = pi*integral (fun2, x(1), x(2))
V = 23.7802

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!