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

31 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-11-20
"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).

请先登录,再进行评论。

回答(1 个)

Sam Chak
Sam Chak 2023-11-20
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 个评论
Sam Chak
Sam Chak 2023-11-21
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by