BIRLA VISHVAKARMA MAHAVIDYALAYA
VALLABHVIDYANAGAR
ES106: Programming for Engineers
Introduction to MATLAB
Experiment No: 1
Date:
Objective: To get familiarized with MATLAB programming environment
Theory:
MATLAB
MATLAB stands for matrix laboratory is a very powerful technical language for mathematical
programming. After logging into your account, you can enter MATLAB by double-clicking on
the MATLAB shortcut icon on your Windows desktop. When you start MATLAB, a special
window called the MATLAB desktop appears. The desktop is a window that contains other
windows. The major tools within or accessible from the desktop are:
The Command Window
The Command History
The Workspace
The Current Directory
The Help Browser
The Start button
Using MATLAB as a calculator
As an example of a simple interactive calculation, just type the expression you want to evaluate.
Let's start at the very beginning. For example, let's suppose you want to calculate the
expression, 1 + 2 * 3. You type it at the prompt command (>>) as follows,
>> 1 + 2 ∗ 3
ans =
7
You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation. Note that the
variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a
value to a variable or output argument name. For example,
>> 𝑥 = 1 + 2 ∗ 3
x =
7
will result in x being given the value 1 + 2*3 = 7. This variable name can always be used to
refer to the results of the previous computations. Therefore, computing 4x will result in
>> 4 ∗ 𝑥
ans =
28.0000
Before we conclude this minimum session, Table gives the partial list of arithmetic operators.
Symbol Operation Example
+ Addition 2+3
- Subtraction 2-3
* Multiplication 2*3
/ Division 2/3
MATLAB variables are created with an assignment statement. The syntax of variable
assignment is
variable name = a value (or an expression)
For example,
>> 𝑥 = 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛
where expression is a combination of numerical values, mathematical operators, variables, and
function calls. On other words, expression can involve:
manual entry
built-in functions
user-defined functions
>> clear
The command clear or clear all removes all variables from the workspace. This frees up system
memory. In order to display a list of the variables currently in the memory,
type
>> who
while, whos will give more details which include size, space allocation, and class of the
variables.
To view the online documentation, select MATLAB Help from Help menu or MATLAB Help
directly in the Command Window. The preferred method is to use the Help Browser. The Help
Browser can be started by selecting the ? icon from the desktop toolbar. On the other hand,
information about any command is available by typing
>> help Command
Use on-line help to request info on a specific function
>> help sqrt
MATLAB offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions. Typing help fun and help spec fun 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), ex, ln(x), are evaluated by the functions
sin, cos, tan, exp, and log respectively in MATLAB.
cos(x) Cosine abs(x) Absolute value
sin(x) Sine sign(x) Signum function
tan(x) Tangent max(X) Maximum value
acos(x) Arc cosine min(x) Minimum value
asin(x) Arc cosine ceil(x) Round towards +∞
atan(x) Arc tangent floor(x) Round towards -∞
exp(x) Exponential round(x) Round to nearest integer
sqrt(x) Square root rem(x) Remainder after division
log(x) Natural logarithm angle(x) Phase angle
log10(x) Common logarithm conj(x) Complex conjugate
Commonly used commands
>> clc : to clear command window
>> who : list variables in the current workspace
>> whos : along with list of variables it give information like size , number of bytes , type
>> clear : clears variable from the current workspace.
Handling array:-
Descriptio
Command Output (Results)
n
Row array A=
A = [11,12,13,14]
11 12 13 14
Column A=
array 11
A = [11;12;13;14]
12
13
14
Matrix A =
A = 11 12 13 14
[11:14;21:24;31:34;41:44 21 22 23 24
] 31 32 33 34
41 42 43 44
Draw Plot for following example;
Descriptio
Command Output(Results)
n
Create an
x-array of
100
X=linespace(0,4*pi,10 X=
samples
0) [0 …….]
between 0
and 4π.
Calculate
Y = sin(x)
sin(.) of
the x-
array
Plot the
Plot(Y)
y-array
Exercise
1 Get familiarised with MATLAB window, MATLAB Script, Console.
2 Perform basic codes given in the manual and check how you can work with
MATLAB.