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
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
2023-11-20
Hi @Lê
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
3 个评论
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
%% Calculating the volume
fun2 = @(x) (2 + (x.^2).*cos(x)).^2 - (x.^4 + x + 1).^2;
V = pi*integral (fun2, x(1), x(2))
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!