how to do a contour plot using function handle?
30 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
By creating meshgrid, I can do contour plot.
T=linspace(0,2*pi,100);
d = linspace(0,2*pi,100) ;
[X,Y] = meshgrid(T,d);
M =(sin(Y).*sin(2.*X)) ;
contourf(X*(180/pi),Y*(180/pi),M)
But when I try to do it as
M = @(T,d)(sin(d).*sin(2.*T)) ;
fcontour(M)
I'm not able to get any graph. If anybody can explain to me how this works. Appriciate your help.
3 commentaires
Aquatris
il y a environ 10 heures
I tried in 2019b, and just giving function to the fcontour function also works. No need to provide arguments to the M. Interesting behaviour.
Réponses (2)
Star Strider
il y a environ 11 heures
Provide arguments to ‘M’ and it works —
T=linspace(0,2*pi,100);
d = linspace(0,2*pi,100) ;
[X,Y] = meshgrid(T,d);
M = @(T,d)(sin(d).*sin(2.*T)) ;
figure
contourf(M(X,Y))
.
2 commentaires
Star Strider
il y a environ 11 heures
Modifié(e) : Star Strider
il y a environ 7 heures
My pleasure!
EDIT — (22 Jul 2024 at 15:58)
This would also work —
T=linspace(0,2*pi,100);
d = linspace(0,2*pi,100);
[Tmin, Tmax] = bounds(T)
[dmin, dmax] = bounds(d)
MshDns = numel(T)
M = @(T,d)(sin(d).*sin(2.*T)) ;
hfc = fcontour(M, [Tmin Tmax dmin dmax], 'MeshDensity',MshDns, 'Fill','on', 'LineColor','k');
% get(hfc)
SzX = size(hfc.XData)
SzY = size(hfc.YData)
SzZ = size(hfc.ZData)
hfc.ContourMatrix
With the stated 'MeshDensity' value, the result would likely be the same as using contour, however it would only produce square matrices, so if you wanted them to have different dimensions, you would have to use the first example and contourf. The last two arguments are necessary to reproduce the contourf result.
All the results could be exported as well for use elsewhere in your code.
.
Muskan
il y a environ 11 heures
Hi,
As per my understanding the issue occurs because because "fcontour" needs a function handle that takes two individual scalar inputs, not a single vector. So, the correct approach is to define the function handle in such a way that it matches fcontour's expected input.
You can follow the following steps to properly define and use the function handle with "fcontour":
- Define the function handle to take two separate inputs.
- Use "fcontour" with the correct function handle and specify the range for "T" and "d".
Here is a code snippet on how you can achieve the same:
% Define the function handle to take two separate inputs
M = @(T, d) sin(d).*sin(2.*T);
% Plot using fcontour
fcontour(M, [0 2*pi 0 2*pi])
xlabel('T (radians)')
ylabel('d (radians)')
title('Contour plot of sin(d) * sin(2*T)')
Kindly refer to the following documentation of "fcontour" for more information: https://www.mathworks.com/help/matlab/ref/fcontour.html
2 commentaires
Stephen23
il y a environ 10 heures
Modifié(e) : Stephen23
il y a environ 9 heures
"As per my understanding the issue occurs because because "fcontour" needs a function handle that takes two individual scalar inputs, not a single vector. "
The FCONTOUR documentation actually states that "The function must accept two matrix input arguments and return a matrix output argument of the same size." (bold added)
The OP's code does not accept "a single vector", it accepts two matrices.
"So, the correct approach is to define the function handle in such a way that it matches fcontour's expected input."
It already does.
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!