Top Banner
CMPS 1371 Introduction to Computing for Engineers MatLab
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CMPS 1371 Introduction to Computing for Engineers MatLab.

CMPS 1371Introduction to

Computing for Engineers

MatLab

Page 2: CMPS 1371 Introduction to Computing for Engineers MatLab.

Getting Started

In Windows click on the following: Programs

Math & Science MatLab R2008a

MATLAB 7.0.lnk

Page 3: CMPS 1371 Introduction to Computing for Engineers MatLab.

Getting Started

MATLAB opens to a default window configuration

Page 4: CMPS 1371 Introduction to Computing for Engineers MatLab.

MATLAB uses a standard windows menu bar

To exit MATLAB use the close icon

Page 5: CMPS 1371 Introduction to Computing for Engineers MatLab.

Command Window

Enter commands at the promptCurrent Directory

MATLAB WindowsCommand History

Workspace Window

Page 6: CMPS 1371 Introduction to Computing for Engineers MatLab.

MATLAB Windows

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

They are not necessarily all open at once

Page 7: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 8: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 9: CMPS 1371 Introduction to Computing for Engineers MatLab.

Command History

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

Page 10: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 11: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 12: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 13: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 14: CMPS 1371 Introduction to Computing for Engineers MatLab.

Document Window

Page 15: CMPS 1371 Introduction to Computing for Engineers MatLab.

Figure Window

When Figures are created a new window opens

It’s extremely easy to create graphs in MATLAB

Page 16: CMPS 1371 Introduction to Computing for Engineers MatLab.

The semicolon suppresses the output from each command

Page 17: CMPS 1371 Introduction to Computing for Engineers MatLab.
Page 18: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 19: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 20: CMPS 1371 Introduction to Computing for Engineers MatLab.

Save and Run

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

Page 21: CMPS 1371 Introduction to Computing for Engineers MatLab.

Solving Problems

We’ve already solved some simple problems

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

Page 22: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 23: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 24: CMPS 1371 Introduction to Computing for Engineers MatLab.

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'

Page 25: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 26: CMPS 1371 Introduction to Computing for Engineers MatLab.

Scalar Calculations

You can use MATLAB like you’d use a calculator

>> 9 + 10

ans=19

Command Prompt

Result

Page 27: CMPS 1371 Introduction to Computing for Engineers MatLab.

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

Page 28: CMPS 1371 Introduction to Computing for Engineers MatLab.

Order of Operation

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

Parentheses first Exponentiation Multiplication / division Addition / subtraction

Page 29: CMPS 1371 Introduction to Computing for Engineers MatLab.

Order of Operation

5*(3+6) = 45

5*3+6 = 21

White space does not matter!!!

5*3 + 6 = 21

Page 30: CMPS 1371 Introduction to Computing for Engineers MatLab.

Parentheses

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

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

Page 31: CMPS 1371 Introduction to Computing for Engineers MatLab.

Compute from left to right

5*6/6*5 = 25

5*6/(6*5) = 1

Page 32: CMPS 1371 Introduction to Computing for Engineers MatLab.

Here’s an exampleFind the surface area of a cylinder

)(222 2 hrrrhrSA

r = radiusr = 5

h = heighth = 10

π r2

π r2

2π r * h

Page 33: CMPS 1371 Introduction to Computing for Engineers MatLab.
Page 34: CMPS 1371 Introduction to Computing for Engineers MatLab.

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