Title: = MATLAB
1 MATLAB
Mathematics Laboratory
MATLAB
Civil Engineering
Dan Ophir, Ph.D. Cell. 052-551359 e-Mail
comp_ophir_at_yahoo.com
2Requirements
- Tests
- Intermediate,
- Final.
75 Lectures 25 Exercises
Note Tests Exercises
Exercises in diskettes 1. .rtf format
(Office) 2. .m, .mdl, .fig (Matlab)
3Students
8
7
6
5
4
3
2
1
4Files
Users
- .m --- Script
- .mat --- Workspace
- .mdl --- Block program Model (Simulink)
5Conventions
- gt - pressing on a menu, sub-menu, button
- blue link
- red a reserved word.
6Mode of Interaction
Interactive Batch Script Program
Interpreter Compiler
7MATLAB - Potential
Type Demo
8Demo
MATLABgtMatricesgtgraphs and matrices MATLABgtNumeric
sgt2D-solutions MATLABgtNumericsgtCommand line
demosgtRun Command line demos gtfunction
plot MATLABgtVisualizationgt3D plots of comples
functionsgtRun 3D plots ofgt Cube root
9Demo (2-cont.)
MATLABgtLanguage/Graphicsgt3d surface plotsgt Run 3D
surface plotsgtcolor map
10Work Environment
Files
Work- Space
Desktop
Windows
11Windows
- Systems
- Commands (history)
- Variables
- Help Window
- Launch Pad
- Workspace (variables)
- Array Editor
- Users
- Script .m
- Module .mdl
- Figure .fig
12Getting Starting
MATLAB Command Window
- First commands
- helpwin list of all .m files (scripts) included
in the path (described in a help window) gt tips
(gtSee also, gtBack, gtForward) - 2. help - a help in the command window.
- 3. help debug a help of special topics or
command. - lookfor conversion
- which fprintf
13Interactive Calculation
Command window x5 yx2 disp(y)
files .mat for workspace saving. MATLAB.mat
default file name
use save / load to treat with workspace file
who the local variables values whos the local
variables data structure
14M-File Editor/Debugger window
Input
1. Command window setpath 2. Command window
open fibonaci.m file 3. Command window fibonaci
Output
1. Command window Fibonacci series 2. Figure No.
1
15Fibonnacci
1 1 2 3 5 8 13 21
Converges to the golden ratio.
AB CB CB AC
A
B
C
16Golden Ratio
Parthenon
17Simulink window
Modeling Programming
- First step
- menufilegtnewgtmodel
- or gtopengtbrowsing .mdl
- gives the model specifications.
- 3) model - icon
- Second step
- menu filegtopen
- Browsing for .mdl
- Third step (Integral window)
- menu simulationgtstart
- Input Integration windindows
C\MATLAB_5\my_examples\mdl\MDL\Simple\INTEGRAL.MD
L
18MATLAB as a Language
Language Syntax, Semantics
19Language Components
script function f(a,b) command
xy5 variable myBook_1 operator
constant 3.27 special symbol , ( )
20Operations (operators)
operation symbol example
Addition 32
Subtraction - 54.4 25.3
Multiplication 3.15 6
Division / or \ 23.4/8
Exponentiation 34
helpwingtmatlab\opsgt
21Variables
- Naming rules
- letters, digits, _
- Case sensitive
- Starting with a letter
Reserved World List (17) for end if while
function return elseif case otherwise switch
continue else try catch global persistent break
22MATLAB specialvariables
Special Variables
23Control Flow keywords
if - Conditionally execute
statements. else - IF statement
condition. elseif - IF statement
condition. end - Terminate scope of
FOR, WHILE, SWITCH and IF statements. for
- Repeat statements a specific number of
times. while - Repeat statements an
indefinite number of times. break -
Terminate execution of WHILE or FOR loop.
helpgtmanualgtcontrol flow helpgtelse
24Functions Library
Trigonometric sin, cot Exponential log, exp,
, sqrt Complex conj, real Rounding and
remainder fix, floor, mod Coordinate
Transformation carth2sph,pol2cart Number
Theoretic factor, isprime, gcd Specialized
besselj, legendre
helpwingt matlab\elfun (topics elem. func) gt tan
25Function Syntax
function list of output arguments name
(input-arg.) commands
Example function mean avg(x,n)
mean sum(x)/n
26Command
Command collection of the following
entities variables, constants, calling
functions, keywords (reserved words) and
operators.
Examples x2310sin(0.3)
x
27Program
Program A collection of commands with some
significance.
gtgt fibonnaci - sequence gtgt b(1)1 b(2)1 gtgt
for I35, b(I)b(I-1)b(I-2)
end gtgt b gtgt who
28Example FOR, matrix
Output 2 3 4 5 3 4
5 6 4 5 6 7 5 6
7 8 6 7 8 9
Command gtgt for i15, for j14,
a(i,j)ij end
end gtgt a
basewords loop, matrix, index operators gtgt ,
()
column
row
29Simple for, if
Code
Output
for I15 I end
1 2 3 4 5
I1 J2 X5 X6 if x5 I else J end
1 2
30FAQ
Frequently Asked Questions ????? ??????
gtgt If for
Previous command
31Language Manual
MANUAL.M
MANUAL.M
help manual
32Script, Program, Function
Script, Program, Function A collection of
commands enclosed between two special commands
(module) and having a special task.
33Exercise 1(list)
Choose 8 Reserved words and explain their
function in MATLAB and give examples of their
usage. Use the help features of MATLAB and the
manual.m file.
Choose 5 operators not given in the lecture and
explain their function
34Exercise 2 (Pitagoras)
Input Two perpendicular sides a,b of a
right-angled triangle, Output The length of the
opposite side c. Do To write commands to compute
c.
c2 a2 b2
c
b
a
35Simple Script Sum_Arr
1. a1 2 3 2. n3 3. function fsum(a,n) 4.
Computing the sum of the elements of an
array 5. sum10 6. for i1n, 7.
sum1sum1a(i) 8. end 9. fsum1 10.
ssprintf('sumd',sum1) 11. disp(s)
36Fibonacci Script (recursive)
1. fibonaci.m (recusive algorithm) 2. A script
to calculate Fibonacci numbers 3. first
described by Leonardo of Pisa 4. f1 1 n1 5.
while f(n) f(n1) lt100 6. f(n2)f(n)f(n1)
7. nn1 8. end Â
37Exercise 3 (primes)
- Write a script/function myPrime to print all the
prime numbers less than n - Take n100.
- Compare the results with those received by using
the MATLAB original function.
38Line 2D
39Exercise 4 Triangle
Draw (in the .m file) a triangle ,whose vertices
are at the points (5,5), (0,10), (10,10)
???? (???? .m) ????? ????????? ??????? ?????
(5,5),(0,10),(10,10)