
Creating 3d plots in Octave
In this online tutorial, I will show you how to create a 3D plot in Octave.
So, what is a 3D plot? It's a plot drawn in 3D space, with the x, y, and z axes. It's used to represent a mathematical function f(x,y) with two independent variables, where z=f(x,y).
Let me give you a practical example.
First, create arrays with the values of the x and y axes from -10 to 10.
>> x = y = linspace(-10,10,50)';
Both arrays are composed of 50 elements.
Now, create the matrix of points on the xy plane using the meshgrid() function and assign the values to the xx and yy arrays.
>> [xx,yy]=meshgrid(x,y);
Next, create the array with the values of the function you want to represent.
For example, create the array for the function f(x,y)=x2-y2 using the points xx and yy on the xy plane.
>> z=xx.^2-yy.^2;
To draw the 3D plot, use the mesh() command, specifying the x, y, and z coordinates of the function.
>> mesh(x,y,z)
The result is the 3D plot of the function z=x2-y2 in space.
If you want to visualize the contour lines as well, use the meshc(x,y,z) command.
>> meshc(x,y,z)
This will give you a plot with contour lines on the base plane.
So, what are contour lines? Contour lines (or contour curves) are the projections of the heights onto the plane. The inner circles indicate higher elevations, much like in geographical maps where the inner lines represent the heights of hills and mountains or the depths of the sea.
Alternatively, you can use the meshz() command.
>> meshz(x,y,z)
This command displays vertical lines between the plot and the base.
Another alternative command to represent a function in 3D is the surf() command.
>> surf(x,y,z)
This command colors the spaces in the plot's grid.
To add contour lines to this representation, use the surfc(x,y,z) command.
>> surfc(x,y,z)
This will show contour lines on the xy plane.
Finally, to display lighting effects on the plot, use the surfl() command.
surfl(x,y,z)
This command adds lighting effects to the plot's surface.
In this introductory lesson, I've provided you with a list of commands that allow you to draw any mathematical function in 3D in Octave.
However, Octave's graphic capabilities are much broader than what I've covered here.