Docsity

MATLAB Basics: Learning Goals, Environment, Scripts and Functions, Arithmetic Operations, , Slides of Calculus for Engineers

Partial preview of the text

Download MATLAB Basics: Learning Goals, Environment, Scripts and Functions, Arithmetic Operations, and more Slides Calculus for Engineers in PDF only on Docsity!

Engr/Math/Physics 25

Chp1 MATLAB

OverView: Part-

Learning Goals

  • Turn On MATLAB and use as a calculator
  • Create Basic Cartesian Plots
  • Write and Save simple “Script” Program-

files

  • Execute Conditional Statements
    • IF, THEN, ELSE, >, <, >=, etc.
  • Execute Loop Statements
    • FOR & WHILE

>> %Use MATLAB As Example Cmd Window Session Calculator

>> 17 ans = 323*

>> 77/19 -4. ans = -0.

>> 64^(1/3) + 32^0. ans = 6

*>> (7+11)2. ans = 45

L = 14. L =

W = 13. W =

Area = LW Area = 191.*

Script & Function Files (m-files)

  • SCRIPTS and FUNCTIONS in MATLAB are

stored in text files that end with the extension

“.m”

  • These files are called m-files
  • SCRIPTS (a.k.a. “programs”)
  • Scripts files are useful for automating tasks that

may need to be repeated.

  • They have no input/output parameters
  • They can (but probably shouldn’t) share variables

with the command workspace

Script & Function Files (m-files)

  • SCRIPTS (cont.)
    • Scripts are sequences of interactive statements stored in a file - i.e., They look liked Stored versions of Command Window Sessions
  • FUNCTIONS (a.k.a. “subroutines”)
    • Function m-files are MATLAB subprograms analogous to FORTRAN Subroutines, or C functions
    • They communicate with the command window and other functions via a list of INPUT and OUTPUT PARAMETERS

Entering Commands & Expressions

  • MATLAB retains your previous keystrokes.
  • Use the up-arrow (↑) key to scroll back through

the commands.

  • Press the key (↑) once to see the previous entry,

and so on.

  • Use the down-arrow (↓) key to scroll forward.
  • Edit a line using the left (←) & right (→) arrow

keys the Backspace key, and the Delete key.

  • Press the Enter key to execute the command

Arithmetic Scalar Operations

Symbol Operation MATLAB

^ exponentiation:^ ab^ a^b

* multiplication:^ ab^ a*b

/ right division:^ a/b^ a/b

\ left division:^ b/a^ a\b

+ addition:^ a^ +^ b^ a + b

  • subtraction:^ a^ -^ b^ a - b

 LEFT-Division A\ b read from

Right-to-Left as: “b divided by A ”C

Precedence Examples

**ans = 23

8 + (3*5) ans =

23

(8 + 3)* ans = 55**

>>4^2128/4*

**ans = 0

4^2128/(42) ans = 3*

Precedence Examples cont.

>> 3*4^2 + 5

**ans = 53

(34)^2 + 5 ans = 149*

>>27^(1/3) + 32^0.

**ans = 5

27^1/3 + 32^0. ans = 11**

Work Session Commands

Command Description

clc (^) Clears the Command window clear (^) Removes all variables from memory clear v1 v2 Removes the variables v1 and v from memory exist(‘var’) (^) Determines if a file or variable exists having the name „var‟ quit (^) Stops MATLAB

Work Session Commands cont.

Command Description

who Lists the variables currently inmemory

whos

Lists the current variables and sizes, and indicates if they have imaginary parts. : (Colon) Generates an array having regularlyspaced elements , (Comma) (^) Separates elements of an array

; (Semicolon) Suppresses screen printing; alsodenotes a new row in an array

… (Ellipsis) (^) Continues a line

Special VARS & const’s

Command Description

ans Temporary variable containing the mostrecent answer

eps Specifies the accuracy of floating pointprecision i,j (^) The imaginary unit (-1) Inf (^) Infinity (unbounded magnitude) NaN Indicates an undefined numerical result;a.k.a., N ot a N umber pi (^) The number pi (3.14159...)  NaN returns the IEEE arithmetic representation for Not-a-Number (NaN). These result from operations which have undefined numerical results;. e.g., try Q = 0/

The Complex Plane

Im (i or j)

Re

FABi or FABj