Skip to content

Instantly share code, notes, and snippets.

@nh2
Created November 19, 2013 08:22
Show Gist options
  • Save nh2/7542053 to your computer and use it in GitHub Desktop.
Save nh2/7542053 to your computer and use it in GitHub Desktop.
Simple way to plot a function taking 2 arguments in Matlab
% Simple way to plot a function taking 2 arguments.
% Usage:
% plot3d( @(x,y) x^2+y^2, range1, range2 )
function plot3d(f, range1, range2)
% mesh really wants it in this order :(
r = zeros(length(range2), length(range1));
[X, Y] = meshgrid(range1, range2);
s1 = size(range1)
s2 = size(range2)
sx = size(X)
sy = size(Y)
for ix = 1:length(range1)
for iy = 1:length(range2)
% mesh really wants it in this order :(
r(iy, ix) = f( X(iy, ix), Y(iy, ix) );
end
end
mesh(X, Y, r);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment