Programming in MATLAB Dr. Mihail August 28, 2018 (Dr. Mihail) - - PowerPoint PPT Presentation

programming in matlab
SMART_READER_LITE
LIVE PREVIEW

Programming in MATLAB Dr. Mihail August 28, 2018 (Dr. Mihail) - - PowerPoint PPT Presentation

Programming in MATLAB Dr. Mihail August 28, 2018 (Dr. Mihail) Programming August 28, 2018 1 / 12 The Process What does programming (in any language) involve? (Dr. Mihail) Programming August 28, 2018 2 / 12 The Process What does


slide-1
SLIDE 1

Programming in MATLAB

  • Dr. Mihail

August 28, 2018

(Dr. Mihail) Programming August 28, 2018 1 / 12

slide-2
SLIDE 2

The Process

What does programming (in any language) involve?

(Dr. Mihail) Programming August 28, 2018 2 / 12

slide-3
SLIDE 3

The Process

What does programming (in any language) involve?

Analysis

Understanding the problem. Figure out exactly the problem to be solved. Clearly define the input(s) and output(s). Solve by hand first. This step is useful to generate test data later.

(Dr. Mihail) Programming August 28, 2018 2 / 12

slide-4
SLIDE 4

The Process

What does programming (in any language) involve?

Analysis

Understanding the problem. Figure out exactly the problem to be solved. Clearly define the input(s) and output(s). Solve by hand first. This step is useful to generate test data later.

Design

Formulate the overall structure of the program. The “how” of the program gets worked out. Develop your own algorithm through pseudocode.

(Dr. Mihail) Programming August 28, 2018 2 / 12

slide-5
SLIDE 5

The Process

What does programming (in any language) involve?

Analysis

Understanding the problem. Figure out exactly the problem to be solved. Clearly define the input(s) and output(s). Solve by hand first. This step is useful to generate test data later.

Design

Formulate the overall structure of the program. The “how” of the program gets worked out. Develop your own algorithm through pseudocode.

Implementation

Translate the design (pseudocode) into a computer language

(Dr. Mihail) Programming August 28, 2018 2 / 12

slide-6
SLIDE 6

The Process

What does programming (in any language) involve?

Analysis

Understanding the problem. Figure out exactly the problem to be solved. Clearly define the input(s) and output(s). Solve by hand first. This step is useful to generate test data later.

Design

Formulate the overall structure of the program. The “how” of the program gets worked out. Develop your own algorithm through pseudocode.

Implementation

Translate the design (pseudocode) into a computer language

Testing

Try various inputs and verify correctness of inputs. If problems found (process called debugging), fix them.

(Dr. Mihail) Programming August 28, 2018 2 / 12

slide-7
SLIDE 7

Sample Problem

Problem Setting

It is assumed that achievement test scores should be correlated with student’s classroom performance. One would expect that students who consistently perform well in the classroom (tests, quizes, etc.) would also perform well on a standardized achievement test (0 - 100 with 100 indicating high achievement). A teacher decides to examine this

  • hypothesis. At the end of the academic year, she computes a correlation

between the students achievement test scores (she purposefully did not look at this data until after she submitted students grades) and the overall g.p.a. for each student computed over the entire year. The data for her class are provided next.

(Dr. Mihail) Programming August 28, 2018 3 / 12

slide-8
SLIDE 8

Problem Data

Achievement GPA 98 3.6 96 2.7 94 3.1 88 4.0 91 3.2 77 3.0 86 3.8 71 2.6 59 3.0 63 2.2 84 1.7 79 3.1 75 2.6 72 2.9 86 2.4 85 3.4 71 2.8 93 3.7 90 3.2 62 1.6 (Dr. Mihail) Programming August 28, 2018 4 / 12

slide-9
SLIDE 9

Problem questions

Questions

What is the correlation coefficient? What does this statistic mean concerning the relationship between achievement test prformance and g.p.a.? What would be the slope and y-intercept for a regression line based

  • n this data?

If a student scored a 93 on the achievement test, what would be their predicted G.P.A.? If they scored a 74? A 88?

(Dr. Mihail) Programming August 28, 2018 5 / 12

slide-10
SLIDE 10

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-11
SLIDE 11

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-12
SLIDE 12

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b Sample correlation coefficient: r =

N

i=1(xi −¯

x)(yi −¯ y)

√N

i=1(xi −¯

x)2√N

i=1(yi −¯

y)2 . This

correlation coefficient is an indicator of how well the data fits a model, in this case we assume our model is a linear function.

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-13
SLIDE 13

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b Sample correlation coefficient: r =

N

i=1(xi −¯

x)(yi −¯ y)

√N

i=1(xi −¯

x)2√N

i=1(yi −¯

y)2 . This

correlation coefficient is an indicator of how well the data fits a model, in this case we assume our model is a linear function.

What MATLAB features will I make use of to solve the problem?

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-14
SLIDE 14

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b Sample correlation coefficient: r =

N

i=1(xi −¯

x)(yi −¯ y)

√N

i=1(xi −¯

x)2√N

i=1(yi −¯

y)2 . This

correlation coefficient is an indicator of how well the data fits a model, in this case we assume our model is a linear function.

What MATLAB features will I make use of to solve the problem?

Mechanism to load and store data as floating point type vectors and scalars.

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-15
SLIDE 15

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b Sample correlation coefficient: r =

N

i=1(xi −¯

x)(yi −¯ y)

√N

i=1(xi −¯

x)2√N

i=1(yi −¯

y)2 . This

correlation coefficient is an indicator of how well the data fits a model, in this case we assume our model is a linear function.

What MATLAB features will I make use of to solve the problem?

Mechanism to load and store data as floating point type vectors and scalars. Simple arithmetic operations on vectors and scalars (exponentiation and summation)

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-16
SLIDE 16

Analysis

Understanding the problem

Questions worth asking:

What mathematical tool(s) do I need to solve the problem?

Linear functions: y = f (x) = ax + b Sample correlation coefficient: r =

N

i=1(xi −¯

x)(yi −¯ y)

√N

i=1(xi −¯

x)2√N

i=1(yi −¯

y)2 . This

correlation coefficient is an indicator of how well the data fits a model, in this case we assume our model is a linear function.

What MATLAB features will I make use of to solve the problem?

Mechanism to load and store data as floating point type vectors and scalars. Simple arithmetic operations on vectors and scalars (exponentiation and summation) Visualization (plotting)

(Dr. Mihail) Programming August 28, 2018 6 / 12

slide-17
SLIDE 17

Analysis

Designing the algorithm

We seek a step by step algorithm (or recipe) to solve the problem.

(Dr. Mihail) Programming August 28, 2018 7 / 12

slide-18
SLIDE 18

Analysis

Designing the algorithm

We seek a step by step algorithm (or recipe) to solve the problem.

Step 1

Where do we start?

(Dr. Mihail) Programming August 28, 2018 7 / 12

slide-19
SLIDE 19

Analysis

Designing the algorithm

We seek a step by step algorithm (or recipe) to solve the problem.

Step 1

Where do we start?

(Dr. Mihail) Programming August 28, 2018 7 / 12

slide-20
SLIDE 20

Algorithm Design

Design

1 Load data (excel spreadsheet) (Dr. Mihail) Programming August 28, 2018 8 / 12

slide-21
SLIDE 21

Algorithm Design

Design

1 Load data (excel spreadsheet)

Math

We now need to compute r: r = N

i=1(xi − ¯

x)(yi − ¯ y) N

i=1(xi − ¯

x)2 N

i=1(yi − ¯

y)2 (1)

(Dr. Mihail) Programming August 28, 2018 8 / 12

slide-22
SLIDE 22

Algorithm Design

Design

1 Load data (excel spreadsheet) 2 Compute r (Dr. Mihail) Programming August 28, 2018 9 / 12

slide-23
SLIDE 23

Algorithm Design

Design

1 Load data (excel spreadsheet) 2 Compute r

Math

We now need to estimate a and b from y = ax + b given the data we

  • have. The data we have comes in pairs of (x, y) values, ASSUMED, or

HYPOTHESIZED to come from a linear generating function where a is the slope and b is the y-intercept.

(Dr. Mihail) Programming August 28, 2018 10 / 12

slide-24
SLIDE 24

Algorithm Design

Design

1 Load data (excel spreadsheet) 2 Compute r

Math

We now need to estimate a and b from y = ax + b given the data we

  • have. The data we have comes in pairs of (x, y) values, ASSUMED, or

HYPOTHESIZED to come from a linear generating function where a is the slope and b is the y-intercept.

Formulas

a = N

i=1(xi − ¯

x)(yi − ¯ y) N

i=1(xi − ¯

x)2 (2) b = ¯ y − a ∗ ¯ x (3)

(Dr. Mihail) Programming August 28, 2018 10 / 12

slide-25
SLIDE 25

Algorithm Design

Design

1 Load data (excel spreadsheet) 2 Compute r 3 Calculate a, the slope 4 Calculate b, the y-intercept 5 Plug in new values of x and get the prediction:

f (74) will predict GPA based on achievement score of 74 f (88) will predict GPA based on achievement score of 88

(Dr. Mihail) Programming August 28, 2018 11 / 12

slide-26
SLIDE 26

Let’s write code

Big Idea

We will write a script, which is self-contained, ordered set of MATLAB statements, in order to solve this problem.

The first few steps

Open MATLAB Create new script Write code! (solution is available to download)

(Dr. Mihail) Programming August 28, 2018 12 / 12