Introduction to MATLAB
If you type any command in command window and then close MATLAB then the data wouldn’t be
saved.
Declaring variables, matrix, vector, random matrix, function
Variables:
To declare a variable type A = 1
Then variable A will be declared, and the value of the variable will be 1.
*** Variable names are case-sensitive.
Example: A and a are different variables, Add and add are different variables
Matrix:
To add a matrix, declare the matrix B = [a11 a22 a33; b11 b22 b33; c11 c22 c33]
N.B.
We have to put space between each value to differentiate them as an individual value.
Type values of first row, then put a semicolon (;) and
then type the second row, then put a semicolon (;) and
then type the third row.
Vector:
To add a row vector, declare the vector C = [ax ay az]
To add a column vector, declare the vector D = [ax; ay; az]
N.B.
A Row vector has just one row.
A column vector has just one column.
Random Matrix:
To get a Random Matrix of consisting values between z to zn+1 and x rows and y columns
E = randi ([z zn+1], x, y)
Example:
To get a Random Matrix of consisting values between 1 to 5 and 5 rows and 6 columns
type E = randi ([1 5],5,6)
If we use the same command again we will get a completely different matrix as it is a random matrix.
Function:
To declare a function f
f = @ (x) function containing x
F = @ (x, y) function containing both x and y
Example:
f = @ (x) x^3 + x^2 + 1
F = @ (x, y) x^2 + y^2 + 1
Here x is the value for which we want to determine f(x).
N.B.
*** In a declared function all x in the function will be in small letter.
Matlab File Naming Conventions:
1. No space in naming a file
2. Name must not start with a number or a special character
Comment and Hiding output:
To add a single line comment in MATLAB use (%) before the line you want to make comment.
To add a multi line comment in MATLAB use (%{) at the beginning and (%}) at the end of your comment.
To hide the output of a line add semicolon (;) at the end of the line.
Some Basic commands
clc (clears command window)
clear all (removes variables from memory)
disp (display content of an array or string)
fprintf (performs formatted writes to screen)
Some Basic Operations:
A + B add up matrix A and B
A-B subtracts matrix A and B
A * B Multiply matrix A and B
A/B Divide matrix A with B or multiply matrix A and B -1
A^B Matrix A raise to the power B (doesn’t work for matrix)
A.*B Element wise multiplication
A./B Element wise division
A.^B Element wise power
If A is a square matrix and the inverse of matrix A is Y matrix then
Y = inv(A)
Determinant of matrix A is D
D = det(A)
Transpose of matix A is T
T = A.’
Some Basic commands
A(2,3) returns the element at 2nd row 3rd column of Matrix A
A(2,:) returns 2nd row of matrix A
A(:,3) returns 3rd column of matrix A
zeros(3,4) generate matrix of zeros
ones(2,2) generate matrix of ones
rand(4,6) generate matrix having random entries
eye(4) generate identity matrix
To get remainder of x/y
rem(x:y)
Example:
To get remainder of 7/1
R = rem(7,1)
To get remainder of -7/1
R = rem(-7,1)
To get remainder of -1.25/1
R = rem(-1.25,1)
-0.25
Branching Statements in MATLAB (if-else)
Operators (relational, logical)
== Equal to
~= Not equal to
< Strictly smaller
> Strictly greater
<= Smaller than or equal to
>= Greater than equal to
&& And operator
|| Or operator
If-else Statement:
if condition
fprintf() or disp()
else if condition
fprintf() or disp()
else
fprintf() or disp()
end
Loops:
For loop:
a = initial number
h = step size
b = final number
for a:h:b
fprintf() or disp(‘’)
end
While loop:
Initial value
while condition
fprintf() or disp(‘’)
Increment or decrement
end
Plotting:
Introduce your vectors of independent and dependent variables.
Use MATLAB built-in plot command.
Beautify your plot
To edit your plotted graph:
Plot window – view – plot browser
Command Window:
Command Window = We write command here but it can’t be saved for further use. We use command
window to check things.
To call any variable just type the declared variable in command window.
To see the previous commands use the upward arrow key.
To clear command window type clc in command window.
To clear workspace window type clear all in command window.
Type edit in command window to open editor window.
Type close all in command window to close extra windows.
Workspace Window:
Workspace Window = All declared variable are stored here
In the workspace window all the variables that are declared previously are available and by clicking any
variable from the workspace window we can see the excel sheet regarding that variable.
To clear workspace window type clear all in command window.
Now we can’t call any variable as everything is cleared.
Editor Window:
Editor Window = We write the code here
We use editor window to write our codes and to save codes. The variable we declare in editor window
will be stored in the workspace window
Type edit in command window to open editor window.
If you put semicolon at the end of a line in the editor window the line will not be visible in the command
window.