Submitted by:
Shivam Mishra (1508260)
Shashikant Kumar (1508259)
Rajinish Kumar (1508253)
FILE TYPES
MATLAB has three types of files for storing information:
M - FILES
M-files are standard ASCII text files, with a .m extension to the filename.
There are two types of these files: script files and function files
Most programs you write in MATLAB are saved as M-files. All built-in functions in MATLAB are M-
files, most of which reside on your computer in precompiled format.
MAT FILES
Mat-files are binary data-files, with a .mat extension to the filename.
These are created by MATLAB when you save data with the save command.
The data is written in a special format that only MATLAB can read.
MEX FILES
Mex-files are MATLAB-callable Fortran and C programs, with a .mex extension to the filename.
Use of these files requires some experience with MATLAB
DISPLAY WINDOW
MATLAB VARIABLES
Variable names:
Must start with a letter.
May contain only letters, digits, and the underscore.
Matlab is case sensitive, i.e. var1 & VAR1 are different variables.
Assignment statement:
Variable = number;
Variable = expression;
To check the Max length supported by current MATLAB version “namelengthmax”
MATLAB is a weakly typed language No need to declare variables!
VARIABLES
Variable can’t have the same name of keyword
Use “iskeyword” to list all keywords
Built-in variables. Don’t use these names!
i and j can be used to indicate complex numbers
Pi has the value 3.1415
ans stores the last unassigned value (like on a calculator)
Inf and –Inf are positive and negative infinity
NaN represents ‘Not a Number’
Example:
>> tutorial = 1234;
>> tutorial = 1234
tutorial =
1234
ARRAY
Like other programming languages, arrays are an important
part of MATLAB
Two types of arrays:
1. Matrix of numbers (either double or complex)
2. Cell array of objects (more advanced data structure)
VECTOR
ROW VECTOR
comma or space separated values between brackets
»row = [1 2 5.4 -6.6]
»row = [1, 2, 5.4, -6.6];
COLUMN VECTOR
Semicolon separated values between brackets
»column = [4;2;7;4]
The difference between a row and a column vector can get
by:
Looking in the workspace
Displaying the variable in the command window
Using the size function
>>size(row) >>size(column)
To get a vector's length, use the length function
>>length(row) >>length(column)
MATRIX
Make matrices like vectors
Element by element
» a= [1 2;3 4];
OPERATIONS
Arithmetic Operators: + - * / \ ^ ‘
Relational Operators: < > <= >= == ~=
Logical Operators: Element wise & | ~
Colon: (:)
Addition and subtraction are element-wise “sizes must match”:
All the functions that work on scalars also work on vectors
»t = [1 2 3];
»f = exp(t);
is the same as
»f = [exp(1) exp(2) exp(3)];
INDEXING
MATLAB indexing starts with 1, not 0
a(n) returns the nth element
The index argument can be a vector. In this case, each element
is looked up individually, and returned as a vector of the same
size as the index vector.
INDEXING
To select rows or columns of a matrix:
MATLAB SCRIPTS
Scripts are collection of commands
executed in sequence
written in the MATLAB editor
saved as MATLAB files (.m extension)
To create an MATLAB file from command-line
»edit helloWorld.m.
or click
MATLAB SCRIPTS
Click on New Creates Blank
Script Script File
MATLAB SCRIPTS
COMMENT!
Anything following a % is seen as a comment
The first contiguous comment becomes the script's help file
Comment thoroughly to avoid wasting time later
All variables created and modified in a script exist in the
workspace even after it has stopped running
EXAMPLE TO WRITE A SCRIPT FILE
% To compute area and circumference of a circle
% File written by Shivam Mishra. Last modified 10/02/19
% -------------------------
Radius = 4;
Area = pi*radius^2;
disp(‘area=‘); disp(area);
Circum = 2*pi*Radius;
disp(‘circum=‘); disp(circum);
EXAMPLE
Solutions to Systems of Linear Equations
Question
A system of 3 linear equations with 3 unknowns (x1, x2, x3):
3x1 + 2x2 – x3 = 10
-x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
SOLUTION
Let :
3 2 1 x1 10
A 1 3 2 x x2 b 5
1 1 1 x3 1
Then, the system can be described as:
Ax = b
Solution by Matrix Inverse: • Solution by Matrix Division:
Ax = b The solution to the equation
A-1Ax = A-1b Ax = b
x = A-1b can be computed using left division.
MATLAB: MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1]; >> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1]; >> b = [ 10; 5; -1];
>> x = inv(A)*b >> x = A\b
x= x=
-2.0000 -2.0000
5.0000 5.0000
-6.0000 -6.0000
Answer: Answer:
x1 = -2, x2 = 5, x3 = -6 x1 = -2, x2 = 5, x3 = -6
FLOW CONTROL: If...else
Example: (if…else and elseif clauses)
if temperature > 100
disp (‘Too hot – equipment malfunctioning.’)
elseif temperature > 90
disp (‘Normal operating range.’);
else
disp (‘Too cold – turn off equipment.’)
end
FLOW CONTROL: LOOPS
•Example (for loop):
for loop for t = 1:5000
for variable = expression y(t) = sin (2*pi*t/10);
commands end
end •Example (while loop):
EPS = 1;
while loop while ( 1+EPS) >1
while expression EPS = EPS/2;
commands end
EPS = 2*EPS
end
the break statement
break – is used to terminate the execution of the loop.
SIMPLE CALCULATION IN MATLAB
OUTPUT FORMAT
Though computations inside MATLAB are performed using double
precision, the appearance of floating point numbers on the screen is controlled
by the output format in use.
There are several different screen output formats.
The following shows the printed value of 10π in 7 different formats.
format short 31.4159
format short e 3.1416e+001
format long 31.41592653589793
format long e 3.141592653589793e+001
format short g 31.416
format long g 31.4159265358979
format rat 3550/113
CALCULATION WITH VECTOR
Thank you