1
= MATLABMathematics + Laboratory
+
Dan Ophir, Ph.D.
Cell.: 052-551359
e-Mail: [email protected]
Civil Engineering
=
2
Requirements
Tests:
1. Intermediate,
2. Final.
Exercises in diskettes
1. .rtf format (Office);
2. .m, .mdl, .fig (Matlab)
50% Lectures + 40% Exercises
Note = Tests + Exercises
8
Demo
MATLAB>Matrices>graphs and matrices
MATLAB>Numerics>2D-solutions
MATLAB>Numerics>Command line demos>Run Command line demos >function plot
MATLAB>Visualization>3D plots of comples functions>Run 3D plots of> Cube root
11
Windows
User’s:
1. Script .m
2. Module .mdl
3. Figure .fig
System’s:
1. Commands (history)
2. Variables
3. Help Window
4. Launch Pad
5. Workspace (variables)
6. Array Editor
12
Getting Starting
MATLAB: Command Window
First commands:
1. helpwin – list of all .m files (scripts) included in the path (described in a help window) > tips (>See also, >Back, >Forward)
2. help - a help in the command window.
3. help debug – a help of special topics or command.
lookfor conversion
which fprintf
13
Interactive Calculation
Command window:
x=5;
y=x+2;
disp(y);
files .mat for workspace saving.
MATLAB.mat – default file name
who – the local variables values
whos – the local variables data structure
use: save / load –
to treat with workspace file
14
M-File Editor/Debugger window
1. Command window: setpath
2. Command window: open fibonaci.m file
3. Command window: fibonaci
Input:
Output:
1. Command window: Fibonacci series
2. Figure No. 1
17
Simulink window
First step:
1) menu:file>new>model
2) or: >open>browsing .mdl
gives the model specifications.
3) model - icon
Second step:
1) menu: file>open
2) Browsing for .mdl
C:\MATLAB_5\my_examples\mdl\MDL\Simple\INTEGRAL.MDL
Modeling Programming
Third step: (Integral window)
1. menu: simulation>start
2. Input & Integration windindows
19
Language Components
script – function: f(a,b)
command: x=y+5
variable: myBook_1
operator: + *
constant: 3.27
special symbol: % , ; “ ( )
20
Operations (operators)
operation symbol example
Addition + 3+2
Subtraction - 54.4 –25.3
Multiplication * 3.15 * 6
Division / or \ 23.4/8
Exponentiation ^ 3^4
helpwin>matlab\ops>*
21
Variables
Naming rules:
1. letters, digits, _
2. Case sensitive
3. 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
23
Control 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.
help>manual>control flow
help>else
24
Functions 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
helpwin> matlab\elfun (topics elem. func) > tan
25
Function Syntax
Example:
function [mean] = avg(x,n)
mean = sum(x)/n;
function [list of output arguments ] = name (input-arg.)
commands…
26
Command
Command: collection of the following entities:
variables, constants, calling functions, keywords (reserved words) and operators.
Examples: x=23+10*sin(0.3);
x
27
Program
Program: A collection of commands with some significance.
>> % fibonnaci - sequence
>> b(1)=1; b(2)=1;
>> for I=3:5,
b(I)=b(I-1)+b(I-2);
end
>> b
>> who
28
Example: FOR, matrix
Command:
>> for i=1:5,
for j=1:4,
a(i,j)=i+j;
end
end
>> a
Output:
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
basewords:
loop, matrix, index
operators: >> : , () ;row column
29
Simple for, if
for I=1:5
I
end
1 2 3 4 5
Code: Output:
I=1; J=2; X=5; X=6;
if x==5
I
else
J
end
1
2
32
Script, Program, Function
Script, Program, Function :
A collection of commands enclosed between two special commands (module) and having a special task.
33
example 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
34
example 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.
b
a
c
c2 = a2 + b2
35
Simple Script – Sum_Arr
1. a=[1 2 3];
2. n=3;
3. % function f=sum(a,n)
4. % Computing the sum of the elements of an array
5. sum1=0;
6. for i=1:n,
7. sum1=sum1+a(i);
8. end
9. f=sum1;
10. s=sprintf('sum:%d',sum1);
11. disp(s);
36
Fibonacci Script (recursive)
1. % fibonaci.m (recusive algorithm)
2. %A script to calculate Fibonacci numbers
3. %first described by Leonardo of Pisa
4. f=[1 1]; n=1;
5. while f(n) + f(n+1) <100
6. f(n+2)=f(n)+f(n+1)
7. n=n+1;
8. end
37
example 3 (primes)
1. Write a script/function myPrime to print all the prime numbers less than n;
2. Take n=100.
3. Compare the results with those received by using the MATLAB original function.