ManticMoo.COM -> Jeff's Articles -> Programming Articles -> Matlab -> Graphing a 3D function using MatLab

Graphing a 3D function using MatLab

by Jeffrey P. Bigham

Creating a 3D graph using Matlab is incredibly simply, but at first glance seems somewhat intimidating. You use what is called a mesh which will allow you to specify the function value at each 2d point. First, initialize the mesh of points at which you will graph the function's value. The following will graph the function at x values from 0 to 100 in increments of 0.1 and y values from -50 to 50 in increments of 0.5.


[x, y] = meshgrid(0:.1:100, -50:.5:50);

Next, assign z the formula that you want to graph. Just substitute x and y into your formula of choice:


z = y * sin( pi * x / 10.0);

And, finally, graph the mesh as follows:


mesh(z)

Putting it all together, you get:


[x, y] = meshgrid(0:.1:100, -50:.5:50);
z = y * sin( pi * x / 10.0);
mesh(z)

And all of this leads to this beautiful plot: plot of z = y * sin(pi*x/10)

ManticMoo.COM -> Jeff's Articles -> Programming Articles -> Matlab -> Graphing a 3D function using MatLab