Getting Started

Matlab Interface

1. The Script: This is where the code is typed. Here we have simply defined a variable "var" and assigned to it the value of 2 + 3. Then, we display the value of the variable on line 2. The semi-colons (;) at the end tell Matlab that this row of code is finished.

2. The Command Window: This is where the output is displayed. Here, as we suppressed the output of line 1 with a semi-colon, only the disp(var) creates an output which shows the value of 5.

3. The Workspace: This is where all the variable, arrays, vectors and matrices are stored. Depending on their size, they will be displayed. For large values, an approximation or the dimensions will be shown instead.

4. The Toolbar: This is where all the useful tools are stored. It allows the user to Run the code as well as saving and opening files and many other commands.

Basic Code

Here are some of the basic yet most useful operators on Matlab:

1. + - * / : The "sum", "subtract", "multiply" and "divide" operators.

2. cos() and sin() : The cosine and sine operators. Matlab can also do tan(), acos(), asin(), atan() and hyperbolic functions cosh(), sinh() and tanh().
N.B.These operators are in radians and need to be spelt cosd() and sind() if degrees are used.

3. ^ : The caret symbol used to raise a number to a given exponential power.

4. sqrt() : Stands for square root and gives such number.

5. exp() : Stands for exponential and gives e raised to the given power.
N.B. Matlab can process imaginary numbers.

6. log() : Gives the natural logarithm of the given number

7. abs() : Gives the absolute value of a variable.

N.B. The values here are displayed in the command window as their output was not suppressed using a semi-colon at the end of the line. Otherwise, each variable and its assigned value can be seen in the workspace.


Plotting Points

Plotting specific points on Matlab is easy and follows the format:

plot(X, Y, 'style', X2, Y2, 'style_2', ..., Xn, Yn, 'style_n');

X are the x values
Y are the y values
style represents the way the points appears on the graph. 'x' for a cross, '.' for a dot etc...
N.B. We do not need toput a semi-colon at the end of the 'plot' line as it will show a graph either way. Yet, it is good practice to end each statement with a semi-colon unless wanted otherwise.

Here we are using the following code:

plot(1, 2, 'x', 1, 3, 'x', 2, 3,'x', 2, 1, 'x');

Plotting 2D Graphs

Plotting 2D graphs on Matlab follows the format:

x = [lower limit:increment:upper limit];
y = function of x;
plot(X, Y, 'style');

N.B. Here the style is optional and might be better left as empty in order to get a continuous line as opposed to each individual point on the line.

Here we are using the following code:

x = [0:0.1:4*pi];
y = sin(x);
plot(x, y);

Plotting 3D Graphs

Plotting 3D graphs on Matlab follows the format:

[x, y] = meshgrid[lower limit:increment:upper limit];
z = function of x and y;
surf(x, y, z);

Here we are using the following code:

[x, y] = meshgrid(-5:0.1:5);
z = x.^2 - y.^2;
surf(x, y, z);
shading interp;

N.B. shading interp is not needed but removes the gridlines to give a smoother look to the graph. Also, as x and y are matrices of size 101 x 101, the '.' tells Matlab to multiply element-wise as opposed to multiply them as matrices.

Finally, it is worth noting that there are other types of plots such as contour plots, quiver plots etc.. which might be useful. However, this is a basic introduction to Matlab plots and therefore not the right place to introduce those.