cmps 1371 introduction to computing for engineers matlab

34
CMPS 1371 Introduction to Computing for Engineers MatLab

Upload: maude-fowler

Post on 03-Jan-2016

235 views

Category:

Documents


4 download

TRANSCRIPT

CMPS 1371Introduction to

Computing for Engineers

MatLab

Getting Started

In Windows click on the following: Programs

Math & Science MatLab R2008a

MATLAB 7.0.lnk

Getting Started

MATLAB opens to a default window configuration

MATLAB uses a standard windows menu bar

To exit MATLAB use the close icon

Command Window

Enter commands at the promptCurrent Directory

MATLAB WindowsCommand History

Workspace Window

MATLAB Windows

MATLAB uses several different windows to display data, commands and results.

They are not necessarily all open at once

Command Window

Similar to a scratch pad Once you hit enter, you can’t edit any

commands You can retype them or use the arrow keys to

retrieve commands and edit them before hitting enter again

Command Window

Command History

Records the commands you issue in the command window

When you exit the command window, or when you issue the clc command, the command window is cleared

But the command history remainsCommand History

Command History

You can transfer commands from the command history to the command window Double click on a command Click and drag

Workspace Window

Keeps track of the variables you’ve defined Name Value Class Size Bytes

You may need to click on the name bar and select size and bytes in order to see these parameters

Workspace Window

When you define variables in the command window, they are listed in the workspace window

Current Directory

The current directory window is a list of files When you try to load information from a file or

try to save information – MATLAB uses the current directory

Document Window

If you double click on any variable in the workspace window MATLAB launches a document window containing the array editor

You can edit variables in the array editor

Document Window

Figure Window

When Figures are created a new window opens

It’s extremely easy to create graphs in MATLAB

The semicolon suppresses the output from each command

Editing Window

This window allows you to type and save a series of commands without executing them

There are several ways to open an editing window From the file menu With the new file icon

Open an editing window from the file menu or with the new file icon

Save and Run

Write your code in the editing window, then run it using the Save and Run icon

Solving Problems

We’ve already solved some simple problems

We need to understand how MATLAB works to solve more complicated problems

Variables

MATLAB allows you to assign a value to a variable

A=3 Should be read as A is assigned a

value of 3 Use the variables in subsequent

calculations

Naming Variables

All names must start with a letter They may contain letters, numbers and

the underscore ( _ ) Names are case sensitive There are certain keywords you can’t

use

Keywords

Use the iskeyword function for a list of keywords

>>iskeywordans =

'break' 'case' 'catch'

'continue' 'else'

'elseif' 'end' 'for'

'function'

'global' 'if' 'otherwise' 'persistent' 'return' 'switch' 'try' 'while'

Which of these names are allowed in MATLAB?

test Test if my-book my_book Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log

x

x

x

x

x x

bad idea

Scalar Calculations

You can use MATLAB like you’d use a calculator

>> 9 + 10

ans=19

Command Prompt

Result

Assignment Operator

To define a variable a we might type

a=1+2

which should be read as:“a” is assigned a value of 1+2

Order of Operation

Same as you’ve learned in math class Same as your calculator

Parentheses first Exponentiation Multiplication / division Addition / subtraction

Order of Operation

5*(3+6) = 45

5*3+6 = 21

White space does not matter!!!

5*3 + 6 = 21

Parentheses

Use only ( ) { } and [ ] mean something different MATLAB does not assume operators

5 * (3+4) not 5(3+4)

Compute from left to right

5*6/6*5 = 25

5*6/(6*5) = 1

Here’s an exampleFind the surface area of a cylinder

)(222 2 hrrrhrSA

r = radiusr = 5

h = heighth = 10

π r2

π r2

2π r * h

Try some

Type each expression on the command line and then press Enter >> 7 + 8 / 2 >> (7 + 8) / 2 >> 5 ^ 3 / 2 >> sqrt(64) >> x = 15 >> x = 3 * x – 12 >> y = pi >> z = y * 2

>> A = 12; >> B = 4; >> C = A / B + A – B * 2; >> C