360 likes | 377 Views
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. OBJECTIVES.
E N D
Matlab Programming for Engineers Introduction to Matlab Matlab BasicsBranching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Nidal Farhat
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
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=) Some relational operations and their results
RELATIONAL OPERATORS (==, ~=, >, < , >=, <=) Be careful because of roundoff errors Compare array by a value Compare array by another one (of the same size)
LOGIC OPERATORS EXAMPLE
Branches: a) if Statement
BRANCHES (if-STATEMENT) Optional
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:
BRANCHES (if-STATEMENT) Example: evaluating a Function of Two Variables Test it using the following (x,y) values:
BRANCHES (if-STATEMENT) Nested if-Statements It is preferable to use if-else-end statement whenever is possible
Branches: b) switch Statement
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
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.
BRANCHES (switch-STATEMENT) Example:
BRANCHES (switch-STATEMENT) Example: Example
BRANCHES (switch-STATEMENT) Example
Branches: b) try/catch Statement
BRANCHES (try/catch) • Thetry/catchstatement is designed to trap errors. • In MATLAB when an error is encountered while running the program the program execution isaborted. • Usetry/catchstatement to run a part of the program susceptible to produce error when executed. MATLAB willtry to execute this part. If an error is produced, then instead of aborting, it executes the part of the program found in thecatchstatement. • If no error is produced in thetry block, thecatchblock will not be executed. • The programmer can handle errors in the program without causing the program to stop.
BRANCHES (try/catch) Example
fprintf command fprintf(format,data)
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
Additional plotting features Plotting Multiple Plots on the Same Axes OR
Additional plotting features Creating Multiple Figures
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]
Additional plotting features Enhanced Control of Plotted Lines plot(x,y,'PropertyName',value,…)
Additional plotting features Enhanced Control of Plotted Lines Example,
Additional plotting features Enhanced Control of Text Strings
Additional plotting features Enhanced Control of Text Strings
Additional plotting features Enhanced Control of Text Strings Examples:
Additional plotting features Polar Plots polar(theta,r) Example,
Additional plotting features Polar Plots polar(theta,r) Solution: