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

616 次查看(过去 30 天)
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!

回答(2 个)

Manikanta Aditya
Manikanta Aditya 2024-3-5
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 个评论
Dyuman Joshi
Dyuman Joshi 2024-3-6
编辑:Dyuman Joshi 2024-3-6
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;

请先登录,再进行评论。


Aquatris
Aquatris 2024-3-5
编辑:Aquatris 2024-3-6
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 个评论
Walter Roberson
Walter Roberson 2024-3-5
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')
Aquatris
Aquatris 2024-3-6
Thank you for pointing it out @Walter Roberson. I also changed the answer to integrate your addition.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calculus 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by