ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ Ενότητα 12: Συνοπτική Παρουσίαση Ανάπτυξης Κώδικα με το Matlab Σαμαράς Νικόλαος
Άδειες Χρήσης Το παρόν εκπαιδευτικό υλικό υπόκειται σε άδειες χρήσης Creative Commons. Για εκπαιδευτικό υλικό, όπως εικόνες, που υπόκειται σε άλλου τύπου άδειας χρήσης, η άδεια χρήσης αναφέρεται ρητώς. 2
Χρηματοδότηση Το παρόν εκπαιδευτικό υλικό έχει αναπτυχθεί στα πλαίσια του εκπαιδευτικού έργου του διδάσκοντα. Το έργο «Ανοικτά Ακαδημαϊκά Μαθήματα στο Πανεπιστήμιο Μακεδονίας» έχει χρηματοδοτήσει μόνο τη αναδιαμόρφωση του εκπαιδευτικού υλικού. Το έργο υλοποιείται στο πλαίσιο του Επιχειρησιακού Προγράμματος «Εκπαίδευση και Δια Βίου Μάθηση» και συγχρηματοδοτείται από την Ευρωπαϊκή Ένωση (Ευρωπαϊκό Κοινωνικό Ταμείο) και από εθνικούς πόρους. 3
MATLAB: Quick view (1) MATLAB is a high-performance language for technical computing. It integrates computation, visualization and programming in an easy to use environment. Typical uses include: Math and Computation Algorithm Development Data Analysis and Visualization Application Development, including GUI building, API Basic data structure: Matrix 4
MATLAB: Quick view (2) MATLAB consists of five main parts: Development Environment Mathematical Function Library. A vast collection of computational algorithms. Programming Language. Graphics Application Program Interface (API). A library that allows to write C and Fortran programs that interact with MATLAB. 5
MATLAB: Quick view (3) MATLAB supports three kinds of files: Scripts (procedure). Don t accept input arguments or return output arguments. They operate on data in the workspace. Extension: filename.m Functions. Accept input arguments and return output arguments. Extension: filename.m Workspace files. Save the workspace to a binary file which has a.mat extension. 6
MATLAB: Quick view (4) Scripts. A script m-file can be executed by simply type its name in the command window. Scripts can operate on existing data in the workspace. Script file: filename01.m m=100; n=100; rand('state',1); sprand('state'); A=sprand(m,n,0.01); nz=nnz(a); spy(a) 7
MATLAB: Quick view (5) Typing the statement filename01 in the command window 8
MATLAB: Quick view (6) Functions. 1 st line: function [output arguments] = function_name(input arguments) Script file: filename02.m function [A,nz]=filename02(m,n,seednumber,density) if nargin==3 density=0.01 end rand('state',seednumber); sprand('state'); A=sprand(m,n,density); nz=nnz(a); spy(a) 9
MATLAB: Quick view (7) Typing the statement [A,nz]=filename01(1000,1000,2) 0 200 400 600 800 1000 1200 1400 1600 1800 2000 0 500 1000 nz = 39792 1500 2000 10
MATLAB: Quick view (8) Data Types 11
MATLAB: Quick view (9) Arithmetic Operators 12
MATLAB: Quick view (10) Relational Operators 13
MATLAB: Quick view (11) Logical Operators 14
MATLAB: Quick view (12) Operator Precedence Precedence levels determine the order in which MATLAB evaluates an expression. Within each precedence level, operators have equal precedence and are evaluated from left to right. 1. Parentheses ( ) 2. Power (.^, ^), Transpose ( ) 3. Logical negation (~) 4. Multiplication (.*, *), Division (./,.\, /, \) 5. Addition (+), Subtraction (-) 15
MATLAB: Quick view (13) 6. Colon operator (:) 7. Less (<=, <), Greater (>=, >), Equal (==), Not equal (~=) 8. Logical AND (&) 9. Logical OR ( ) Use parentheses to explicitly specify the intended precedence. 16
MATLAB: Quick view (14) Conditional Control IF, ELSEIF, ELSE Evaluates a logical expression and executes a group of statements based on the value of the expression. if logical_expression 1 statements 1 elseif logical_expression 2 statements 2 else statements n end 17
MATLAB: Quick view (15) SWITCH, CASE, OTHERWISWE Switch executes statements based on the value of a variable or expression. switch expression (string or scalar) case exp_value 1 statements 1 case exp_value 2 statements 2 otherwise statements n end 18
MATLAB: Quick view (16) Loop Control FOR Executes a group of statements a predetermined number of times. for counter1=initial value1:step1:final value1 statements 1 for counter2=initial value2:step2:final value2 statements 2 if expression continue else break end statements 3 end end 19
MATLAB: Quick view (17) WHILE Executes a group of statements repeatedly as long as the controlling expression is true. while expression 1 statements 1 if expression 2 continue else break end statements 2 end 20
MATLAB: Quick view (18) Error Control TRY, CATCH Provide a way to take certain actions in the event of an error. try catch end statements 1 statements 2 statements n error message 1 error message n 21
MATLAB: Quick view (18) PROFILER The profiler GUI and the stopwatch functions enable to get back information on how a program is performing. profile on; [A,nz]=filename02(3000,3000,2,0.001); profile viewer 22
MATLAB: Quick view (19) 23
Τέλος Ενότητας