How to plot an Implicit function with certain conditions
36 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Shai Zipori
le 17 Avr 2024
Commenté : Mathieu NOE
le 19 Avr 2024
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.
0 commentaires
Réponse acceptée
Pratyush Swain
le 17 Avr 2024
Hi Shai,
To plot this implicit functions you need to leverage the fimplicit function in MATLAB.Here is an demonstrated example on how we can use this function for your usecase:
warning('off','all');
% Plotting the function using fimplicit
% NOTE: Here I have arbitrarily chosen input values for x and y
% x range --> 1<=x<=2.5, y range --> 0<=y<=1.5
fimplicit(@fun, [1, 2.5, 0, 1.5]);
title('Implicit Function with Conditions');
xlabel('x');
ylabel('y');
% Callback for Impicit to calculate the values, Here x and y are retreived as vector of input values
function values = fun(x, y)
% Initialize output array to NaN %
values = NaN(size(x));
% Define the condition for y to avoid division by zero %
valid = y ~= 0;
% Calculate the expression (2x/y)*(1-1/2y) under valid condition %
% NOTE: Here the ./ operater ensures division occurs on all valid
% elements of vector x
expression = (2*x(valid)./y(valid)).*(1-1./(2*y(valid)));
% Apply all conditions and obtain a final logical vector
condition = x > 1.166 & expression > 0 & expression < pi/2 & y < 1;
% Apply the implicit function where conditions are met
values(condition) = (2*x(condition)./y(condition)) - tan(expression(condition));
end
I have referred to this thread to form a solution for this usecase: https://www.mathworks.com/matlabcentral/answers/1756760-how-to-plot-implicit-function-with-conditions
For more information on fimplicit function, please refer to https://www.mathworks.com/help/matlab/ref/fimplicit.html
2 commentaires
Mathieu NOE
le 18 Avr 2024
you can improve the result by increasing the 'MeshDensity' factor :
h = fimplicit(@fun, [1, 5, 0.5, 1.1], 'MeshDensity',300);
then you get rid of the waves and NaNs
% Plotting the function using fimplicit
% NOTE: Here I have arbitrarily chosen input values for x and y
% x range --> 1<=x<=5, y range --> 0.5<=y<=1.1
h = fimplicit(@fun, [1, 5, 0.5, 1.1], 'MeshDensity',300);
title('Implicit Function with Conditions');
xlabel('x');
ylabel('y');
% if you need to access to the x & y data
x = h.XData;
y = h.YData;
figure
plot(x,y);
title('Implicit Function with Conditions');
xlabel('x');
ylabel('y');
% Callback for Impicit to calculate the values, Here x and y are retreived as vector of input values
function values = fun(x, y)
% Initialize output array to NaN %
values = NaN(size(x));
% Define the condition for y to avoid division by zero %
valid = y ~= 0;
% Calculate the expression (2x/y)*(1-1/2y) under valid condition %
% NOTE: Here the ./ operater ensures division occurs on all valid
% elements of vector x
expression = (2*x(valid)./y(valid)).*(1-1./(2*y(valid)));
% Apply all conditions and obtain a final logical vector
condition = x > 1.166 & expression > 0 & expression < pi/2 & y < 1;
% Apply the implicit function where conditions are met
values(condition) = (2*x(condition)./y(condition)) - tan(expression(condition));
end
Plus de réponses (1)
Mathieu NOE
le 18 Avr 2024
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 commentaires
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!