**Matlab Tutorial** – You can find all the Matlab tutorial here. In this tutorial we will discuss about Mathematical function and basic plotting in Matlab.

**Mathematical functions**

MATLAB offers many predefined mathematical functions for technical computing which contains a large set of mathematical functions.

Typing help elfun and help specfun calls up full lists of elementary and special functions respectively.

There is a long list of mathematical functions that are built into MATLAB. These functions are called built-ins. Many standard mathematical functions, such as sin(x), cos(x), tan(x), e^x , ln(x), are evaluated by the functions sin, cos, tan, exp, and log respectively in MATLAB.

Table below lists some commonly used functions, where variables x and y can be numbers, vectors, or matrices

In addition to the elementary functions, MATLAB includes a number of predefined constant values. A list of the most common values is given below:

**Notes: **

• Only use built-in functions on the right hand side of an expression. Reassigning the value to a built-in function can create problems.

• There are some exceptions. For example, i and j are pre-assigned to √ −1. However, one or both of i or j are often used as loop indices.

• To avoid any possible confusion, it is suggested to use instead ii or jj as loop indices.

**Basic plotting**

MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation is possible with very few commands. You are highly encouraged to plot mathematical functions and results of analysis as often as possible. Trying to understand mathematical equations with graphics is an enjoyable and very efficient way of learning mathematics. Being able to plot mathematical functions and data freely is the most important step, and this section is written to assist you to do just that.

**Creating simple plots**

The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x coordinates, x = (x1, . . . , xN ), and a vector of y-coordinates, y = (y1, . . . , yN ), locate the points (xi , yi), with i = 1, 2, . . . , n and then join them by straight lines. You need to prepare x and y in an identical array form; namely, x and y are both row arrays or column arrays of the same length.

The MATLAB command to plot a graph is plot(x,y). The vectors x = (1, 2, 3, 4, 5, 6) and y = (3, −1, 2, 4, 5, 1) produce the picture shown in Figure.

>> x = [1 2 3 4 5 6];

>> y = [3 -1 2 4 5 1];

>> plot(x,y)

**Note:** The plot functions has different forms depending on the input arguments. If y is a vector plot(y)produces a piecewise linear graph of the elements of y versus the index of the elements of y. If we specify two vectors, as mentioned above, plot(x,y) produces a graph of y versus x.

For example, to plot the function sin (x) on the interval [0, 2π], we first create a vector of x values ranging from 0 to 2π, then compute the sine of these values, and finally plot the result:

>> x = 0:pi/100:2*pi;

>> y = sin(x);

>> plot(x,y)

Notes:

• **0:pi/100:2*pi** yields a vector that

– starts at 0,

– takes steps (or increments) of π/100,

– stops when 2π is reached.

• If you omit the increment, MATLAB automatically increments by 1

**Adding titles, axis labels, and annotations**

MATLAB enables you to add axis labels and titles. For example, using the graph from the previous example, add an x- and y-axis labels

Now label the axes and add a title. The character \pi creates the symbol π. An example of 2D plot is shown in Figure.

>> xlabel(’x = 0:2\pi’)

>> ylabel(’Sine of x’)

>> title(’Plot of the Sine function’)

The color of a single curve is, by default, blue, but other colors are possible. The desired color is indicated by a third argument. For example, red is selected by plot(x,y,’r’). Note the single quotes, ’ ’, around r.

**Multiple data sets in one plot**

Multiple (x, y) pairs arguments create multiple graphs with a single call to plot. For example, these statements plot three related functions of x: y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 ∗ cos(x), in the interval 0 ≤ x ≤ 2π.

>> x = 0:pi/100:2*pi;

>> y1 = 2*cos(x);

>> y2 = cos(x);

>> y3 = 0.5*cos(x);

>> plot(x,y1,’–’,x,y2,’-’,x,y3,’:’)

>> xlabel(’0 \leq x \leq 2\pi’)

>> ylabel(’Cosine functions’)

>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)

>> title(’Typical example of multiple plots’)

>> axis([0 2*pi -3 3])

The result of multiple data sets in one graph plot is shown in Figure below.

By default, MATLAB uses line style and color to distinguish the data sets plotted in the graph. However, you can change the appearance of these graphic components or add annotations to the graph to help explain your data for presentation.

**Specifying line styles and colors**

It is possible to specify line styles, colors, and markers (e.g., circles, plus signs, . . . ) using the plot command:

plot(x,y,’style_color_marker’)

where style_color_marker is a triplet of values from Table.

To find additional information, type help plot or doc plot.

You can see the complete Matlab Matlab here:

- Introduction to Matlab
- Chapter 2 on Matlab
- Matrix on Matlab
- Array And Linear Operation Matlab
- Programming in Matlab
- Control Flow and Operation in Matlab
- Debugging M-files in Matlab

## 1 Comment