Download presentation
Presentation is loading. Please wait.
1
Matlab Programming for Engineers
Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Nidal Farhat
2
Relational Operators (==, ~=, >, < , >=, <=)
OBJECTIVES The Logical Data Type Relational Operators (==, ~=, >, < , >=, <=) Logic Operators (&, &&, |, ||, xor, ~) Logical Functions (ischar, isempty, ….) Branches (if-else) Statement switch Statement try-catch Statement Additional Plotting Features
3
THE LOGICAL DATA TYPE (true, false)
4
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)
Some relational operations and their results
5
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=)
Be careful because of roundoff errors Compare array by a value Compare array by another one (of the same size)
6
LOGIC OPERATORS Operator Operation &
Logical AND (scalar/array, both sides evaluation) && Logical AND (scalar, left then right side evaluation) | Logical inclusive OR (scalar/array both sides evaluation) || Logical inclusive OR (scalar, left then right side evaluation) xor Logical exclusive OR ~ Logical NOT
7
LOGIC OPERATORS EXAMPLE
8
LOGICAL FUNCTIONS
9
QUIZ!
10
Branches: a) if Statement
11
BRANCHES (if-STATEMENT)
Optional
12
BRANCHES (if-STATEMENT)
Example: Write a program that determine whether a quadratic equation: has real (identical/different) or complex roots. (Use input command for entering the coefficients (a,b,c) and disp commands to display the output). Write a program that calculates the roots in each case. (Use input command for entering the coefficients of the equation and disp commands to display the output). Test the program on the following equations:
13
BRANCHES (if-STATEMENT)
Example: evaluating a Function of Two Variables Test it using the following (x,y) values:
14
BRANCHES (if-STATEMENT)
Nested if-Statements It is preferable to use if-else-end statement whenever is possible
15
Branches: b) switch Statement
16
BRANCHES (switch-STATEMENT)
The switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives. The switch_expr and case_expr may be a scalar or a string Optional
17
BRANCHES (switch-STATEMENT)
Remarks: The case expression could be more than one enclosed by {} At most one code block can be executed. If the switch_exp matches more than case_exp the first one is executed.
18
BRANCHES (switch-STATEMENT)
Example:
19
BRANCHES (switch-STATEMENT)
Example: Example
20
BRANCHES (switch-STATEMENT)
Example
21
b) try/catch Statement
Branches: b) try/catch Statement
22
BRANCHES (try/catch) The try/catch statement is designed to trap errors. In MATLAB when an error is encountered while running the program the program execution is aborted. Use try/catch statement to run a part of the program susceptible to produce error when executed. MATLAB will try to execute this part. If an error is produced, then instead of aborting, it executes the part of the program found in the catch statement. If no error is produced in the try block, the catch block will not be executed. The programmer can handle errors in the program without causing the program to stop.
23
BRANCHES (try/catch)
24
BRANCHES (try/catch) Example
25
fprintf command fprintf(format,data)
26
Additional plotting features
Example f(x) = sin(x), from -2π to 2π Then restrict the axes to the region 0 ≤ x ≤ π, and 0 ≤ y ≤ 1
27
Additional plotting features
Plotting Multiple Plots on the Same Axes OR
28
Additional plotting features
Creating Multiple Figures
29
Additional plotting features
Subplots Subplot(m,n,p) ≡ creates mxn subplots in the current figure (array of subplots), m rows and n columns. p is the current subplot. p=1 p=2 p=3 p=4 p=[5 6]
30
Additional plotting features
Enhanced Control of Plotted Lines plot(x,y,'PropertyName',value,…)
31
Additional plotting features
Enhanced Control of Plotted Lines Example,
32
Additional plotting features
Enhanced Control of Text Strings
33
Additional plotting features
Enhanced Control of Text Strings
34
Additional plotting features
Enhanced Control of Text Strings Examples:
35
Additional plotting features
Polar Plots polar(theta,r) Example,
36
Additional plotting features
Polar Plots polar(theta,r) Solution:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.