如何轮廓绘制自定义函数? [英] How can I contour plot a custom function?

查看:69
本文介绍了如何轮廓绘制自定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义函数,它根据两个给定的输入返回01:

I have a custom function which returns either 0 or 1 depending on two given inputs:

function val = myFunction(val1, val2)

  % logic to determine if val=1 or val=0

end

如何在以下网格网格生成的x,y坐标上创建函数的轮廓图?

How can I create a contour plot of the function over the x,y coordinates generated by the following meshgrid?

meshgrid(0:.5:3, 0:.5:3);

此图将仅显示等值线图上函数为01的位置.

This plot will just simply display where the function is 0 or 1 on the contour map.

推荐答案

如果函数myFunction并非旨在处理矩阵输入,则可以使用函数

If your function myFunction is not designed to handle matrix inputs, then you can use the function ARRAYFUN to apply it to all the corresponding entries of x and y:

[x,y] = meshgrid(0:0.5:3);      %# Create a mesh of x and y points
z = arrayfun(@myFunction,x,y);  %# Compute z (same size as x and y)

然后,您可以使用函数 CONTOUR 生成等高线图以上数据.由于您的z数据只有2个不同的值,因此只绘制一个轮廓线水平(该值为0.5,介于两个值之间的一半)可能很有意义.您可能还想使用 CONTOURF 函数,该函数会产生颜色-填充轮廓,可以清楚地显示出零和零的位置:

Then you could use the function CONTOUR to generate a contour plot for the above data. Since your z data only has 2 different values, it would probably make sense for you to only plot one contour level (which would be at a value of 0.5, halfway between your two values). You might also want to instead use the function CONTOURF, which produces color-filled contours that will clearly show where the ones and zeroes are:

contourf(x,y,z,1);  %# Plots 1 contour level, filling the area on either
                    %#   side with different color


注意::由于您要绘制的数据只有一和零,因此绘制等高线可能不是可视化最佳方法.相反,我会使用类似功能 IMAGESC 的东西,例如:


NOTE: Since you are plotting data that only has ones and zeroes, plotting contours may not be the best way to visualize it. I would instead use something like the function IMAGESC, like so:

imagesc(x(1,:),y(:,1),z);

请记住,相对于 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆