410 likes | 710 Views
= MATLAB. Mat hematics + Lab oratory. =. +. MATLAB. Civil Engineering. Dan Ophir, Ph.D. Cell.: 052-551359 e-Mail: comp_ophir@yahoo.com. Requirements. Tests: Intermediate, Final. 75% Lectures + 25% Exercises. Note = Tests + Exercises. Exercises in diskettes
E N D
= MATLAB Mathematics + Laboratory = + MATLAB Civil Engineering Dan Ophir, Ph.D. Cell.: 052-551359 e-Mail: comp_ophir@yahoo.com
Requirements • Tests: • Intermediate, • Final. 75% Lectures + 25% Exercises Note = Tests + Exercises Exercises in diskettes 1. .rtf format (Office); 2. .m, .mdl, .fig (Matlab)
Files User’s: • .m --- Script • .mat --- Workspace • .mdl --- Block program – Model (Simulink)
Conventions • > - pressing on a menu, sub-menu, button • blue – link • red – a reserved word.
Mode of Interaction Interactive Batch: Script Program Interpreter Compiler
MATLAB - Potential Type: Demo
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
Demo (2-cont.) MATLAB>Language/Graphics>3d surface plots> Run 3D surface plots>color map
Work Environment Work- Space Files Desktop Windows
Windows • System’s: • Commands (history) • Variables • Help Window • Launch Pad • Workspace (variables) • Array Editor • User’s: • Script .m • Module .mdl • Figure .fig
Getting Starting MATLAB: Command Window • First commands: • 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
Interactive Calculation Command window: x=5; y=x+2; disp(y); files .matfor 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
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
Fibonnacci 1 1 2 3 5 8 13 21… Converges to the golden ratio. AB : CB = CB : AC A B C
Golden Ratio Parthenon
Simulink window Modeling Programming • First step: • menu:file>new>model • or: >open>browsing .mdl • gives the model specifications. • 3) model - icon • Second step: • menu: file>open • Browsing for .mdl • Third step: (Integral window) • menu: simulation>start • Input & Integration windindows C:\MATLAB_5\my_examples\mdl\MDL\Simple\INTEGRAL.MDL
MATLAB as a Language Language : Syntax, Semantics
Language Components script – function: f(a,b) command: x=y+5 variable: myBook_1 operator: + * constant: 3.27 special symbol: % , ; “ ( )
Operations (operators) helpwin>matlab\ops>*
Variables • 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
Special Variables MATLAB specialvariables
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
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
Function Syntax function [list of output arguments ] = name (input-arg.) commands… Example: function [mean] = avg(x,n) mean = sum(x)/n;
Command Command: collection of the following entities: variables, constants, calling functions, keywords (reserved words) and operators. Examples: x=23+10*sin(0.3); x
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
Example: FOR, matrix Output: 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8 6 7 8 9 Command: >> for i=1:5, for j=1:4, a(i,j)=i+j; end end >> a basewords: loop, matrix, index operators: >> : , () ; column row
Simple for, if Code: Output: for I=1:5 I end 1 2 3 4 5 I=1; J=2; X=5; X=6; if x==5 I else J end 1 2
FAQ Frequently Asked Questions שאלות שכיחות >> If for … Previous command
Language Manual MANUAL.M MANUAL.M help manual
Script, Program, Function Script, Program, Function : A collection of commands enclosed between two special commands (module) and having a special task.
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
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. c2 = a2 + b2 c b 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. 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);
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
Exercise 3 (primes) • Write a script/function myPrime to print all the prime numbers less than n; • Take n=100. • Compare the results with those received by using the MATLAB original function.
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)