Matlab: BY K. Kalyan Kumar
Matlab: BY K. Kalyan Kumar
BY K. KALYAN KUMAR
Strengths of MATLAB
2
quick when performing matrix operations MATLAB may behave like a calculator or as a programming language MATLAB is interpreted, errors are easier to fix
MATLAB Desktop
3
Command Window type commands Workspace view program variables clear to clear
clear all: removes all variables, globals, functions and MEX links clc: clear command window
Command window
5
somewhat like UNIX. A prompt(>>) appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.
a=5;
b=a/2
b=
2.5000
2*5
ans= 10
statement. To place multiple commands in a single line, they should be separated by commas or semicolons. Commas tell MATLAB to display results; semicolons suppress printing. Statement continuation is done by three periods in succession. Ex: pens=5,pencost=10; total_cost=pens* pencost
of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores. Punctuation characters are not allowed.
Reserved Words
9
Value of
Infinity Not a number e.g. 0/0
i = j = square root of minus one: (-1) (imaginary number) e.g. sqrt(-1) ans= 0 + 1.0000i The smallest usable positive real number
The largest usable positive real number
realmin realmax
^ or .^ a^b or a.^b * (matrix multiply) or .* (array multiply) a*b or a.*b / or ./ a/b or a./b \ or .\ b\a or b.\a 56/8 = 8\56 + a + b a - b = a = b (assign b to a)
Script M-files:
12
13
your disk by choosing Save from the File menu; then at the MATLAB prompt typing the name of the file without .m extension and press enter key
Function
Beep disp(variablename) Input
Description
Makes computer beep displays results without
keyboard
pause(n)
Arrays :
16
counting by 1, ending at or before last X=first:increment:last creates row vector starting with first, counting by increment, ending at or before last X=linspace(first,last,n) creates linearly spaced row vector starting with first, ending at last having n elements
Examples:
17
>> x=[1 6 8]
x=
>> x=2:5 x= 2 3 4 5 >> x=2:0.5:5 x= 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 >> x=linspace(1,10,10) x= 1 2 3 4 5 6 7 8 9 10 >> x=linspace(0,10,10) x= Columns 1 through 9 0 1.1111 2.2222 3.3333 4.4444 5.5556 6.6667 7.7778 8.8889 Column 10 10.0000
Dot-transpose operation:
18
>> A=[1 2 3 4]; >> B=complex(A,A)
B=
1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i >> C=B' C=
Standard Arrays:
19
20
>>A=rand(3) A= 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218 0.9706 0.8003 0.9157 >> diag(A) ans = 0.9649 0.4854 0.9157
Array operations:
21
>> A=[1 2 3; 4 5 6] A= 1 2 3 4 5 6 >> B=[4 5 6;3 2 1] B= 4 5 6 3 2 1 >> A+B ans = 5 7 9 7 7 7 >> A*B ??? Error using ==> mtimes Inner matrix dimensions must agree. >> A.*B ans = 4 10 18 12 10 6
Less Than Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To
~ & | && ||
Matlab evaluates expression as logical true or false false equivalent to zero true equivalent to any non-zero number
loop stops when index > end (or index < end)
26
Function files:
27
the name of the function file must be the same name as the function
28
>> [a,v]=rectangle123(3,4,6) a= 12 v= 72
29
30