how to do a contour plot using function handle?

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 个评论

Odd. It works here:
M = @(T,d)(sin(d).*sin(2.*T)) ;
fcontour(M)
I'm using 2021a. But providing arguments to M, as suggested by another answer works. Thank you.
I tried in 2019b, and just giving function to the fcontour function also works. No need to provide arguments to the M. Interesting behaviour.

请先登录,再进行评论。

 采纳的回答

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))
.

4 个评论

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)
Tmin = 0
Tmax = 6.2832
[dmin, dmax] = bounds(d)
dmin = 0
dmax = 6.2832
MshDns = numel(T)
MshDns = 100
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)
SzX = 1x2
100 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
SzY = size(hfc.YData)
SzY = 1x2
100 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
SzZ = size(hfc.ZData)
SzZ = 1x2
100 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
hfc.ContourMatrix
ans = 2x3661
-0.8000 2.4455 2.4117 2.3483 2.2848 2.2659 2.2213 2.1871 2.1579 2.1412 2.1096 2.0944 2.0857 2.0689 2.0557 2.0459 2.0392 2.0356 2.0348 2.0370 2.0422 2.0504 2.0619 2.0768 2.0944 2.0959 2.1246 2.1579 2.1600 2.2168 61.0000 0.9520 0.9361 0.9281 0.9415 0.9520 0.9799 1.0155 1.0507 1.0789 1.1424 1.1790 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9635 1.9675 2.0309 2.0916 2.0944 2.1579
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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.
.
I see. Thanks for expaining.

请先登录,再进行评论。

更多回答(1 个)

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":
  1. Define the function handle to take two separate inputs.
  2. 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 个评论

"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.
Hi, I likely missed catching that, thank you for pointing that out, that helps a lot!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by