Shivam Mishra (1508260) Shashikant Kumar (1508259) Rajinish Kumar (1508253)
Shivam Mishra (1508260) Shashikant Kumar (1508259) Rajinish Kumar (1508253)
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:
Matlab is case sensitive, i.e. var1 & VAR1 are different variables.
Assignment statement:
Variable = number;
Variable = expression;
Example:
>> tutorial = 1234;
>> tutorial = 1234
tutorial =
1234
ARRAY
Like other programming languages, arrays are an important
part of MATLAB
COLUMN VECTOR
»column = [4;2;7;4]
The difference between a row and a column vector can get
by:
>>size(row) >>size(column)
Element by element
» a= [1 2;3 4];
OPERATIONS
Arithmetic Operators: + - * / \ ^ ‘
Colon: (:)
Addition and subtraction are element-wise “sizes must match”:
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
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