matlab - How can I contour plot a custom function? -


i have custom function returns either 0 or 1 depending on 2 given inputs:

function val = myfunction(val1, val2)    % logic determine if val=1 or val=0  end 

how can create contour plot of function on x,y coordinates generated following meshgrid?

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

this plot display function 0 or 1 on contour map.

if function myfunction not designed handle matrix inputs, can use function arrayfun apply corresponding entries of x , y:

[x,y] = meshgrid(0:0.5:3);      %# create mesh of x , y points z = arrayfun(@myfunction,x,y);  %# compute z (same size x , y) 

then use function contour generate contour plot above data. since z data has 2 different values, make sense plot 1 contour level (which @ value of 0.5, halfway between 2 values). might want instead use function contourf, produces color-filled contours show ones , zeroes are:

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


note: since plotting data has ones , zeroes, plotting contours may not best way visualize it. instead use function imagesc, so:

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

keep in mind y-axis in plot reversed relative plot generated contourf.


Comments

Popular posts from this blog

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -

openssl - Load PKCS#8 binary key into Ruby -