introduction to matlab
Download
Skip this Video
Download Presentation
Introduction to MATLAB

Loading in 2 Seconds...

play fullscreen
1 / 30

Introduction to MATLAB - PowerPoint PPT Presentation


  • 154 Views
  • Uploaded on

Introduction to MATLAB. Alternative MATLAB Introduction Lecture authored by Annie Abell. Introduction to MATLAB. MATLAB is a powerful program for numerical computations, plotting and programming . Use it as a calculator. Define variables and use them in calculations.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introduction to MATLAB' - domani


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to matlab

Introduction to MATLAB

Alternative MATLAB Introduction Lecture authored by Annie Abell

introduction to matlab1
Introduction to MATLAB

MATLAB is a powerful program for numerical computations, plotting and programming.

  • Use it as a calculator.
  • Define variables and use them in calculations.
  • Use built-in functions.
  • Plot graphs.
  • Write and run computer programs.
  • Perform symbolic mathematics.
slide3

Open MATLAB now:

Click on the shortcut icon

or select: start / All programs / MATLAB

There will be a fairly long initialization process, then following prompt should appear in the command window:

>>

slide4

Command prompt

Workspace (variable values)

Current Directory (lists file, etc.)

Command History (what has been typed)

Command Window (where commands are entered)

matlab s working directory
MATLAB’s Working Directory

When you first start MATLAB it is a good practice to change the working directory to your Z: drive or USB device.

Keep all of your files in the same folder so you do not have to constantly change your directory

slide6

Order of Precedence

Higher-precedence operations are executed before lower-precedence operations (like Excel or graphing calculators).

If two operations have the same precedence, then the expression is executed from left to right.

PRECEDENCEOPERATION

First Parentheses (innermost pair first)

Second Exponentiation.

Third Multiplication and division

Fourth Addition and subtraction

slide7

Precedence of Operations

Task : Find the average of 7 and 8

>> (7+8)/2

ans =

7.500

… correct! 

>> 7 + 8 / 2

ans =

11

…not what we wanted 

slide8

Precedence of Operations

Task: Add the cube root of 27 to the 5th root of 32

>> 27 ^ 1/3 + 32 ^ 0.2

ans =

11 … not what we wanted 

>> 27 ^ (1/3) + 32 ^ 0.2

ans =

5

slide9

Variable Definition

>> x = 5

x =

5

>> x = x + 5

x =

10

This statement does not make any mathematical sense but in programming language it changes the value of ‘x’ to a new value.

slide10

Calculations with Variables

We can use the variables defined previously to do calculations.

Define the following: a=8, my_var =12

>> a + my_var

ans =

20

>> a * my_var^2

ans =

1152

slide11

Rules About Variable Names

  • Can be up to 63 characters long.
  • Must begin with a letter.
  • Can contain letters, digits, and the underscore character (no spaces).
  • MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters.
  • Avoid naming variables with names that are used by MATLAB: exp, sin, cos, sqrt, length, mean, max, min…
slide12

Examples of Variable Names

Acceptable:

My_name

MyName

myname1

Unacceptable:

1Name starts with numeral

My Name spaces not allowed

My-Name special characters not allowed

slide13

Built-In Math Functions: Examples

exp(x) exponential (ex)

log(x)natural logarithm (log base \'e\')

log10(x)base 10 logarithm (log)

sqrt(x) square root

abs(x) absolute value

slide14

Built-In Math Functions

In MATLAB, trigonometric functions use radians. So for these examples, x is in radians:

  • sin(x), cos(x), asin(x), acos(x), tan(x), cot(x)

MATLAB also has equivalent functions where x is in degrees:

  • sind(x), cosd(x), tand(x), etc
slide15

Built-In Math Functions: Examples

Task: calculate sin(∏/ 2). Note: ∏ is defined by MATLAB as pi.

>> sin(pi/2)

ans =

1

slide16

Built-In Math Functions: Examples

Task: calculate e2

>> e ^ 2

??? Undefined function or variable \'e\'

How do we fix this?

>> exp(2)

ans =

7.3891

slide17

Built-In Math Functions: Examples

Task: calculate sin2x, where x = ∏/4

>> x = pi/4

x =

0.7854

>>sin(2*x)

ans =

1

slide18

Built-In Math Functions: Examples

Task: Using (cos2xsin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.

>> x = 30

x =

30

>> x = x * pi / 180

x =

0.5236

slide19

Built-In Math Functions: Examples

Task: Using (cos2x sin2x + tan(x/2)) / e3x , find the value for x = 30 degrees.

>> (cos(2*x)* sin(x)^2 + tan(x/2)) / exp(3*x)

ans =

0.0817

slide20

Saving a Script File

  • Once the script file is completed, it must be saved. In our class use Save As to your Z:\ or your own USB drive.
  • This should be the same as the working directory you specified when you started MATLAB.
  • ALWAYS SAVE YOUR WORK. Back it up in multiple places for safety! To the cloud!!
slide21

Saving a Script File

Valid File Names

  • Prob1a.m
  • Prob1a.m
  • problemone.m

Invalid File Names

Prob-1a.m Special Characters not allowed

Prob 1a.m Spaces not allowed

1aProb.m Cannot start with number

slide22

Useful Programming Commands

To clear the command window: clc

>> clc

To clear all variables previously defined: clear

>> x = 5

x =

5

>> clear

>> x

??? Undefined function or variable \'x\'

slide23

The Display Function: disp()

This function displays a message to the command window. It can be a string of text or variablecontents.

To display your message as text,use single quotes around your message:

>> disp(‘Brutus Buckeye’)

Brutus Buckeye

slide24

The Display Function: disp()

To display any variable contents, do not use quotes:

>> x = 5

x =

5

>> disp(x)

x =

5

slide25

Suppressing Output

This is good practice! You don\'t need all of your program displayed in the command window, just the relevant info.

>> x = 6

x =

6

>> y = 7; …what happens when you type this?

slide26

Script File Example

Pythagoras\' Theorem: Hypotenuse2 = Height2 + Base2

  • Create a script file and name it Pyth.m
  • Clear the command window
  • Clear all variables from memory
  • Display your name and seat number
  • Declare height of triangle as 3 units
  • Declare base of triangle as 4 units
  • Find the hypotenuse of the triangle

Hypotenuse

Height

Base

slide27

How To Turn In All Assignments

You must print two things for every MATLAB assignment:

  • Script file
  • Command window output

… if you do not turn in both, you will lose points 

slide28

Example Script File Printout

Example Command Window Printout

slide29

Self Check… Do you understand these concepts?

  • Arithmetic Operations with scalars: 5+3, 5*3 , 5^3
  • Precedence of operations: 7+8/2 vs. (7+8)/2
  • Variable assignment: a=8, b=12
  • Calculations with variables: a+b, a*b
  • Important commands: clc, clear
  • Use of semi colons (;) to suppress output
  • Display function: disp(‘Brutus Buckeye’)
  • Mathematical functions: sin(pi/2) , exp(2)
  • Calculate the value of (cos2xsinx + tan(x/2)) / exp(3x) for x = 30 degrees
slide30

Homework Assignment

MAT-01

Introduction to Matlab

ad