MATLAB Examples

This is an example of how to create a contour plot from a function in MATLAB®.

Read about the fcontour function in the MATLAB documentation. This function is available in R2016a or newer.

For more examples, go to MATLAB Plot Gallery

% Check version
if verLessThan('matlab','9.0')
    error(['fcontour is available in R2016a or newer. ', ...
        'For older releases, use ezcontourf instead.'])
end

% Create the contour plot using the function f(x,y) = sin(3*x)*cos(x+y)
figure
fcontour(@(x,y) sin(3*x).*cos(x+y),[0 3 0 3],'Fill','on','LineColor','k')

% Add labels and title
xlabel('x')
ylabel('y')
title('Contour plot for f(x,y) = sin(3*x)*cos(x+y)')

% Change the default colormap to 'spring'
colormap('spring')