Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
S YADAV
Programming Basics
To Start Matlab On Microsoft Windows platforms, double-clicking the MATLAB shortcut on your Windows desktop.
On UNIX platforms, start MATLAB by typing matlab at the operating system prompt. Quitting the MATLAB Program To end your MATLAB session, select File > Exit MATLAB in the desktop, or type quit in the Command Window S YADAV
Programming in Matlab
S YADAV
Basics of Matlab
Interactive mode In interactive mode, commands are typed (or cut-and-pasted) into the 'command window'. 3+4 ans = 7
Batch mode. In batch mode, a series of commands are saved in a text file (either using Matlab's built-in editor, or another text editor ) with a '.m' extension. The batch commands in a file are then executed by typing the name of the file at the Matlab command prompt.
S YADAV
Example Scripts
Function
%Name of function is sum1 function c=sum1(a,b) c=a+b end M file names should be sum1.m
S YADAV
Variables
As in programming languages, the MATLAB language provides mathematical expressions, but unlike most programming languages, these expressions involve entire matrices. MATLAB does not require any type declarations or dimension statements. When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. For example, num_students = 25 creates a 1-by-1 matrix named num_students and stores the value 25 in its single element. To view the matrix assigned to any variable, simply enter the variable name. Variable names consist of a letter, followed by any number of letters, digits, or underscores.(max length of variable is 63) MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. B and b are not the same variable.
S YADAV
S YADAV
MATRIX
*MATLAB IS MATRIX MANIPULATION LANGUAGE.
1
6
2
7
3
8
ACCESSING SUBMATRICES
A= 11 12 13 14 15 16
17
23
18
24
19 20 21
25 12 26 13 27 14
22
28 15 16]
A(1,:) = [11
A(1,1:3)=[11
12 13 ];
i.e. FIRST ROW AND ONE TO THREE COLUMNS A(2,3:6)=[19 20 21 22 ]; i.e second row , third to sixth column.
S YADAV
11
A=1:2:9
A=
1 3 5 7 9
A=[5:-1:-5] A= 5 13 4 3 2 1 0 -1 -2 -3 -4 -5
S YADAV
eyes(n) will produce nxn identity matrix eyes(m,n) will produce mxn identity matrix ones(n) will produce nxn matrix of ones. ones(m,n) will produce mxn matrix of ones. zeros(m,n) will produce matrix of zeros rand(m,n) will produce mxn matrix of randomvalue triu(X) will extract upper triangular part of matrix X. tril(X) will extract lower triangular part of matrix X.
14
S YADAV
MATRIX OPERATIONS
+ * ^ . \ / ADDITION SUBTRACTION MULTIPLICATION POWER CONJUGATE TRANSPOSE TRANSPOSE LEFT DIVISION RIGHT DIVISION
THESE MATRIX OPERATIONS APPLY TO SCALARS AS WELL. IF THE SIZES OF MATRICES ARE INCOMPATIBLE FOR THE MATRIX OPERATION, AN ERROR MESSAGE WILL RESULT. 15
S YADAV
EXAMPLE OF MULTIPLICATION
A=
1 2 2 3
B=
1 1 1 1
3
5 16
3
5
S YADAV
ENTRY-WISE OPERATIONS
OPERATIONS OF ADDITION AND SUBTRACTION ALREADY OPERATE ENTRY WISE A= B= 1 2 2 1 1 1 1 1
A+B
2 3 3 2
17
S YADAV
.* , .^ , ./ ,.\
A=[1 2 3 4]
B=[1 2 3 4]
A.*B=[1 4 9 16] A=[4 6 8 10] B=[2 2 2 2] A./B=[2 3 4 5] A=[1 2 3] A.^3=[1 8 27] 18
S YADAV
MATRIX DIVISION
IF A IS AN INVERTIBLE MATRIX AND b IS A COMPATIBLE COLUMN VECTOR, then
x=A\b is the solution of A*x=b
[1 2 3;4 5 6; 8 9 7]*[x;y;z]=[1;2;3]
[x;y;z]=[1 2 3;4 5 6;8 9 7]\[1;2;3]
IF A IS AN INVERTIBLE MATRIX AND b IS A COMPATIBLE ROW VECTOR, then x=b/A is the solution of x*A=b [x y z]*[1 2 3;4 5 6; 8 9 7]=[1 1 3] [x y z]=[1 1 3]/[1 2 3;4 5 6;8 9 7] 19
S YADAV
Statement is terminated with ; If it is not terminated with ; , result will be displayed on screen. Statements can be placed on same line if they are terminated with ; Single statement can be continued to next line with three or more periods e.g. y=x*x+ .. 2*x+3;
Continued ..
MATLAB is case sensitive. who or whos will list variables in current workspace. inmem lists compiled m files in current memory.
21
S YADAV
Continued..
VARIABLE OR FUNCTION CAN BE CLEARED FROM WORKSPACE clear variablename clear functionname clear WILL CLEAR ALL NON PERMANENT VARIABLES. ON LOGOUT ALL VARIABLES ARE LOST save WILL SAVE ALL VARIABLES IN FILE matlab.mat load WILL RESTORE WORKSPACE TO ITS FORMAL STATE. save and load TAKE VARIABLE NAME AND FILENAME AS OPTIONAL ARGUMENTS. edit fun opens the file fun.m in a text editor.
22
S YADAV
23
S YADAV
RELATIONAL OPERATORS
< LESS THAN > GREATER THAN <= LESS THAN OR EQUAL >= GREATER THAN OR EQUAL == EQUAL ~= NOT EQUAL NOTE: = IS USED IN ASSIGNMENT AND = = IS USED IN A RELATION
LOGICAL OPERATORS & AND | OR ~ NOT RELATIONS MAY BE CONNECTED BY LOGICAL OPERATORS
24
S YADAV
S YADAV
Control statement
If ,if else For loop While loop Switch case Brake ,continue etc.
26
S YADAV
IF
price=4500; if price >5000, disp('PRICE IS MORE THAN 5000'); end
27
S YADAV
IF ELSE
else
disp(PRICE IS NOT MORE THAN 5000); end
28
S YADAV
IF ELSEIF
price=4500; if price >5000, disp('PRICE IS MORE THAN 5000');
29
S YADAV
WHILE LOOP
var=20; while var>0,
disp(var);
var=var-1;
end
disp('variable is zero now'); disp(var);
30
S YADAV
FOR LOOP
for i=1:10,
disp(i); end for i=1:2:11, disp(i)
end
31
S YADAV
n=3;
for i=1:n,
for j=1:n,
a(i,j)=5;
end
end
disp(a);
32
S YADAV
SWITCH CASE
var1=10;
var2=5; switch operation case 'add' output=var1+var2; disp(output); case {'multiply','product'} output=var1*var2;
disp(output);
33
S YADAV
CONTINUED .
case {'subtract','sub'}
output=var1-var2; disp(output); case 'divide' output=var1/var2; disp(output); otherwise
CONTINUED.
case {'subtract','sub'}
output=var1-var2; disp(output); case 'divide' output=var1/var2; disp(output); otherwise
BREAK STATEMENT
var=20;
while var>0, disp(var); if var==10 break; end var=var-1; end
FUNCTION IN MATLAB
37
S YADAV
SCALAR FUNCTIONS
Operate essentially on scalars. Operate element-wise when applied to a matrix. sin asin abs cos log10 log (natural log) floor tan rem (remainder) sign round A=sin(1) A= 0.8415 A=sin([1 1.2 1.3 1.4]) A= 0.8415 0.9320 0.9636 0.9854
38
S YADAV
VECTOR FUNCTIONS
OPERATE ESSENTIALY ON VECTOR (ROW OR COLUMN) WHEN APPLIED TO MxN MATRIX, OPERATE COLUMN BY COLUMN TO PRODUCE ROW VECTOR CONTAINING RESULT OF APPLICATION TO EACH COLUMN.
S YADAV
MATRIX FUNCTIONS
eigenvalues and eigenvectors cholesky factorization singular value decomposition inverse LU factorization QR factorization reduced row echelon form matrix exponential
S YADAV
STRING FUNCTIONS
strcmp(str1,str2) strncmp(str1,str2,n) strcat(str1,str2) str2num(str) str2double(str) num2str(num) CAT(DIM,A,B) concatenates the arrays A and B along the dimension DIM. CAT(2,A,B) is the same as [A,B]. CAT(1,A,B) is the same as [A;B].
41
S YADAV
WRITING FUNCTION
return;
42
S YADAV
FUNCTION CONTINUED function result=perform(operation ,var1,var2) switch operation case 'multiply' result=var1*var2; case 'add' result=var1+var2; case 'subtract' result=var1-var2; case 'divide' result=var1/var2; otherwise disp('Only multilply, add,subtract and divide operations are allowed'); result='error'; 43 S YADAV end
INPUT
name=input('Please enter your name : ','s');
fprintf('\nHello %s !\n',name);
: ');
FORMATED OUTPUT
name=svits';
age=06; salary=1800000; fprintf('\n Institute Name : %s\t Age : %d . \t Salary : Rs %0.2f',name,age,salary); fprintf('\n I am writing 123456 in exponential . form : %e',12356);
Thank you
46
S YADAV