PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016...

70
Εισαγωγή στο Matlab Εισαγωγή στο Matlab Πέτρος Κωμοδρόμος Αναπλ. Καθ., Τμήμα ΠΜΜΠ Πανεπιστήμιο Κύπρου Σεπτέμβριος, 2016 1

Transcript of PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016...

Page 1: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εισαγωγή στο Matlab

Πέτρος Κωμοδρόμος

Αναπλ. Καθ., Τμήμα ΠΜΜΠ

Πανεπιστήμιο Κύπρου

Σεπτέμβριος, 2016

1

Page 2: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Περιεχόμενα

Εισαγωγή στο πρόγραμμα Matlab

Βασικές λειτουργίες

Διανύσματα και μητρώα

Γραφικές απεικονίσεις και παραστάσεις

γραφικές απεικονίσεις σημείων και ευθύγραμμων τμημάτων

γραφικές παραστάσεις συναρτήσεων

γραφικές παραστάσεις πολλαπλών συναρτήσεων

πολλαπλές γραφικές παραστάσεις

πολλαπλά γραφικά σχήματα

2

Page 3: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αρχεία Μatlab

Αρχεία Εντολών

Αρχεία Δεδομένων

Προγραμματισμός με το Matlab

Αρχεία συναρτήσεων

Λογικά τεστ

Βρόγχοι

3

Page 4: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πρόγραμμα Matlab (παλαιότερη έκδοση)

4

Page 5: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πρόγραμμα Matlab (νεώτερη έκδοση)

5

Page 6: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab6

Περιβάλλον και παράθυρα Matlab

Page 7: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πρόγραμμα Matlab

7

Page 8: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab8

Φάκελος εργασίας Matlab

Page 9: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πληροφορίες/βοήθεια για το Matlab

Εκκίνηση προγράμματος

>> help ( >> more on)

>> help matlab\general >> help beep

>> demos

Ιστοσελίδα κατασκευαστών: http://www.mathworks.com

>> doc

>> lookfor <XYZ>

Χρήση σαν απλή υπολογιστική

9

Page 10: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

( )

^

* , /

+ , -

< , <= , == , => , >

& , |

Βασικές λειτουργίες Matlab

(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

Τελεστές:

Προτεραιότητα τελεστών

Αριθμητικές εκφράσεις

Μεταβλητές:

>> help ops

>> help colon

17+3/2+6/3

10

Page 11: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αριθμητικοί τελεστές

Plus +

Unary plus +

Minus -

Unary minus -

Matrix multiply *

Array multiply .*

Matrix power ^

Array power .^

Backslash or left matrix divide \ (INV(A)*B )

11

Page 12: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Προτεραιότητα τελεστών

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

Page 13: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Τριγονομετρικές συναρτήσεις

sin - Sine.sind - Sine of argument in degrees.sinh - Hyperbolic sine.asin - Inverse sine.asind - Inverse sine, result in degrees.asinh - Inverse hyperbolic sine.cos - Cosine.cosd - Cosine of argument in degrees.cosh - Hyperbolic cosine.acos - Inverse cosine.acosd - Inverse cosine, result in degrees.acosh - Inverse hyperbolic cosine.

13

Page 14: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

tan - Tangent.tand - Tangent of argument in degrees.tanh - Hyperbolic tangent.atan - Inverse tangent.atand - Inverse tangent, result in degrees.atan2 - Four quadrant inverse tangent.atanh - Inverse hyperbolic tangent.sec - Secant.secd - Secant of argument in degrees.sech - Hyperbolic secant.asec - Inverse secant.asecd - Inverse secant, result in degrees.asech - Inverse hyperbolic secant.

14

Page 15: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

csc - Cosecant.

cscd - Cosecant of argument in degrees.

csch - Hyperbolic cosecant.

acsc - Inverse cosecant.

acscd - Inverse cosecant, result in degrees.

acsch - Inverse hyperbolic cosecant.

cot - Cotangent.

cotd - Cotangent of argument in degrees.

coth - Hyperbolic cotangent.

acot - Inverse cotangent.

acotd - Inverse cotangent, result in degrees.

acoth - Inverse hyperbolic cotangent.

15

Page 16: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εκθετικές/Λογαριθμικές συναρτήσεις

exp - Exponential.

log - Natural logarithm.

log10 - Common (base 10) logarithm.

log2 - Base 2 logarithm and dissect floating point number.

pow2 - Base 2 power and scale floating point number.

realsqrt - Square root of number greater than or equal to zero.

sqrt - Square root.

16

Page 17: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Συναρτήσεις στρογγυλοποιήσεων

fix - Round towards zero.

floor - Round towards minus infinity.

ceil - Round towards plus infinity.

round - Round towards nearest integer.

mod - Modulus (signed remainder after division).

rem - Remainder after division.

sign - Signum.

17

Page 18: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

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

Page 19: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Διανύσματα και μητρώα

>> a(2)

>> a(1) = 6;

>> a

>> c(2,3)

>> c(1,1:2)

>> c(2,2) = -5;

>> c

2

11

6 7

6

2

3

6 7 8

9 -5 11

19

Page 20: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

6

4 5

8 10

12 15

44

62

1 2 3

6 9

7 10

8 11

>> d = a * b

>> e = c * a

>> a’

>> c’

>> sum(a)

20

Page 21: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

x = [ 1 2 3

4 1 1 ]

y = [ 4 2 1

3 1 2 ] v = x .* y

>> w = x * y’

u = x * y

= [ 4 4 3

12 1 2 ]

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

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

Page 22: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

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

Page 23: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

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

Page 24: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πληροφορίες πινάκων

size - Size of array.

length - Length of vector.

numel - Number of elements.

disp - Display matrix or text.

Βασικοί πίνακες

zeros - Zeros array.

ones - Ones array.

eye - Identity matrix.

rand - Uniformly distributed random numbers.

24

Page 25: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Συναρτήσεις πινάκων

rank - Matrix rank.det - Determinant.trace - Sum of diagonal elements.

inv - Matrix inverse.

eig - Eigenvalues and eigenvectors.

25

Page 26: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Ανάλυση δεδομένων

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

Page 27: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Γραφικές απεικονίσεις σημείων

>> x1 = 5;

>> y1 = 7;

>> plot(x1,y1,'*')

% Σχεδιασμός σημείων (x1,y1)

>> grid on

% Προσθήκη κύριων γραμμών στο διάγραμμα

>> title('Sxediash Shmeioy')

% Προσθήκη τίτλου

>> xlabel('X')

>> ylabel('Y')

% Προσθήκη τίτλων στους άξονες σχεδίασης

27

Page 28: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Γραφικές απεικονίσεις σημείων

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

Page 29: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Γραφικές απεικονίσεις σημείων και ευθειών

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

Page 30: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Γραφικές παραστάσεις

>> x = 0:0.25:15; (αρχή:βήμα:τέλος)

% Αυτόματος καθορισμός διανύσματος με τιμές γωνιών

>> y = sin(x);

% Αυτόματος υπολογισμός διανύσματος με τιμές ημιτόνων

>> plot(x,y)

% Σχεδιασμός σημείων (x,y)

>> grid on

% Προσθήκη κύριων γραμμών στο διάγραμμα

>> title('Sxediash Hmitonoy')

% Προσθήκη τίτλου

>> xlabel('X')

>> ylabel('Y')

% Προσθήκη τίτλων στους άξονες σχεδίασης

30

Page 31: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Παράδειγμα

Σχεδιάστε την γραφική παράσταση της εξίσωσης:

όπου: 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

Page 32: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Παράδειγμα

dx=10

x1=0:0.1:dx;

y1=cos(x1);

plot(x1,y1);

grid on

hold on

axis([-2 30 -2 30])

x2=x1;

y2=7.5+cos(x2/dx*2*pi);

plot(x2,y2, ’g--');

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');

32

Page 33: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πολλαπλές γραφικές παραστάσεις>> 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

Page 34: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Πολλαπλά γραφικά σχήματα

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

Page 35: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Γραφικές παραστάσεις και απεικονίσεις:

Σχεδιάστε τα σημεία (-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

Page 36: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Βασικές συναρτήσεις δισδιάστατων γραφικών

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

Page 37: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

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

gtext - Place text with mouse.

37

Page 38: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Τρισδιάστατα γραφικά

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

Page 39: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

plot3 - Plot lines and points in 3-D space.

mesh - 3-D mesh surface.

surf - 3-D colored surface.

fill3 - Filled 3-D polygons.

Βασικές συναρτήσεις τρισδιάστατων γραφικών

39

Page 40: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

axis - Control axis scaling and appearance.

grid - Grid lines.

box - Axis box.

hold - Hold current graph.

subplot - Create axes in tiled positions.

xlim - X limits.

ylim - Y limits.

zlim - Z limits

title - Graph title

Βασικές συναρτήσεις γραφικών

40

Page 41: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

area - Filled area plot.

bar - Bar graph.

barh - Horizontal bar graph.

comet - Comet-like trajectory.

compass - Compass plot.

feather - Feather plot.

fill - Filled 2-D polygons.

hist - Histogram.

pie - Pie chart.

Εξειδικευμένες συναρτήσεις δισδιάστατων γραφικών

41

Page 42: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

figure - Create figure window.clf - Clear current figure.close - Close figure.refresh - Refresh figure.

subplot - Create axes in tiled positions.

axis - Control axis scaling and appearance.

box - Axis box.

hold - Hold current graph.

ishold - Return hold state.

Γενικές συναρτήσεις γραφικών

42

Page 43: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αρχεία Matlab: αρχεία Μ (M-files)

>> myMFile

myMFile.m

FileSet Path

43

Page 44: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αρχεία δεδομένων Matlab

>> load data1

>> data1

>> data(2,1)

4 5 6

7 8 9

7

44

Page 45: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αρχεία 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

>> y

>> load C:\mySaved2

>> y

1 3 5 7 9 11

??? Undefined function or variable ’y'.

4 7 -3 2

45

Page 46: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Σχεδίαση με βάση αρχεία Matlab

>> temperatures

temps.dat

temperatures.m

46

Page 47: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Αρχεία συναρτήσεων Matlab

myMFile1.m

a = 2.5;

e = pi * a ^ 2;

>> myMFile1

>> e

e = 19.634954

• Απλές εντολές σε M-file:

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

Page 48: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Συναρτήσεις

myFun1.m

function y=myFun1(x)

y = x*x*x;

return

• Ορισμός συναρτήσεως:

function apotelesma = synarthsh1(parametroi)

myFun2.m

function myFun2(x,y)

x + y

return

>> clear all

>> z=myFun1(8)

>> y

??? Undefined function or variable 'y'.

>> z

z = 512

>> myFun2(8,7)

ans = 15

Ορισμός

συνάρτησης

Σώμα

συνάρτησης

48

Page 49: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Επιστροφή πολλαπλών τιμών και τοπικές μεταβλητές

function [a2,b3] = MyFun1(x,y)

a2 = x^2;

b3 = y^3;

return

a2 = 75;

b3 = -45;

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 =

-1249

Page 50: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Σχετικοί τελεστές (Relational operators)

> < > = < = = = ~ =

Equal ==

Not equal ~=

Less than <

Greater than >

Less than or equal <=

Greater than or equal >=

50

Page 51: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Λογικοί τελεστές (Logical operators)

&& || ~

Short-circuit logical AND &&

Short-circuit logical OR ||

Logical NOT ~

51

Page 52: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Λογικά τεστς

>> x = 5;

>> y = 7;

>> x < y

>> x >= y - 3

>> x == y - 3

>> 0 < x && x+2 > y

>> 0 < x || y > x

>> ~ (x < 5)

1

1

0

0

1

1

52

Page 53: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Διακλαδώσεις

if έκφραση1

εκτελείται μόνο

αν η έκφραση1

είναι αληθής

end

if έκφραση1

εκτελείται μόνο

αν η έκφραση1

είναι αληθής

elseif έκφραση2

εκτελείται μόνο

αν η έκφραση1

δεν είναι αληθής

και η έκφραση2

είναι αληθής

else

εκτελείται αν

καμία έκφραση

δεν είναι αληθής

end

if έκφραση1

εκτελείται μόνο

αν η έκφραση1

είναι αληθής

else

εκτελείται μόνο

αν η έκφραση1

δεν είναι αληθής

end

53

Page 54: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Παράδειγμα διακλαδώσεων

rand1.m

54

Page 55: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Δομή ελέγχου 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

Page 56: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Δομή ελέγχου 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

Page 57: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Βρόγχοι

for μεταβλητή = αρχική τιμή : (προαιρετικό) βήμα : τελική τιμή

εντολές

end

for i=1:5

disp(i^2)

end

1

4

9

16

25

for i=5:-1:1

if i == 3 continue

end

disp(i^2)

end

25

16

4

1

57

Page 58: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Βρόγχοι (συνεχ.)

i = 0;

while i<10

i = i + 2

end

disp(i)

while λογική έκφραση

εντολές

end

(μέσα στις οποίες πρέπει κάτι σχετικό με την

λογική έκφραση να αλλάζει, ή να υπάρχει κάποιο

break, ώστε να αποφεύγεται βρόγχος επ’άπειρον)

i = 2

i = 4

i = 6

i = 8

i = 10

10

continue: συνέχεια με επόμενη εκτέλεση βρόγχου for ή while

break: τερματισμός εκτέλεσης βρόγχων for και while

i = 0;

while 1if i>=10

breakend

i = i + 2

end

disp(i)

58

Page 59: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Δομές ελέγχου

if - Conditionally execute statements.

else - Execute statement if previous IF condition failed.

elseif - Execute if previous IF failed and condition is true.

end - Terminate scope of control 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.

continue - Pass control to the next iteration of a loop.

switch - Switch among several cases based on expression.

case - SWITCH statement case.

otherwise - Default SWITCH statement case.

59

Page 60: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Σύγκριση Matlab και άλλων γλωσσών προγραμματισμού

• Matlab

σε σύγκριση με:

• Fortran

• C/C++

• Java

• Visual Basic/VB .net

• C#

60

Page 61: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Είσοδος δεδομένων/Έξοδος αποτελεσμάτων

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

Page 62: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Μορφοποίηση αποτελεσμάτων

>> format long

>> pi

ans =

3.14159265358979

>> format short

>> pi

ans =

3.1416

>> format compact >> format loose

>> format short e >> format long e

62

Page 63: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

>> disp(pi) >> t= 'Testing’

>> disp(t)

Testing

3.1416

disp(x): παρουσιάζει τον πίνακα x

>> x = 4.5

>> disp x

x

>> disp(x)

4.5

63

Page 64: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εκτέλεση εντολών υπό μορφή χαρακτήρων

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

Page 65: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Μέγιστες και ελάχιστες τιμές

>> realMax

ans =1.7977e+308

>> realMin

ans =2.2251e-308

INF : Infinity

NaN : Not-a-Number

65

Page 66: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εντολές Λειτουργικού Συστήματος

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

Page 67: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εντολές Χώρου Εργασίας (workspace)

who - List current variables.

whos - List current variables, long form.

clear - Clear variables and functions from memory.

load - Load workspace variables from disk.

save - Save workspace variables to disk.

quit - Quit MATLAB session.

exit - Exit from MATLAB.

67

Page 68: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Εντολές ελέγχου

what - List MATLAB-specific files in directory.

type - List M-file.

open - Open files by extension.

which - Locate functions and files.

68

Page 69: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

now - Current date and time as date number.date - Current date as date string.clock - Current date and time as date vector.

datestr - String representation of date.

calendar - Calendar.

cputime - CPU time in seconds.

tic - Start stopwatch timer.toc - Stop stopwatch timer.pause - Wait in seconds.

Χρονικές συναρτήσεις

69

Page 70: PowerPoint Presentation - UCYTitle PowerPoint Presentation Author petros Created Date 10/26/2016 10:00:15 AM

Εισαγωγή στο Matlab

Περισσότερες πληροφορίες για το 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