Documente Academic
Documente Profesional
Documente Cultură
Irfan Turk
Fatih University, 2013-14
What is MATLAB?
MATLAB stands for MATrix LABoratory.
MATLAB is a high-performance language for technical
computing.
MATLAB ENVIRONMENT
Command Window
Type commands
Current Directory
View folders and m-files
Workspace
View program variables
Command History
View past commands
Editor
Writing the code
int b;
double a;
float c;
Example:
>>1B=1;
>>_A1=-8;
unless specified.
Example:
>>x=1;
>>x1=-8;
Arrays :
Row 4
Col 1 Col 2 Col 3 Col 4 Col 5
and 1 column).
zero as in C, or in others.
1
B= 3
5
1 2
3 4
5 6
Column #
B(3)=5
C(2)=2
predictions, then test them with the isvarname, iskeyword, and which
commands.
1.
test
2. Test
3. if
4. my-book
5. my_book
6. Thisisoneeverylongnamebutisitstillallowed?
7. 1stgroup
8. group_1
9. zzaAbc
10. 34wAwy?12#
11. sin
12. log
Operators
+ addition
a+b
- subtraction
a-b
* multiplication
a*b
/ division
a/b
^ power
a^b
complex conjugate transpose a (apostrophe)
Examples:
Example 1: For a given A(3by3), and B(3by3) matrices,
Operators(Element by element)
.* multiplication
./ division
.^power
a .* b
a ./ b
a .^ b
Examples
A = [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
x = A(1,:)
y = A(3 ,:)
b = x .* y
c=x./y
d = x .^2
x=
y=
b=
c=
0.14 0.25 0.33
d=
1 2 3
7 8 9
7 16 27
Examples
Example 5(in book, it is Example 2.1-2nd question):
Volume
V=1000
Temperature
T=300
Pressure
P=100
Molecular weight MW=29
Gas Constant
R=8.314
What is mass=m=? Solve it in Matlab
(PV=nRT, and n=m/MW)
Display
format short
4 decimal digits
format long
14 decimal digits
3.145214569
format short e
1.1234e+002
format long e
format +
format rat
fractional form
355/54
format short g
123.46
format long g
3.1235468
format bank
format short eng
format long eng
Example
3.1416
3.12345678901234e+002
3.14
3.1235e+00
3.12345678901234e+000
+/-/
Script M-Files
Using command window is an easy tool for
Example
Example:6 (problem 2.20 from the book)
Create a matrix A equal to [-1/3,0,1/3,2/3], and use each
of the built-in format options below to display the
results:
format short (default format)
format long
format short e
format rat
format short g
Example
Example 7:
X=linspace(0,pi,100) % X is a vector
Y=sin(X)
% Y is a vector
plot(X,Y)
title(Plotting)
xlabel(This is x label)
ylabel(This is y label)
Example
Example 8: Plot the graphs below.
a) 0x 2*pi, 1+cos(x)
b) -2*pix pi, sin(x)-1
c) -4x 4, x^2-4