How to plot a simple graph with matlab
Example 1: Let us first understand the simple plot :
- MATLAB
% angle from 0 to 2pi theta = 0 : 0.01 : 2 * pi; % sin function works on an array f = sin(theta); % 2D plot plot(theta, f, 'b' ) % label for X-axis xlabel( '\theta' ); % label for Y-axis ylabel( 'sin(\theta)' ); title( 'Plot of Sine function' ) |
Output :
In the above code, the sine graph is plotted. Here as seen from the code we first define theta value of the sine plot, then build an array out of it and that function is finally plotted on the graph as shown in the output. Example 2: Here we will plot the helix which is a 3-dimensional figure. As we all know from basic geometry that a helix is formed from the combination of the sine function and cosine function by declaring the value of theta and then iterating it over the same function. Try to visualize the output.
- MATLAB
% angle from 0 to 2pi theta = 0:0.01:2*pi; % sin function works on an array f = sin(theta); % 3D plot t = 0 : pi / 50 : 10 * pi; % open a new figure figure % 3D plot (helix) plot3(sin(t), cos(t), t); |
Output :Example 3: