1 / 39

= MATLAB

= 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

gail
Download Presentation

= MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. = MATLAB Mathematics + Laboratory = + MATLAB Civil Engineering Dan Ophir, Ph.D. Cell.: 052-551359 e-Mail: comp_ophir@yahoo.com

  2. Requirements • Tests: • Intermediate, • Final. 75% Lectures + 25% Exercises Note = Tests + Exercises Exercises in diskettes 1. .rtf format (Office); 2. .m, .mdl, .fig (Matlab)

  3. Students

  4. Files User’s: • .m --- Script • .mat --- Workspace • .mdl --- Block program – Model (Simulink)

  5. Conventions • > - pressing on a menu, sub-menu, button • blue – link • red – a reserved word.

  6. Mode of Interaction Interactive Batch: Script Program Interpreter Compiler

  7. MATLAB - Potential Type: Demo

  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

  9. Demo (2-cont.) MATLAB>Language/Graphics>3d surface plots> Run 3D surface plots>color map

  10. Work Environment Work- Space Files Desktop Windows

  11. Windows • System’s: • Commands (history) • Variables • Help Window • Launch Pad • Workspace (variables) • Array Editor • User’s: • Script .m • Module .mdl • Figure .fig

  12. 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

  13. 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

  14. 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

  15. Fibonnacci 1 1 2 3 5 8 13 21… Converges to the golden ratio. AB : CB = CB : AC A B C

  16. Golden Ratio Parthenon

  17. 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

  18. MATLAB as a Language Language : Syntax, Semantics

  19. Language Components script – function: f(a,b) command: x=y+5 variable: myBook_1 operator: + * constant: 3.27 special symbol: % , ; “ ( )

  20. Operations (operators) helpwin>matlab\ops>*

  21. 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

  22. Special Variables MATLAB specialvariables

  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 function [list of output arguments ] = name (input-arg.) commands… Example: function [mean] = avg(x,n) mean = sum(x)/n;

  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 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

  29. 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

  30. FAQ Frequently Asked Questions שאלות שכיחות >> If for … Previous command

  31. Language Manual MANUAL.M MANUAL.M help manual

  32. Script, Program, Function Script, Program, Function : A collection of commands enclosed between two special commands (module) and having a special task.

  33. 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

  34. 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

  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. 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.

  38. Line 2D

  39. 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)

More Related