How to plot an Implicit function with certain conditions
Show older comments

im trying to plot in MATLAB the red implicit function given these 3 conditions, in DESMOS it's super easy but unfortunately i have no idea how to pull it off in MATLAB, i've searched the internet but my luck fell short, would appreciate the help.
Accepted Answer
More Answers (1)
hello @Shai Zipori
I have to say I don't do much with implicit function problems , tried using fsolve and fminbnd but was lacking how to implement the conditions
now, a poor's man solution is to create a x, y grid , evaluate your function, and find the x,y points where you function is minimal
% solving implicit function
% (2*x./y) - tan((2*x./y).*(1-1./(2*y))) = 0;
x = linspace(1.167,5,1000); % create x array with condition x>1.166
y = linspace(0.5,1,1000);
% create a X Y meshgrid and evaluate function
[X,Y] = meshgrid(x,y);
C = (2*X./Y).*(1-1./(2*Y));
fun = (2*X./Y) - tan(C); % this is your implicit function
% apply condition (C>0 & C<pi/2)
ind = (C>0 & C<pi/2);
Z = NaN(size(fun));
Z(ind) = fun(ind); % keep only valid fun values according to condition (C>0 & C<pi/2)
% plot function Z=f(x,y) to show minimum line (is what we are looking for)
figure
h = imagesc(x,y,log(abs(Z)));
colorbar('vert')
set(gca,'YDir','normal')
set(h, 'AlphaData', 1-isnan(Z))
% find x,y of minimum line
for ci = 1:numel(x)
zz = Z(:,ci);
% find y coordinate to get minimum z value
[val,ind] = min(abs(zz));
if ~isempty(ind)
xc(ci) = x(ci);
yc(ci) = y(ind);
end
end
figure
plot(xc,yc);
4 Comments
the method above will give you a correct plot if you have a refined grid resolution . if you reduce the grid size , the final curve will exibit some staircase behaviour
of course , one solution is to smooth the coarse curve
% solving implicit function
% (2*x./y) - tan((2*x./y).*(1-1./(2*y))) = 0;
x = linspace(1.167,5,300); % create x array with condition x>1.166
y = linspace(0.5,1,300);
% create a X Y meshgrid and evaluate function
[X,Y] = meshgrid(x,y);
C = (2*X./Y).*(1-1./(2*Y));
fun = (2*X./Y) - tan(C); % this is your implicit function
% apply condition (C>0 & C<pi/2)
ind = (C>0 & C<pi/2);
Z = NaN(size(fun));
Z(ind) = fun(ind); % keep only valid fun values according to condition (C>0 & C<pi/2)
% plot function Z=f(x,y) to show minimum line (is what we are looking for)
figure
h = imagesc(x,y,log(abs(Z)));
colorbar('vert')
set(gca,'YDir','normal')
set(h, 'AlphaData', 1-isnan(Z))
% find x,y of minimum line
for ci = 1:numel(x)
% find y coordinate to get minimum z value
[val,ind] = min(abs(Z(:,ci)));
if ~isempty(ind)
xc(ci) = x(ci);
yc(ci) = y(ind);
end
end
yf = smoothdata(yc,'loess',25);
figure
plot(xc,yc,'b',xc,yf,'r');
legend('coarse','smoothed');
or you could do this :
% solving implicit function
% (2*x./y) - tan((2*x./y).*(1-1./(2*y))) = 0;
x = linspace(1.167,5,300); % create x array with condition x>1.166
y = linspace(0.5,1,300);
% create a X Y meshgrid and evaluate function
[X,Y] = meshgrid(x,y);
C = (2*X./Y).*(1-1./(2*Y));
fun = (2*X./Y) - tan(C); % this is your implicit function
% apply condition (C>0 & C<pi/2)
ind = (C>0 & C<pi/2);
Z = NaN(size(fun));
Z(ind) = fun(ind); % keep only valid fun values according to condition (C>0 & C<pi/2)
% plot function Z=f(x,y) to show minimum line (is what we are looking for)
figure
h = imagesc(x,y,log(abs(Z)));
colorbar('vert')
set(gca,'YDir','normal')
set(h, 'AlphaData', 1-isnan(Z))
% find x,y of minimum line
k1 = 0;
k2 = 0;
for ci = 1:numel(x)
% find x, y coordinates of minimum Z value
% 1/ coarse method (relies on grid resolution)
[val,ind] = min(abs(Z(:,ci)));
if ~isempty(ind)
k1 = k1 + 1;
xc(k1) = x(ci);
yc(k1) = y(ind);
end
% 2/ refined method with linear interpolation
[ZxP,ZxN] = find_zc(y,Z(:,ci),0);
if ~isempty(ZxN)
k2 = k2 + 1;
xf(k2) = x(ci);
yf(k2) = ZxN;
end
end
figure
plot(xc,yc,'b',xf,yf,'r');
legend('coarse','refined');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ZxP,ZxN] = find_zc(x,y,threshold)
% put data in rows
x = x(:);
y = y(:);
% positive slope "zero" crossing detection, using linear interpolation
y = y - threshold;
zci = @(data) find(diff(sign(data))>0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxP = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
% negative slope "zero" crossing detection, using linear interpolation
zci = @(data) find(diff(sign(data))<0); %define function: returns indices of +ZCs
ix=zci(y); %find indices of + zero crossings of x
ZeroX = @(x0,y0,x1,y1) x0 - (y0.*(x0 - x1))./(y0 - y1); % Interpolated x value for Zero-Crossing
ZxN = ZeroX(x(ix),y(ix),x(ix+1),y(ix+1));
end
Shai Zipori
on 18 Apr 2024
Mathieu NOE
on 19 Apr 2024
my pleasure !
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







