29
Welcome to EGR 106 Foundations of Engineering II Course information Today’s specific topics: Computation and algorithms MATLAB Basics Demonstrations Material in textbook chapter 1

Welcome to EGR 106 Foundations of Engineering II

  • Upload
    nenet

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

Welcome to EGR 106 Foundations of Engineering II. Course information Today’s specific topics: Computation and algorithms M ATLAB Basics Demonstrations Material in textbook chapter 1. Computation. What is computation? Examples:3 + 2 tan 40 o - PowerPoint PPT Presentation

Citation preview

Page 1: Welcome to EGR 106  Foundations of Engineering II

Welcome to EGR 106 Foundations of Engineering II

Course information Today’s specific topics:

– Computation and algorithms– MATLAB

Basics Demonstrations

Material in textbook chapter 1

Page 2: Welcome to EGR 106  Foundations of Engineering II

Computation

What is computation?

Examples: 3 + 2

tan 40o

Computation = “transformation from one or more inputs to an output”

Page 3: Welcome to EGR 106  Foundations of Engineering II

Human Computation

“Simple” computations (trivial to us)– Adding 2 single digit numbers– Recognizing a letter of the alphabet– Comparing 2 numbers for <, =, >

“Harder” computations (based on simple)– Adding 3 or more numbers – Reading a word– Sorting a list of numbers

Page 4: Welcome to EGR 106  Foundations of Engineering II

Algorithms

Definition: “a set of directions for carrying out a computation in terms of other, simpler computations”

“Simpler computations” = ones that we already know how to do

Page 5: Welcome to EGR 106  Foundations of Engineering II

Example

Memorizing addition tables in grammar school

0 1 2 3 4 5 6 7 8 9

0 0 1 2 3 4 5 6 7 8 9

1 1 2 3 4 5 6 7 8 9 10

2 2 3 4 5 6 7 8 9 10 11

3 3 4 5 6 7 8 9 10 11 12

4 4 5 6 7 8 9 10 11 12 13

5 5 6 7 8 9 10 11 12 13 14

6 6 7 8 9 10 11 12 13 14 15

7 7 8 9 10 11 12 13 14 15 16

8 8 9 10 11 12 13 14 15 16 17

9 9 10 11 12 13 14 15 16 17 18

Page 6: Welcome to EGR 106  Foundations of Engineering II

Permits multi-digit addition

Page 7: Welcome to EGR 106  Foundations of Engineering II

Computer Computation

Very fast at arithmetic operations Algorithms = computer programs

– Need to understand what computations are “simple” for the computer

– Need to write a clear set of directions to be followed

– Build more complicated computations from intermediate ones

Page 8: Welcome to EGR 106  Foundations of Engineering II

Examples

Find the smallest in a list of number Sort a list of numbers Some for discussion/thought

(first two from Kaplan, Introduction to Scientific Computation and Programming)

Page 9: Welcome to EGR 106  Foundations of Engineering II

Find the Smallest of a List:

S et P =1, S =first num ber on list

S et S equal to the sm aller of S and theP th num ber on the list

Done withlist?

S top

no

yes

S tart with the list and 2 variab les, P and S

Increase P by 1

Page 10: Welcome to EGR 106  Foundations of Engineering II

Example: list = 7,1,5

Step P S

1 1 S = 7

2 2

3 S = smaller(7,1) = 1

4 3

5 S = smaller(1,5) = 1

6 stop

Page 11: Welcome to EGR 106  Foundations of Engineering II

Sort a List of Numbers:

S tart with your list and an em pty list

Let S = the sm allest num ber in the list

Done withlist?

S top

no

yes

Delete S from the old list;A ppend S to the new list

Note reuse!

Page 12: Welcome to EGR 106  Foundations of Engineering II

Example: list = 8,5,1,2

Step S Orig List New List

1 1

2 8,5,2 1

3 2

4 8,5 1,2

5 5

6 8 1,2,5

7 8

8 1,2,5,8

Page 13: Welcome to EGR 106  Foundations of Engineering II

Some Others….

Adding numbers expressed in Roman numerals

LXVI + XXXIV = ???

Find the 2nd smallest number in a list Convert the month/day into day of the year

Feb 15 = day 46

Page 14: Welcome to EGR 106  Foundations of Engineering II

MATLAB – What is it ?

Name is from matrix laboratory Powerful tool for

– Computation and visualization of engineering and science mathematics

– Communication of ideas– Programming:

Built-in editor, debugger, and help Many predefined functions (grouped in toolboxes) Interpreted or compiled programs

Page 15: Welcome to EGR 106  Foundations of Engineering II

Today is “beginning MATLAB”, sort of like “beginning French”:

– We start with basic terminology– We consider the simplest of computations– We do computation in interpreter mode (the

“enter” key invokes/runs/executes the operation requested)

Chapter 1 of Gilat– Pages 5-22

Page 16: Welcome to EGR 106  Foundations of Engineering II

The MATLAB Environment

Data represented in arrays– Organized by row and column indices– Use variable names for them

Multi-paned desktop: – Command window– Workspace browser– Current directory– Other windows: Figure, File Editor, Help, …..

More next week

Page 17: Welcome to EGR 106  Foundations of Engineering II
Page 18: Welcome to EGR 106  Foundations of Engineering II

The Command Window

Command prompt >> Basic math operations are available:

addition + subtraction – division /

multiplication * exponentiation ^

“enter” key “executes” or “runs” or “invokes” the operation

Operator precedence: PEMDAS5 – 4 + 3 ^ 4 / ( 3 – 1 ) = ?

Page 19: Welcome to EGR 106  Foundations of Engineering II
Page 20: Welcome to EGR 106  Foundations of Engineering II

Finite precision mathematics !!

By default, 5 significant digits are shown, with exponential notation as needed

Results of NaN, Inf, possible

Page 21: Welcome to EGR 106  Foundations of Engineering II

Allows Stored Variables

The equal sign is an assignment operator c = 7.5 bob3 = 3.7789

There are naming restrictions:Connected symbols, starting with a letter Make them unique Some are predefined for special values or

uses: pi inf flops j i ans for

Page 22: Welcome to EGR 106  Foundations of Engineering II

Combining Operations and =

Generally, computation requires 3 pieces of information:– The operator?– The inputs?– What to do with the output?

MATLAB storing the result in a variable Accomplished by the equal sign, =

Specified on the right hand side of an equal sign

Page 23: Welcome to EGR 106  Foundations of Engineering II

For these examples, 2 and 4 are the input and addition is the operation

No specification of the output; the default is to put it into the variable named ans

The output is assigned to the variable named bob

bob is again the destination of the result, we’ve just used functional style notation for the computation

Page 24: Welcome to EGR 106  Foundations of Engineering II

Note that = is not really an equal sign, but is an assignment operator

The computation on the right can be trivial

Here the computation is done using bob, then the result is put into variable bob

An error results since we’ve got things on the wrong sides of the assignment operator

Page 25: Welcome to EGR 106  Foundations of Engineering II

Other Useful Operations

abs(x) ceil(x) exp(x) fix(x) sign(x) floor(x) log(x) round(x) sqrt(x) conj(x) log10(x) rem(x,y) sin(x)sinh(x) tan(x) atan2(x,y) asin(x) acosh(x) atan(x) sec(x) sind(x) many more exist !!

Page 26: Welcome to EGR 106  Foundations of Engineering II

Examples

Square roots

Note that trig functions generally work in radians, not degrees

In general, all variables are complex numbers

Page 27: Welcome to EGR 106  Foundations of Engineering II

Bits and Pieces

Other useful system commands: – clear, clc – diary – help, lookfor – who, whos

Semicolon (;) suppresses the displaying of the result of a computation

Arrow keys allow for editing of prior commands PC version (network license) is available from ECC

help desk for $10

Page 28: Welcome to EGR 106  Foundations of Engineering II

Plotting Basics

Figure window commands:

figure, figure(3), clf, close plot(x,y) in which x and y are “arrays” Annotation commands:

title('the title goes here')

xlabel('the x axis label goes here')

ylabel('the y axis label goes here')

Page 29: Welcome to EGR 106  Foundations of Engineering II

Now for Some Demos!

Simple demos of variables and math “Demos” at the command line A simple gui of a bouncing ball