How can I outline a custom function?

I have a custom function that 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 

How to create a contour graph of a function along the x,y coordinates generated by the following meshgrid?

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

This graph simply shows where the function is 0 or 1 on the contour map.

+2
source share
2 answers

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

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

You can then use the CONTOUR function to create a contour graph for the above data. Since your z data has only 2 different values, it will probably be useful for you to only build one contour level (which will have a value of 0.5, halfway between your two values). You can also use the CONTOURF function, which creates color-filled paths that clearly show where those and zeros are:

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


NOTE. . Since you are drawing data that has only ones and zeros, plotting outlines might not be the best way to render. Instead, I would use something like the IMAGESC function, for example:

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

Remember that the y axis in this graph will be changed relative to the plot created by CONTOURF .

+4
source

This will do the following:

 function bincontour clear; clc; xrange = 0:.5:3; yrange = 1:.5:5; [xmesh, ymesh] = meshgrid(xrange, yrange); z = arrayfun(@myFunction, xmesh, ymesh); contourf(xrange, yrange, z, 5) end function val = myFunction(val1, val2) val = rand() > 0.5; end 
+2
source

All Articles