Download presentation

Presentation is loading. Please wait.

Published byRalph Johnston Modified over 4 years ago

1
MATLAB and SimulinkLecture 11 To days Outline Introduction MATLAB Desktop Basic Features Branching Statements Loops Script file / Commando file Exercises on this days topics

2
MATLAB and SimulinkLecture 12 Introduction Course schedule W35-W41 Tuesdays 10-12 S319 Theory Tuesdays 13-17 L337/338Free Exercises Thursdays10-12L337/338Exercises Thursdays 13-17 L337/338 Laboratory work W 42-43 (44)Project Work

3
MATLAB and SimulinkLecture 13 Introduction No written examination Grades U – G Laboratory Works Solutions posted by mail, comment the code! Minor Project You should come up with an idea for the project Use your knowledge of MATLAB and Simulink in the project. Presentation of the project.

4
MATLAB and SimulinkLecture 14 Introduction Litterature Stephen J Chapman(2005). MATLAB Programming for Engineers ISBN: 0-534-42417-1 Dabney J.B and Harman T.L (2004). Mastering Simulink ISBN: 0-13-142477-7 Additional reading, see course information

5
MATLAB and SimulinkLecture 15 Introduction MATLAB is used for calculation and visualizing is programmable contains various commands and functions can interact with other program languages as: C, FORTRAN, JAVA etc. uses arrays as the basic data type

6
MATLAB and SimulinkLecture 16 MATLAB Desktop Command Window Present results Enter commands Editor/Debug Window Create M-files Debug M-files

7
MATLAB and SimulinkLecture 17 MATLAB Desktop Command History View previous commands

8
MATLAB and SimulinkLecture 18 MATLAB Desktop Current Directory File Manipulation Files in current directory

9
MATLAB and SimulinkLecture 19 MATLAB Desktop Workspace Shows variables defined in workspace

10
MATLAB and SimulinkLecture 110 MATLAB Desktop Help Demos Tutorials Documentations

11
MATLAB and SimulinkLecture 111 MATLAB Desktop Profiler Improve code writing

12
MATLAB and SimulinkLecture 112 Basic Features Execute commands either by Write the code in command window Write code in a script file Execute script file by typing the file name in command window. Write code in a function file Execute function file by typing the file name in command window.

13
MATLAB and SimulinkLecture 113 Basic Features Addition+ Subtraction- Multiplication* Division / Store results in variables Case sensitive Maximum of 31 characters Must start with a letter

14
MATLAB and SimulinkLecture 114 Basic Features Example In command window >> 5+5 ans = 10 >> a=5+5 a = 10

15
MATLAB and SimulinkLecture 115 Basic Features No need to declare variables Any numerical value assign to a variable will be of class double Any character or sting assign to a variable will be of class character

16
MATLAB and SimulinkLecture 116 Basic Features The fundamental unit of data in MATLAB is the array Row 1 Row 2 Row 3 Col 4Col 3Col 2Col 1

17
MATLAB and SimulinkLecture 117 Basic Features Initializing variables Assign data in assignment statements var1 = 5+5; Input data from keyboard var1 = input(‘Enter a value’) Read data from file var1 = fread(fid, 5, 'uint8=>char')'

18
MATLAB and SimulinkLecture 118 Basic Features How to create arrays in MATLAB The ‘[]’ operator var1 = [1,2,3,4] % Row vector var1 = [1,2;3,4]% 2x2 array operator ‘;’ equal to new row Shortcut expression var1 = first:incr:last var1 = 1:1:4

19
MATLAB and SimulinkLecture 119 Basic Features Built-in functions var1 = zeros(2,4) var1 = ones(2,4) Useful MATLAB functions eye(n) Creates an nxn unity matrix size(var1) Returns the number of rows and columns in variable var1 length(var1) Returns the length of a vector var1, or the longest dimension if var1 is a 2-D array

20
MATLAB and SimulinkLecture 120 Basic Features Accessing elements in an array var1(2,2)= 5; Row 1 Row 2 Row 3 Col 4Col 3Col 2Col 1

21
MATLAB and SimulinkLecture 121 Basic Features Displaying output data The disp function disp(str) disp(‘This is displayed’) The fprintf function fprintf(format,data) fprintf(‘This the value of pi %f \n’,pi)

22
MATLAB and SimulinkLecture 122 Basic Features Scalar and Array Operations Remember the algebraic rules! Dot operator performs element by element operation. OperationMATLAB Form Array AdditionA+B Array SubtractionA-B Array MultiplicationA*B Array DivisionA/B = A*inv(B) Element by ElementA./B, A.*B, A.^B

23
MATLAB and SimulinkLecture 123 Example 2.1 What is the result of each expression

24
MATLAB and SimulinkLecture 124 Basic Features Introduction to plotting Plot the function y(x) for values of x between -5 and 5 >>X=-5:0.1:5 >>y=5*x.^2-2*x+5 >>plot(x,y)

25
MATLAB and SimulinkLecture 125 Basic Features Plot figur

26
MATLAB and SimulinkLecture 126 Basic Features Multiple plots >>X=-5:0.1:5 >>y1=sin(x) >>y2=cos(x) >>subplot(2,1,1),plot(x,y1) >>subplot(2,1,2),plot(x,y2)

27
MATLAB and SimulinkLecture 127 Basic Features

28
MATLAB and SimulinkLecture 128 Basic Features Line style, Line color, marker style and legend plot(x,y,s) s is a character string defining color, line style, marker style. See help plot for more information g=green. = point-- = dashed k=blacko = circle- = solid

29
MATLAB and SimulinkLecture 129 Example 2.3 Design a MATLAB program that reads an input temperature in degrees Fahrenheit, convert it to an absolute temperature in Kelvin, and write the result.

30
MATLAB and SimulinkLecture 130 Example 2.4 The figure shows a voltage source V=120 V with an internal resistance R s =50Ω supplying a load R L. Find the value of R L that will result in maximum power being supplied by the source. Plot the power supplied to the load as a function of the resistance R L +-+- RsRs RLRL V

31
MATLAB and SimulinkLecture 131 Example 2.4

32
MATLAB and SimulinkLecture 132 Branching Statements Logic and Relational Operators Logical OPERATORSRelational Operators OperatorOperationOperatorOperation ==Equal to&Logical AND ~=Not Equal To|Logical OR >Greater ThanxorLogical Exclusive OR <Less Then~Logical NOT =Less/Greater Then or Equal to

33
MATLAB and SimulinkLecture 133 Branching Statements The if Construct if control_expr_1 Statement elseif control_expr_2 Statement else control_expr_3 Statement end

34
MATLAB and SimulinkLecture 134 Example 3.4 Suppose that we are writing a program which reads in a numerical grade and assigns a letter grade to it according to: grade > 95A 95 ≥ grade 86 >B 86 ≥ grade 76 >C 76 ≥ grade 66 >D 66 ≥ grade 0 >F

35
MATLAB and SimulinkLecture 135 Branching Statements The switch Construct switch (switch_expr) case {csde_expr_1, csde_expr_2,…}, Statement case {csde_expr_3, csde_expr_4,…}, Statement otherwise, Statement end

36
MATLAB and SimulinkLecture 136 Example Determine whether an integer between 1 and 10 is even or odd, and print out an appropriate message.

37
MATLAB and SimulinkLecture 137 Loops The While loop while expression ……. code block ……. end

38
MATLAB and SimulinkLecture 138 Loops The for loop for index = expr Statement1 Statement2 …. end

39
MATLAB and SimulinkLecture 139 Loops Comparing Loops and Vectorization If possible use vectors. Loops slows down execution speed Calculate the square of every integer from 1 to 10000. Using a for loop can take up to 3 sec Using vectors takes 0.0014 sec

40
MATLAB and SimulinkLecture 140 Script file / Commando file When a script file executes the same result is made if all commands would be written in the command window A collection of statements All variables created will be stored in the same workspace. Have access to all variables in the workspace No input argument Returns no result Communication between script files through data stored in the workspace

41
MATLAB and SimulinkLecture 141 Suitable Exercises on this days topics 2.1, 2.4, 2.6, 2.10, 2.14, 2.16, 2.18 3.3(2), 3.4(3), 3.14(11), 3.15(12), 3.16(13) 4.24(19), 4.26(21), 4.29(22) The exercise numbers are referring to the book MATLAB Programming for engineers, third edition (second edition).

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google