Πέτρος Κωμοδρόμος Αναπλ. Καθ., Τμήμα ΠΜΜΠ Πανεπιστήμιο Κύπρου Σεπτέμβριος, 2016 1
Περιεχόμενα Εισαγωγή στο πρόγραμμα Matlab Βασικές λειτουργίες Διανύσματα και μητρώα Γραφικές απεικονίσεις και παραστάσεις γραφικές απεικονίσεις σημείων και ευθύγραμμων τμημάτων γραφικές παραστάσεις συναρτήσεων γραφικές παραστάσεις πολλαπλών συναρτήσεων πολλαπλές γραφικές παραστάσεις πολλαπλά γραφικά σχήματα 2
Αρχεία Μatlab Αρχεία Εντολών Αρχεία Δεδομένων Προγραμματισμός με το Matlab Αρχεία συναρτήσεων Λογικά τεστ Βρόγχοι 3
Πρόγραμμα Matlab (παλαιότερη έκδοση) 4
Πρόγραμμα Matlab (νεώτερη έκδοση) 5
Περιβάλλον και παράθυρα Matlab 6
Πρόγραμμα Matlab 7
Φάκελος εργασίας Matlab 8
Πληροφορίες/βοήθεια για το Matlab Εκκίνηση προγράμματος >> help ( >> more on) >> help matlab\general >> help beep >> demos Ιστοσελίδα κατασκευαστών: http://www.mathworks.com >> doc >> lookfor <XYZ> Χρήση σαν απλή υπολογιστική 9
Βασικές λειτουργίες Matlab Τελεστές: >> help ops >> help colon Προτεραιότητα τελεστών Αριθμητικές εκφράσεις Μεταβλητές: ( ) ^ *, / +, - <, <=, ==, =>, > &, (17+3)/2+6/3 17+3/2+6/3 >> who Your variables are: a ans r >> clear >> whos >> pi ans = 3.1416 >> r = 5 r =5 >> pi * r ^ 2 ans = 78.5398 >> a = ans; >> a a = 78.5398 >> a = pi * r ^ 2 a = 78.5398 10
Αριθμητικοί τελεστές Plus + Unary plus + Minus - Unary minus - Matrix multiply * Array multiply.* Matrix power ^ Array power.^ Backslash or left matrix divide \ (INV(A)*B ) 11
Προτεραιότητα τελεστών transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^) unary plus (+), unary minus (-), logical negation (~) multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/), matrix left division (\) addition (+), subtraction (-) colon operator (:) less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not equal to (~=) short-circuit logical AND (&&) short-circuit logical OR ( ) 12
Τριγονομετρικές συναρτήσεις sin sind sinh asin asind asinh cos cosd cosh acos acosd acosh - Sine. - Sine of argument in degrees. - Hyperbolic sine. - Inverse sine. - Inverse sine, result in degrees. - Inverse hyperbolic sine. - Cosine. - Cosine of argument in degrees. - Hyperbolic cosine. - Inverse cosine. - Inverse cosine, result in degrees. - Inverse hyperbolic cosine. 13
tan tand tanh atan atand atan2 atanh sec secd sech asec asecd asech - Tangent. - Tangent of argument in degrees. - Hyperbolic tangent. - Inverse tangent. - Inverse tangent, result in degrees. - Four quadrant inverse tangent. - Inverse hyperbolic tangent. - Secant. - Secant of argument in degrees. - Hyperbolic secant. - Inverse secant. - Inverse secant, result in degrees. - Inverse hyperbolic secant. 14
csc cscd csch acsc acscd acsch cot cotd coth acot acotd acoth - Cosecant. - Cosecant of argument in degrees. - Hyperbolic cosecant. - Inverse cosecant. - Inverse cosecant, result in degrees. - Inverse hyperbolic cosecant. - Cotangent. - Cotangent of argument in degrees. - Hyperbolic cotangent. - Inverse cotangent. - Inverse cotangent, result in degrees. - Inverse hyperbolic cotangent. 15
Εκθετικές/Λογαριθμικές συναρτήσεις exp log log10 log2 pow2 realsqrt sqrt - Exponential. - Natural logarithm. - Common (base 10) logarithm. - Base 2 logarithm and dissect floating point number. - Base 2 power and scale floating point number. - Square root of number greater than or equal to zero. - Square root. 16
Συναρτήσεις στρογγυλοποιήσεων fix floor ceil round mod rem sign - Round towards zero. - Round towards minus infinity. - Round towards plus infinity. - Round towards nearest integer. - Modulus (signed remainder after division). - Remainder after division. - Signum. 17
>> x = 1.333 x = 1.3330 >> floor(x) ans = 1 >> ceil(x) ans = 2 >> round(x) ans = 1 x = -0.6667 >> floor(x) ans = -1 >> ceil(x) ans = 0 >> round(x) ans = -1 >> 18
Διανύσματα και μητρώα >> a(2) 2 >> a(1) = 6; >> a 6 2 3 >> c(2,3) 11 >> c(1,1:2) >> c(2,2) = -5; >> c 6 7 6 7 8 9-5 11 19
>> d = a * b 4 5 8 10 12 15 >> e = c * a 44 62 >> a >> c 1 2 3 6 9 7 10 8 11 >> sum(a) 6 20
x = [ 1 2 3 4 1 1 ] u = x * y y = [ 4 2 1 3 1 2 ] v = x.* y >> w = x * y = [ 11 11 19 15 ] >> inv(w) = [ -0.3409 0.2500 0.4318-0.2500 ] >> w * inv(w) = [ 1.0 0.0 0.0 1.0 ] >> det(w) = -44 = [ 4 4 3 12 1 2 ] >> eye(3) = [ 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 ] >> zeros(2,3) = [ 0.0 0.0 0.0 0.0 0.0 0.0 ] 21
>> a = [ 1 2 3 4 5 6] a = 1 2 3 4 5 6 >> b = [ 0 1 ; 2 0 ; 3 4] b = 0 1 2 0 3 4 >> a*b ans = 13 13 28 28 >> c = a * b c = 13 13 28 28 >> z = a.* b' z = 0 4 9 4 0 24 22
>> format compact >> a=rand(3,3) a = 0.1897 0.3028 0.6979 0.1934 0.5417 0.3784 0.6822 0.1509 0.8600 >> b=eye(3)*4 b = 4 0 0 0 4 0 0 0 4 >> a=rand(3,3) a = 0.9572 0.1419 0.7922 0.4854 0.4218 0.9595 0.8003 0.9157 0.6557 >> b=rand(3,1) b = 0.0357 0.8491 0.9340 >> x=inv(a)*b x = -0.7728 1.1407 0.7745 >> a\b ans = -0.7728 1.1407 0.7745 23
Πληροφορίες πινάκων size - Size of array. length - Length of vector. numel - Number of elements. disp - Display matrix or text. Βασικοί πίνακες zeros ones eye rand - Zeros array. - Ones array. - Identity matrix. - Uniformly distributed random numbers. 24
Συναρτήσεις πινάκων rank det trace inv eig - Matrix rank. - Determinant. - Sum of diagonal elements. - Matrix inverse. - Eigenvalues and eigenvectors. 25
Ανάλυση δεδομένων max - Largest component. min - Smallest component. mean - Average or mean value. median - Median value. std - Standard deviation. var - Variance. sort - Sort in ascending order. sortrows - Sort rows in ascending order. sum - Sum of elements. prod - Product of elements. hist - Histogram. cumsum - Cumulative sum of elements. cumprod - Cumulative product of elements. 26
Γραφικές απεικονίσεις σημείων >> x1 = 5; >> y1 = 7; >> plot(x1,y1,'*') % Σχεδιασμός σημείων (x1,y1) >> grid on % Προσθήκη κύριων γραμμών στο διάγραμμα >> title('sxediash Shmeioy') % Προσθήκη τίτλου >> xlabel('x') >> ylabel('y') % Προσθήκη τίτλων στους άξονες σχεδίασης 27
Γραφικές απεικονίσεις σημείων >> clf >> x1 = 5; >> y1 = 7; >> plot(x1,y1,'*') >> grid on >> title('sxediash Shmeioy') >> xlabel('x') >> ylabel('y') >> hold on >> myaxes = [ 1 10 4 9 ]; >> axis(myaxes) >> plot(7,6,'o') 28
Γραφικές απεικονίσεις σημείων και ευθειών >> clf ; clear; >> x1 = 5; y1 = 7; >> x2 = 7; y2 = 6; >> x3 = 4.2; y3 = 5.5; >> x = [x1 x2 x3]; >> y = [y1 y2 y3]; >> axis equal >> axis([ 1 10 4 9 ]); >> hold on >> plot(x,y, *') >> x(4)=x1; y(4)=y1; >> plot(x,y) >> grid on >> title( Sxediasmos shmeiwn kai trigwnoy') >> xlabel('x') >> ylabel('y') 29
Γραφικές παραστάσεις >> x = 0:0.25:15; (αρχή:βήμα:τέλος) % Αυτόματος καθορισμός διανύσματος με τιμές γωνιών >> y = sin(x); % Αυτόματος υπολογισμός διανύσματος με τιμές ημιτόνων >> plot(x,y) % Σχεδιασμός σημείων (x,y) >> grid on % Προσθήκη κύριων γραμμών στο διάγραμμα >> title('sxediash Hmitonoy') % Προσθήκη τίτλου >> xlabel('x') >> ylabel('y') % Προσθήκη τίτλων στους άξονες σχεδίασης 30
Παράδειγμα Σχεδιάστε την γραφική παράσταση της εξίσωσης: όπου: 0 <= theta <= 100 >> clf >> clear >> theta = 0:0.5:100 >> z = 15*cos(theta/2) >> z = z + 7.5 * sin(theta/5); >> plot(theta,z) >> grid on >> title('askhsh 2h') >> xlabel('theta') >> ylabel('z') 31
dx=10 x1=0:0.1:dx; x4=x1+5; y4=22.5+5*cos((x3-5)/dx*2*pi); plot(x4,y4, 'r'); Παράδειγμα x3=x1+5; y3=15+cos((x3-5)/dx*2*pi); plot(x3,y3, k'); x2=x1; y2=7.5+cos(x2/dx*2*pi); plot(x2,y2, g--'); dx=10 x1=0:0.1:dx; y1=cos(x1); plot(x1,y1); grid on hold on axis([-2 30-2 30]) 32
>> figure(1) Πολλαπλές γραφικές παραστάσεις >> clf >> x = 0:0.25:15; >> y = sin(x); >> plot(x,y, r ) >> grid on >> title('sxediash Hmitonoy') >> xlabel('x') >> ylabel( Hmitono,Synhmitono ) >> z = cos(x); >> hold on >> plot(x,z) >> plot(x,z, or ) Eναλλακτικά: plot(x,y,'r',x,z, x,z,'or') 33
figure(2) subplot(3,2,1) plot(x,y,'-') title('x-y Plot ') subplot(3,2,3) plot(x,y,'--') grid on title('x-y Plot with grid') subplot(3,2,4) plot(x,z) grid on title('x-z Plot with grid') subplot(3,2,6) plot(x,z, 'o') title('x-z Plot: o ') Πολλαπλά γραφικά σχήματα subplot(3,2,5) plot(x,z, '*') title('x-z Plot: *') 34
Γραφικές παραστάσεις και απεικονίσεις: Σχεδιάστε τα σημεία (-3,-2), (0,1), (2,2), (4,5) και (7,9) χρησιμοποιώντας το σύμβολα ο και διακεκομμένη γραμμή στο 3 ον τομέα ενός σχήματος αποτελούμενου από 2 γραμμές και 2 στήλες, όπως πιο κάτω: >> x = [ -3 0 2 4 7 ]; >> y = [ -2 1 2 5 9 ] >> subplot(2,2,3) >> plot(x,y,'o') >> grid on >> hold on >> plot(x,y,'--') 35
Βασικές συναρτήσεις δισδιάστατων γραφικών plot - Linear plot. loglog - Log-log scale plot. semilogx - Semi-log scale plot. semilogy - Semi-log scale plot. plotyy - Graphs with y tick labels on the left and right. 36
axis - Control axis scaling and appearance. zoom - Zoom in and out on a 2-D plot. grid - Grid lines. box - Axis box. hold - Hold current graph. subplot - Create axes in tiled positions. plotedit - Tools for editing and annotating plots. title - Graph title. xlabel - X-axis label. ylabel - Y-axis label. text gtext - Text annotation. - Place text with mouse. 37
Τρισδιάστατα γραφικά >> x=0:0.02:10; >> y=sin(x) >> z=cos(x) >> plot3(x,y,z) >> grid on >> xlabel('x') >> ylabel('y') >> zlabel('z') >> title('using plot3') 38
Βασικές συναρτήσεις τρισδιάστατων γραφικών plot3 mesh surf fill3 - Plot lines and points in 3-D space. - 3-D mesh surface. - 3-D colored surface. - Filled 3-D polygons. 39
Βασικές συναρτήσεις γραφικών axis grid box hold - Control axis scaling and appearance. - Grid lines. - Axis box. - Hold current graph. subplot - Create axes in tiled positions. xlim ylim zlim title - X limits. - Y limits. - Z limits - Graph title 40
Εξειδικευμένες συναρτήσεις δισδιάστατων γραφικών area bar barh comet compass feather - Filled area plot. - Bar graph. - Horizontal bar graph. - Comet-like trajectory. - Compass plot. - Feather plot. fill hist pie - Filled 2-D polygons. - Histogram. - Pie chart. 41
Γενικές συναρτήσεις γραφικών figure clf close refresh subplot axis box hold ishold - Create figure window. - Clear current figure. - Close figure. - Refresh figure. - Create axes in tiled positions. - Control axis scaling and appearance. - Axis box. - Hold current graph. - Return hold state. 42
Αρχεία Matlab: αρχεία Μ (M-files) mymfile.m >> mymfile File Set Path 43
Αρχεία δεδομένων Matlab >> load data1 >> data1 >> data(2,1) 4 5 6 7 8 9 7 44
Αρχεία Matlab: load/save >> x = 1:2:11; >> y = [4 7-3 2]; >> save C:\mySaved1 x % Save workspace variables to disk >> save C:\mySaved2 >> clear all >> load C:\mySaved1 % Load workspace variables from disk >> x 1 3 5 7 9 11 >> y??? Undefined function or variable y'. >> load C:\mySaved2 >> y 4 7-3 2 45
temps.dat Σχεδίαση με βάση αρχεία Matlab >> temperatures temperatures.m 46
Αρχεία συναρτήσεων Matlab Απλές εντολές σε M-file: a = 2.5; e = pi * a ^ 2; mymfile1.m >> mymfile1 >> e e = 19.634954 Συναρτήσεις: myfunction1.m function embado=myfunction1(x) embado = pi * x ^2; return >> e = myfunction1(10) e = 314.1593 >> myfunction1(0.5) ans = 0.7854 >> x=myfunction1(17.5) x = 962.1128 47
Ορισμός συναρτήσεως: Συναρτήσεις function apotelesma = synarthsh1(parametroi) myfun1.m function y=myfun1(x) y = x*x*x; return >> clear all >> z=myfun1(8) >> y??? Undefined function or variable 'y'. >> z z = 512 Σώμα συνάρτησης Ορισμός συνάρτησης myfun2.m function myfun2(x,y) x + y return >> myfun2(8,7) ans = 15 48
Επιστροφή πολλαπλών τιμών και τοπικές μεταβλητές function [a2,b3] = MyFun1(x,y) a2 = x^2; a2 = 75; b3 = y^3; b3 = -45; return x = 3; y = -12; [k,m] = MyFun1(2,5); k m a2 b3 x y >> testmyfun1 k = 4 m = 125 a2 = 75 b3 = -45 x = 3 y = -12 49
Σχετικοί τελεστές (Relational operators) > < > = < = = = ~ = Equal == Not equal ~= Less than < Greater than > Less than or equal <= Greater than or equal >= 50
Λογικοί τελεστές (Logical operators) && ~ Short-circuit logical AND && Short-circuit logical OR Logical NOT ~ 51
Λογικά τεστς >> x = 5; >> y = 7; >> x < y 1 >> x >= y - 3 1 >> x == y - 3 0 >> 0 < x && x+2 > y 0 >> 0 < x y > x 1 >> ~ (x < 5) 1 52
Διακλαδώσεις if έκφραση1 end εκτελείται μόνο αν η έκφραση1 είναι αληθής if έκφραση1 else end εκτελείται μόνο αν η έκφραση1 είναι αληθής εκτελείται μόνο αν η έκφραση1 δεν είναι αληθής if έκφραση1 εκτελείται μόνο αν η έκφραση1 είναι αληθής elseif έκφραση2 else end εκτελείται μόνο αν η έκφραση1 δεν είναι αληθής και η έκφραση2 είναι αληθής εκτελείται αν καμία έκφραση δεν είναι αληθής 53
Παράδειγμα διακλαδώσεων rand1.m 54
Δομή ελέγχου switch x=3 switch x case 1 y1 = x case 3 y3 = x+3 case 7 y7 = x+777 otherwise zz = x^2 end >> x = 3 y3 = 6 55
Δομή ελέγχου switch x = -4 switch x case 1 y1 = x case 3 y3 = x+3 case 7 y7 = x+777 otherwise zz = x^2 end >> x = -4 zz = 16 56
Βρόγχοι for μεταβλητή = αρχική τιμή : (προαιρετικό) βήμα : τελική τιμή end εντολές for i=1:5 end disp(i^2) 1 4 9 16 25 for i=5:-1:1 if i == 3 continue end disp(i^2) end 25 16 4 1 57
Βρόγχοι (συνεχ.) while λογική έκφραση end εντολές (μέσα στις οποίες πρέπει κάτι σχετικό με την λογική έκφραση να αλλάζει, ή να υπάρχει κάποιο break, ώστε να αποφεύγεται βρόγχος επ άπειρον) i = 0; while i<10 i = i + 2 end disp(i) i = 2 i = 4 i = 6 i = 8 i = 10 10 i = 0; while 1 if i>=10 break end i = i + 2 end disp(i) continue: συνέχεια με επόμενη εκτέλεση βρόγχου for ή while break: τερματισμός εκτέλεσης βρόγχων for και while 58
Δομές ελέγχου if else elseif end for while break - Conditionally execute statements. - Execute statement if previous IF condition failed. - Execute if previous IF failed and condition is true. - Terminate scope of control statements. - Repeat statements a specific number of times. - Repeat statements an indefinite number of times. - Terminate execution of WHILE or FOR loop. continue switch case - Pass control to the next iteration of a loop. - Switch among several cases based on expression. - SWITCH statement case. otherwise - Default SWITCH statement case. 59
Σύγκριση Matlab και άλλων γλωσσών προγραμματισμού Matlab σε σύγκριση με: Fortran C/C++ Java Visual Basic/VB.net C# 60
Είσοδος δεδομένων/έξοδος αποτελεσμάτων io1.m a = input('aktina kykloy: '); p = 2 * pi * a; e = pi * a ^ 2; fprintf(' Dokimh: %s ', 'Matlab') fprintf('\n Aktina = %f ', a) fprintf('\n Perimetros = %.3f ', p) fprintf(' \n Epifaneia = %f ', e) fprintf(' p = %f \t e = %f ', p, e) fprintf(' %s = %f ', 'a', a) fprintf(' %s = %f ', 97, a) >> io1 Aktina kykloy: 5 Dokimh: Matlab Aktina = 5.0000 Perimetros = 31.416 Epifaneia = 78.539816 p= 31.415927 e = 78.539816 a = 5 a = 5 61
Μορφοποίηση αποτελεσμάτων >> format long >> pi ans = 3.14159265358979 >> format short >> pi ans = 3.1416 >> format compact >> format loose >> format short e >> format long e 62
disp(x): παρουσιάζει τον πίνακα x >> disp(pi) >> t= 'Testing 3.1416 >> x = 4.5 >> disp(t) Testing >> disp x x >> disp(x) 4.5 63
Εκτέλεση εντολών υπό μορφή χαρακτήρων >> s='2*3-5+3/2 >> eval(s) ans =2.5000 >> s2 = 'x = 45/9-2' s2 = x = 45/9-2 >> eval(s2) x = 3.00 64
Μέγιστες και ελάχιστες τιμές >> realmax ans =1.7977e+308 >> realmin ans =2.2251e-308 INF : Infinity NaN : Not-a-Number 65
Εντολές Λειτουργικού Συστήματος cd - Change current working directory. copyfile - Copy file or directory. movefile - Move file or directory. delete - Delete file or graphics object. pwd - Show (print) current working directory. dir - List directory. ls - List directory. mkdir - Make new directory. rmdir - Remove directory.! - Execute operating system command. 66
Εντολές Χώρου Εργασίας (workspace) who whos clear load save quit exit - List current variables. - List current variables, long form. - Clear variables and functions from memory. - Load workspace variables from disk. - Save workspace variables to disk. - Quit MATLAB session. - Exit from MATLAB. 67
Εντολές ελέγχου what type open which - List MATLAB-specific files in directory. - List M-file. - Open files by extension. - Locate functions and files. 68
Χρονικές συναρτήσεις now date clock datestr calendar cputime - Current date and time as date number. - Current date as date string. - Current date and time as date vector. - String representation of date. - Calendar. - CPU time in seconds. tic toc pause - Start stopwatch timer. - Stop stopwatch timer. - Wait in seconds. 69
Περισσότερες πληροφορίες για το Matlab Εισαγωγή στη Matlab, Γ. Γεωργίου & Χ. Ξενοφώντος, Βιβλ. Καντζηλάρης. http://www.mathworks.com/help/techdoc/index.html http://www.math.ufl.edu/help/matlab-tutorial/ http://www.cs.ubc.ca/spider/cavers/matlabguide/guide.html http://www.cyclismo.org/tutorial/matlab/ http://www.math.utah.edu/lab/ms/matlab/matlab.html http://www.math.mtu.edu/~msgocken/intro/intro.html http://users.ece.gatech.edu/~bonnie/book/tutorial/tutorial.html http://amath.colorado.edu/computing/matlab/tutorial/ 70