1 / 36

Matlab Programming for Engineers

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.

dolmsted
Télécharger la présentation

Matlab Programming for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  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. Branches: b) try/catch Statement

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

  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:

More Related