MATLAB EXPERIMENT 1
MATLAB EXPERIMENT 1
1.1 Objectives
1.2 Introduction
Known for its highly optimized matrix and vector calculations, MATLAB offers an intuitive
language for expressing problems and their solutions both mathematically and visually.
Typical uses include:
• Numeric computation and algorithm development.
• Symbolic computation (with the built-in Symbolic Math functions).
• Modelling, simulation and prototyping.
• Data analysis and signal processing.
• Engineering graphics and scientific visualization.
1.3 History
1.4 Strengths
1.5 Weaknesses
MATLAB development IDE can be launched from the icon created on the desktop. The main
working window in MATLAB is called the desktop. When MATLAB is started, the desktop
appears in its default layout:
Current Folder - This panel allows you to access the project folders and files.
Command Window - This is the main area where commands can be entered
at the commandline. It is indicated by the command
prompt (>>).
Workspace - The workspace shows all the variables created and/or imported from
files.
Command History - This panel shows or rerun commands that are entered
at the commandline.
You are now faced with the MATLAB desktop on your computer, which contains the prompt
(>>) in the Command Window. Usually, there are 2 types of prompt:
Note:
1. To simplify the notation, we will use this prompt, >>, as a standard prompt sign,
though our MATLAB version is for educational purpose.
2. MATLAB adds variable to the workspace and displays the result in the Command
Window.
Command Description
cd Change current directory
clc Clear the Command Window
clear (all) Removes all variables from the workspace
clear x Remove x from the workspace
copy file Copy file or directory
delete Delete files
dir Display directory listing
exist Check if variables or functions are defined
help Display help for MATLAB functions
look for Search for specified word in all help entries
mkdir Make new directory
move file Move file or directory
pwd Identify current directory
rmdir Remove directory
type Display contents of file
what List MATLAB files in current directory
which Locate functions and files
who Display variables currently in the workspace
whos Display information on variables in the workspace
Commonly used Operators and Special Characters
MATLAB supports the following commonly used operators and special characters:
Operator Purpose
+ Plus; addition operator.
- Minus; subtraction operator.
* Scalar and matrix multiplication operator.
.* Array multiplication operator.
^ Scalar and matrix exponentiation operator.
.^ Array exponentiation operator.
\ Left-division operator.
/ Right-division operator.
.\ Array left-division operator.
./ Array right-division operator.
: Colon; generates regularly spaced elements and represents an
entire row or column.
() Parentheses; encloses function arguments and array indices;
overrides precedence.
[] Brackets; enclosures array elements.
. Decimal point.
… Ellipsis; line-continuation operator
, Comma; separates statements and elements in a row
; Semicolon; separates columns and suppresses display.
% Percent sign; designates a comment and specifies formatting.
_ Quote sign and transpose operator.
._ Non-conjugated transpose operator.
= Assignment operator.
Note:
If you end a statement with a semicolon, MATLAB performs the computation, butsuppresses
the display of output in the Command Window.
Special Variables and Constants
MATLAB supports the following special variables and constants:
Name Meaning
ans Most recent answer.
eps Accuracy of floating-point precision.
i,j The imaginary unit √-1.
Inf Infinity.
NaN Undefined numerical result (not a number).
pi The number π
Naming Variables
Variable names consist of a letter followed by any number of letters, digits or underscore.
MATLAB is case-sensitive.
Variable names can be of any length; however, MATLAB uses only first N characters, where
N is given by the function namelengthmax.
Example 1:
Example 2:
Example 3:
In the above example it creates a 1-by-1 matrix named ‘x’ and stores the value 3 in its
element.
Example 4:
In this example x is to find the square root of 25 it creates a 1-by-1 matrix named ‘x’and
stores the value 5 in its element
.
Note:
• Once a variable is entered into the system, you can refer to it later.
• Variables must have values before they are used.
• When you do not specify an output variable, MATLAB uses the variable ans, short
for
answer, to store the results of your calculation.
Example 5:
In the above example we have multiple assignments