How to plot only the real solutions of an implicit function ?

3 views (last 30 days)
Hi,
I'm having some problems trying to plot the following implicit equation f1 = @(x,y) sqrt(L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2) with fimplicit(f1).
If I use the following code, in the plot nothing appears:
R = 0.35;
L = 0.25;
H = 0.4;
f1 = @(x,y) sqrt(L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2);
fimplicit(f1,[-90 90 -90 90])
I have no problem if I remove the square root, so I guess is due to the imaginary solutions. Using fsurf(f1) I do see some real solutions and I would like to plot them. Is there any way to plot only the real solutions without removing the sqrt ?
Thank you in advance.

Answers (2)

Star Strider
Star Strider on 23 Feb 2021
Nothing is being generated:
R = 0.35;
L = 0.25;
H = 0.4;
f1 = @(x,y) sqrt(L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2);
figure
hfi = fimplicit(f1,[-90 90 -90 90]);
x = hfi.XData
y = hfi.YData
z = hfi.ZData
produces:
x =
1×0 empty double row vector
y =
1×0 empty double row vector
z =
1×0 empty double row vector
However:
figure
hfc = fcontour(f1,[-90 90 -90 90]);
LL = hfc.LevelList;
shows that there are no contours at 0. Forcing a contour at 0 creates a blank plot.
  2 Comments
Paul
Paul on 23 Feb 2021
Thank you so much for answering so fast.
However, I'm still a little bit confused. Because, if the same function without the square root has a solution at 0:
R = 0.35;
L = 0.25;
H = 0.4;
f1 = @(x,y) (L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2);
figure
hfi = fimplicit(f1,[-90 90 -90 90]);
If you add the square root, you should have at least the same real solution. But according to the graph there is nothing being generated at 0.
For example, if I solve:
f2 = @(x,y) x+y;
figure
fimplicit(f2);
The real solution of f2(x,y) = x+y = 0, (x = -y) should be the same for f(x,y) = sqrt(x+y), shouldn't be?
Probably I'm wrong with something, but I don't see why there is no countour at 0.
Star Strider
Star Strider on 23 Feb 2021
My pleasure!
That’s also what I found.
If you want to experiment with the real, imag and abs values, make the appropriate function calls in this version of the fcontour call:
hfc = fcontour(@(x,y)real(f1(x,y)),[-90 90 -90 90]);
hfc = fcontour(@(x,y)imag(f1(x,y)),[-90 90 -90 90]);
hfc = fcontour(@(x,y)abs(f1(x,y)),[-90 90 -90 90]);
I did not specifically analyse the behaviour of the function beyond these experiments. (The output permits setting different options and querying the result for the data the function generates.)

Sign in to comment.


John D'Errico
John D'Errico on 23 Feb 2021
Edited: John D'Errico on 23 Feb 2021
First, look at what you have.
R = 0.35;
L = 0.25;
H = 0.4;
syms x y
F = sqrt(L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2)
F = 
The sqrt is irrelevant, since you are looking for a zero, but things are simpler if we just drop the sqrt.
F2 = L^2-(R*(cosd(x)-sind(x).*sind(y))-H).^2
F2 = 
Now, look for the solution locus. I'll expand the region to a larger interval in x and y, just to see if you missed anything with the narrow domain.
fimplicit(F2,[-180,180, -180 180])
So real solutions do exist. They appear to be sinusoidal curves. And solutions do exist in the domain you specified.
There is no need to worry about complex solutions. There are very real solutions.
ysol = solve(F2 == 0,y,'returnconditions',true)
ysol = struct with fields:
y: [4×1 sym] parameters: [1×1 sym] conditions: [4×1 sym]
ysol.y
ans = 
ysol.parameters
ans = 
k
ysol.conditions
ans = 
So as a function of x, we can find 4 primary solutions for any given x, although for SOME values of x, there may be no real solutions. For example, when x is any integer multiple of 180 degrees, it should be quite clear that no finite solutions can exist, since you would have a divide by zero. And for some values of x, the argument to asin will be greater than 1 or less than -1. In those cases, asin will produce complex results.
Finally, there will be infinitely many solutions, since the solution is a function of an integer parameter k.
  1 Comment
Paul
Paul on 23 Feb 2021
Thanks a lot for your reply, John.
As you say, in this case, I'm looking for a zero, so it is possible to drop the sqrt. However, for a general case in which it is not possible to remove the sqrt or it is not easy to notice that you can get rid of the sqrt, is there a way to plot the solution f(x,y) = 0 ?
I mean, I would like to find the solution without removing the sqrt and as far as I'm concern, with fimplicit() there is no option. Do you know any command?

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!