Plotting functions of more than one variable, f(x,y)

Hi there i am trying to plot two functions on matlab which consist of two variables, x and y. I have written a Newton-Raphson m-file to find these points but i would like to have the two functions plotted so that i can make better initial estimates for the Newton-Raphson code to work with initially so that it will converge accurately.
How would I plot the following functions in order to have a visual of their points of intersection?
x^3 - y^2 = 1
0.5 + cos(x)tanh(y) = 0
Any help would be greatly appreciated!

Answers (2)

Hey Conor,
I checked and tried to plot using the 'fimplicit' function for the equations you mentioned.
Here is a sample code to plot your functions:
% Define the functions
f1 = @(x, y) x.^3 - y.^2 - 1;
f2 = @(x, y) 0.5 + cos(x).*tanh(y);
% Create a new figure
figure;
% Plot the first function
fimplicit(f1, [-2, 2, -2, 2], 'r');
hold on;
% Plot the second function
fimplicit(f2, [-2, 2, -2, 2], 'b');
% Add title and labels
title('Intersection of Two Functions');
xlabel('x');
ylabel('y');
% Add a legend
legend('x^3 - y^2 = 1', '0.5 + cos(x)tanh(y) = 0', 'Location', 'best');
% Hold off the figure
hold off;
This code will plot the two functions in the range [-2, 2] for both x and y. The points of intersection between the red and blue curves represent the solutions to the system of equations.
Please adjust the range [-2, 2, -2, 2] as per your requirements to get a better view of the intersection points. This should give you a good starting point for your Newton-Raphson method.
Hope it helps!

4 Comments

This was really helpful, thank you so much. I just have one more question, is there a way I can now mark in these points of intersection on the plot or calculate them to be displayed after plotting so that I can have the answers as well as the visual plot? Thanks again
To find and mark the points of intersection on your plot, you can use MATLAB's numerical solvers like 'fsolve' to find the intersections based on your initial guesses, which you can fine-tune by visually inspecting the plot.
Here's how you can integrate this into your code:
% Define the functions
f1 = @(x, y) x.^3 - y.^2 - 1;
f2 = @(x, y) 0.5 + cos(x).*tanh(y);
% Plot the functions
figure;
fimplicit(f1, [-2, 2, -2, 2], 'r');
hold on;
fimplicit(f2, [-2, 2, -2, 2], 'b');
title('Intersection of Two Functions');
xlabel('x');
ylabel('y');
legend('x^3 - y^2 = 1', '0.5 + cos(x)tanh(y) = 0', 'Location', 'best');
% Define a function for the system of equations
systemOfEquations = @(v) [f1(v(1), v(2)); f2(v(1), v(2))];
% Initial guess for the intersection points
initialGuess = [0, 1]; % This is just an example. Adjust based on the plot.
% Use fsolve to find the intersection point
options = optimoptions('fsolve', 'Display', 'none'); % Suppress fsolve output
intersectionPoint = fsolve(systemOfEquations, initialGuess, options);
% Mark the intersection point on the plot
plot(intersectionPoint(1), intersectionPoint(2), 'ko', 'MarkerFaceColor', 'g');
% Hold off the figure
hold off;
% Display the intersection point in the Command Window
disp('Intersection Point:');
Intersection Point:
disp(intersectionPoint);
0.0000 -0.1614
  • 'fsolve' finds the roots of a system of nonlinear equations, which in this case are your two functions.
  • You need to provide an initial guess. By inspecting the plot you've generated, you can make a reasonable guess where the functions intersect.
  • The options for 'fsolve' are set to suppress output messages for cleaner execution. You can remove or adjust these as needed.
  • This example finds one intersection point. If your plot suggests there are multiple intersections, you'll need to run 'fsolve' with different initial guesses to find the other points.
Hope this clarifies!
As can be observed from the plot, fsolve() does not find an intersection point of the curves in subject. Because the initial guess provided is not good.
As mentioned above, fsolve only provides a single output at a time as per the provided input. If you want to get all the solutions, you can solve the equations symbolically. Or utilize FEX ubsmissions on obtaining curve intersections.
Or compare values directly, as done in the other answer.
% Define the functions
f1 = @(x, y) x.^3 - y.^2 - 1;
f2 = @(x, y) 0.5 + cos(x).*tanh(y);
% Plot the functions
figure;
fimplicit(f1, [-20, 20], 'r');
hold on;
fimplicit(f2, [-20, 20], 'b');
title('Intersection of Two Functions');
xlabel('x');
ylabel('y');
legend('x^3 - y^2 = 1', '0.5 + cos(x)tanh(y) = 0', 'Location', 'best');
% Define a function for the system of equations
systemOfEquations = @(v) [f1(v(1), v(2)); f2(v(1), v(2))];
%Original initial guess
out = fsolve(systemOfEquations, [0 1])
No solution found. fsolve stopped because the problem appears regular as measured by the gradient, but the vector of function values is not near zero as measured by the value of the function tolerance.
out = 1×2
0.0000 -0.1614
%Updated initial guess
initialGuess = [1, 1];
intersectionPoint = fsolve(systemOfEquations, initialGuess)
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.
intersectionPoint = 1×2
2.0981 2.8699
% Mark the intersection point on the plot
plot(intersectionPoint(1), intersectionPoint(2), 'ko', 'MarkerFaceColor', 'g');
% Hold off the figure
hold off;
Thanks for all your help guys!

Sign in to comment.

A simple code:
y = -20:0.001:20; % define y
y(y==0) = []; % remove y == 0 for to prevent division by zero in x2 equation
x1 = (1+y.^2).^(1/3); % solve for x in equation x^3 - y^2 = 1
x2 = acos(-0.5./tanh(y));% solve for x in equation 0.5 + cos(x)tanh(y) = 0
all(abs(x1.^3-y.^2-1)<1e-9) % check if x1 y is solution to x^3 - y^2 = 1
ans = logical
1
all(abs(0.5+cos(x2).*tanh(y))<1e-9) % check if x2 y is solution to 0.5 + cos(x)tanh(y) = 0
ans = logical
1
x1(imag(x1)~=0) = nan; % remove solutions with imaginary numbers
x2(imag(x2)~=0) = nan; % remove solutions with imaginary numbers
plot(x1,y,'b.',x2,y,'r.')
xlabel('x'),ylabel('y')

2 Comments

y = -20:0.001:20; % define y
y(y==0) = []; % remove y == 0 for to prevent division by zero in x2 equation
x1 = (1+y.^2).^(1/3); % solve for x in equation x^3 - y^2 = 1
x2 = acos(-0.5./tanh(y));% solve for x in equation 0.5 + cos(x)tanh(y) = 0
all(abs(x1.^3-y.^2-1)<1e-9) % check if x1 y is solution to x^3 - y^2 = 1
ans = logical
1
all(abs(0.5+cos(x2).*tanh(y))<1e-9) % check if x2 y is solution to 0.5 + cos(x)tanh(y) = 0
ans = logical
1
x1(imag(x1)~=0) = nan;
x2(imag(x2)~=0) = nan;
plot(x1,y,'b.',x2,y,'r.')
xlabel('x'),ylabel('y')
Thank you for pointing it out @Walter Roberson. I also changed the answer to integrate your addition.

Sign in to comment.

Asked:

on 5 Mar 2024

Commented:

on 6 Mar 2024

Community Treasure Hunt

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

Start Hunting!