Mathematics Laboratory Dan Ophir Ph D MATLAB Civil
Mathematics + Laboratory + Dan Ophir, Ph. D. = MATLAB = Civil Engineering Cell. : 052 -551359 e-Mail: comp_ophir@yahoo. com 1
Requirements Tests: 1. Intermediate, 2. Final. 75% Lectures + 25% Exercises Note = Tests + Exercises in diskettes 1. . rtf format (Office); 2. . m, . mdl, . fig (Matlab) 2
Students 8 7 6 5 4 3 2 1 3
Files User’s: 1. . m --- Script 2. . mat --- Workspace 3. . mdl --- Block program – Model (Simulink) 4
Conventions 1) > - pressing on a menu, submenu, button 2) blue – link 3) red – a reserved word. 5
Mode of Interaction Interactive Batch: Script Program Interpreter Compiler 6
MATLAB - Potential Type: Demo 7
Demo MATLAB>Matrices>graphs and matrices MATLAB>Numerics>2 D-solutions MATLAB>Numerics>Command line demos>Run Command line demos >function plot MATLAB>Visualization>3 D plots of comples functions>Run 3 D plots of> Cube root 8
Demo (2 -cont. ) MATLAB>Language/Graphics>3 d surface plots> Run 3 D surface plots>color map 9
Work Environment Work- Space Desktop Files Windows 10
Windows System’s: User’s: 1. Commands (history) 1. Script. m 2. Variables 2. Module. mdl 3. Help Window 3. Figure. fig 4. Launch Pad 5. Workspace (variables) 6. Array Editor 11
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 12
Interactive Calculation Command window: files. mat for workspace saving. x=5; MATLAB. mat – default file name y=x+2; disp(y); use: save / load – to treat with workspace file who – the local variables values whos – the local variables data structure 13
M-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 14
Fibonnacci 1 1 2 3 5 8 13 21… Converges to the golden ratio. AB : CB = CB : AC A C B 15
Golden Ratio Parthenon 16
Simulink window Modeling Programming First step: Second step: 1) menu: file>new>model 1) menu: file>open 2) or: 2) Browsing for. mdl >open>browsing. mdl 3) gives the model specifications. 4) 3) model - icon Third step: (Integral window) 1. menu: simulation>start 2. Input & Integration windindows C: MATLAB_5my_exampl esmdlMDLSimpleINTEG RAL. MDL 17
MATLAB as a Language : Syntax, Semantics 18
Language Components script – function: f(a, b) command: x=y+5 variable: my. Book_1 operator: + * constant: 3. 27 special symbol: % , ; “ ( ) 19
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>matlabops>* 20
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 21
Special Variables MATLAB special variables 22
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 times. - Repeat statements an indefinite number of % break - Terminate execution of WHILE or FOR loop. help>manual>control flow help>else 23
Functions Library Trigonometric : sin, cot Exponential : log, exp, ^, sqrt Complex : conj, real Rounding and remainder: fix, floor, mod Coordinate Transformation: carth 2 sph, pol 2 cart Number Theoretic: factor, isprime, gcd Specialized: besselj, legendre helpwin> matlabelfun (topics elem. func) > tan 24
Function Syntax function [list of output arguments ] = name (input-arg. ) commands… Example: function [mean] = avg(x, n) mean = sum(x)/n; 25
Command: collection of the following entities: variables, constants, calling functions, keywords (reserved words) and operators. Examples: x=23+10*sin(0. 3); x 26
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 27
Example: FOR, matrix Output: Command: >> for i=1: 5, for j=1: 4, a(i, j)=i+j; end 3 4 5 6 7 8 6 7 8 9 basewords: >> a row 2 loop, matrix, index column operators: >> : , () ; 28
Simple for, if Code: for I=1: 5 I Output: 12345 end I=1; J=2; X=5; X=6; if x==5 I 1 2 else J end 29
FAQ Frequently Asked Questions שאלות שכיחות >> If for … Previous command 30
Language Manual MANUAL. M help manual 31
Script, Program, Function : A collection of commands enclosed between two special commands (module) and having a special task. 32
Exercise 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 33
Exercise 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. c 2 = a 2 + b 2 c b 34 a
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. sum 1=0; 6. for i=1: n, 7. sum 1=sum 1+a(i); 8. end 9. f=sum 1; 10. s=sprintf('sum: %d', sum 1); 11. disp(s); 35
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. 7. f(n+2)=f(n)+f(n+1) n=n+1; 8. end 36
Exercise 3 (primes) 1. Write a script/function my. Prime 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. 37
Line 2 D 38
Exercise 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) 39
- Slides: 39