English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This chapter will continue to explore MATLAB's plotting and graphics features. We will discuss-
Draw a bar chart
Draw contours
3D plot
bar command to draw a two-dimensional bar chart. Let's take an example to illustrate this idea.
Let's have an imaginary classroom with10students. We know that the percentage of scores obtained by these students is75,58,90,87,50,85,92,75,60 and95. We will draw a bar chart of this data.
Create a script file and enter the following code-
x = [1:10]; y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95]; bar(x,y), xlabel('Student'), ylabel('Score'), title('First Sem:') print -deps graph.eps
When the file is run, MATLAB displays the following bar chart-
The contour of a function with two variables is a curve along which the function has a constant. Contours are used to create contour plots by connecting contour points at the same elevation (such as mean sea level).
MATLAB provides functions for drawing contours contour .
Let's generate a contour plot to show the contours of the given function g=f(x,y). This function has two variables. Therefore, we must generate two independent variables, namely two datasets x and y. This is done by calling the meshgrid command.
meshgridcommand is used to generate element matrices, which give the range of x and y and the increment description for each case.
Let's plot the function g = f(x, y), where -5≤x≤5, -3≤y≤3. Let's take 0. for the two values.1increment. Variable settings are-
[x,y] = meshgrid(-5:0.1:5, -3:0.1:3);
Finally, we need to assign values to the function that can. Let our function be: x 2 + y 2
Create a script file and enter the following code-
[x, y] = meshgrid(-5:0.1:5,-3:0.1:3); % Independent variables g = x.^2 + y.^2; % Our function contour(x,y,g) % Call the contour function print -deps graph.eps
When running the file, MATLAB displays the following contour plot-
Let's slightly modify the code to organize the mapping
[x, y] = meshgrid(-5:0.1:5,-3:0.1:3; independent variables g = x.^2 + y.^2; % our function [C, h] = contour(x, y, g); % call the contour function set(h, 'ShowText', 'on', 'TextStep', get(h, 'LevelStep'))*2) print -deps graph.eps
When running the file, MATLAB displays the following contour plot-
The three-dimensional plot basically shows the surface defined by two variables g = f(x, y) as defined by the function.
As mentioned before, to define g, we first usemeshgridThe command creates a set of (x, y) points within the range of the function. Next, we assign the function itself. Finally, we usesurfThe command creates a surface plot.
The following example demonstrates the concept-
Let's define the function g = xe- (x 2 + y 2)Create3D-surface plot.
Create a script file and enter the following code-
[x, y] = meshgrid(-2:.2:2); g = x.* exp(-x.^2 - y.^2); surf(x, y, g) print -deps graph.eps
When running the file, MATLAB displays the following3-D-mapping-
You can also usemeshThe command generates a three-dimensional surface. However,surfThe command also displays the surfaces with connecting lines in color whilemeshThe surface mesh created by the command is displayed with colored lines connecting the defined points.