matlab programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/classes/met164/... · met 164...

24
1/24 MET 164 Matlab Programming

Upload: vananh

Post on 17-Mar-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

1/24MET 164

Matlab Programming

Page 2: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

2/24MET 164

What does MATLAB mean?

Contraction of Matrix Laboratory

• Matrices are rectangular arrays of

numerical values

7362

1944

8415

7213

Page 3: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

3/24MET 164

What are the fundamental components of Matlab?

Computing system that accepts one instruction at a time in

text form

Large library of modules that provide high-level capabilities for

processing data

Graphical user interface (GUI) that lets users assemble and

implement programs to solve a problem

Page 4: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

4/24MET 164

What are the advantages and the disadvantages?

Advantages

interpreted language

numerical calculator

built in graphics

Disadvantages

does not work well for large computing projects (many

developers)

professional GUIs are best written in compiled language

Page 5: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

5/24MET 164

Programming Language Background

Abstraction

“expressing a quality apart from a particular

implementation”

Algorithm

“sequence of instructions for solving a sub-program”

Programming Paradigms

“a set of assumptions, concepts, values, and practices that

constitutes a way of viewing reality for the community that

shares them, especially in an intellectual discipline”

Page 6: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

6/24MET 164

What does “expressing a quality apart from a particular

implementation” (abstraction) mean?

It is things that we use in everyday conversation

“He used his bicycle to ride home from campus”

Procedural abstraction: the details will be different based on

the user to instruct the ride home.

“To convert from lb to N, multiply the force by 4.448”

Data abstraction: details about the force will determine how

you do the conversion.

Page 7: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

7/24MET 164

What does “sequence of instructions for solving a sub-

program” (algorithm) mean?

Instructions that we use to problem solve

Problem: The car needs to be fixed

1) Change the oil

2) Drain the old oil, remove the old oil filter, install a new

oil filter, fill the old pan with new oil

3) Place the vehicle on ramps, place a container under the

oil pan to catch the old oil, etc.

The algorithm is full of abstractions that are based on what is computing the algorithm

Page 8: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

8/24MET 164

Programming Paradigms

You will have a better understanding of programming

paradigms after you have learned basic programming

Page 9: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

9/24MET 164

Now, let’s start talking about “basic” data manipulation

within Matlab

z = 4x - y

In algebra, this is a two way relationship

In algebra this represents a constraint on the values, but in

programming, this is an assignment statement

z = x + y

In programming, this is the sum of two values associated to a

new name; it can be changed with the next statement

x + y = z

Page 10: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

10/24MET 164

When we start to define data to variables in Matlab, we

need to use appropriate syntax

Variables

upper or lowercase letters, numbers, and “_”

not “$” or 64 unique characters

Variables may be words

no spaces

use underscore or caps

velocity_feet_per_second

VelocityFeetPerSecond

Page 11: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

11/24MET 164

Matlab is an untyped language

Variables are given a specific “type” when assigned

Numeric (double)

x = 7

Character (char)

x = ‘I like programming’

Issues

misspelled variables may cause errors (without notice)

logic errors with incompatible data

Typed languages (weak or strong) allow the programmer to

assign types and interactions before running or compilation

Page 12: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

12/24MET 164

Now, let’s look at Matlab and talk about some of the

things we see

Command Window

Current FolderWorkspace Tab

Command History

This layout may be adjusted to the user preference

Page 13: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

13/24MET 164

One way to adjust the layout is to use the layout feature

on the top window

Page 14: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

14/24MET 164

Matlab can be used in two modes: Command mode or

Edit mode

Command mode

appears as a scientific calculator

Edit mode

able to type in numerous commands before executing

Page 15: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

15/24MET 164

Matlab can be used in two modes: Command mode or

Edit mode

Command mode

appears as a scientific calculator

Edit mode

able to type in numerous commands before executing (use

what are called scripts)

Page 16: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

16/24MET 164

The workspace provides a list of variables always keeps

the variables in alphabetical order

The “clear” command allows you to clear which ever

workspace variable you want. Example “clear AA BB” to

clear variable AA and BB or “clear all” to clear all of the

variables (note: the quotes are not used)

“ans” is a special

variable

Page 17: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

17/24MET 164

The workspace always keeps the variables in

alphabetical order

Double clicking on a variable in the workspace will allow you

to see the variable and change its value

You may also paste the data here from another source

Page 18: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

18/24MET 164

The Command History pane will allow you to fix

mistakes that you have made

The “clc” command allows you to clear all of the stuff that is

in the command window, but leaves all of the history and

workspace variables to use later

“whos” will allow you to see all of the variable information in

the command window

Page 19: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

19/24MET 164

The output will have specific formats in the Command

Window

A semicolon will suppress the presentation of the result

Page 20: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

20/24MET 164

Matlab has built in figure windows that allows you to plot

data

close, figure

Page 21: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

21/24MET 164

Matlab has a built in text editor that allows you save a

script for running later

Page 22: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

22/24MET 164

Matlab has a built in text editor that allows you save a

script for running later

The result will show in the Command Window

Page 23: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

23/24MET 164

Additional syntax

;

close

clear

clc

^

plot

who

whos

%

Page 24: Matlab Programming - web.ics.purdue.eduweb.ics.purdue.edu/~rvoyles/Classes/MET164/... · MET 164 3/24 What are the fundamental components of Matlab? Computing system that accepts

24/24MET 164

Use the text editor to write your scripts and start using

comments

Start using the text editor

Start using comments

Try different commands and see what happens