course overview

50
Course Overview INGE3016 Algorithms and Computer Programming with MATLAB Dr. Marco A. Arocha June, 2013 1

Upload: odele

Post on 26-Jan-2016

33 views

Category:

Documents


3 download

DESCRIPTION

Course Overview. INGE3016 Algorithms and Computer Programming with MATLAB Dr. Marco A. Arocha June, 2013. First Assignment. To be successful in the course student should bring Computer ( Laptop preferred ) Installed MATLAB application (preferable version 7.0 or latter) Textbook: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Course Overview

Course Overview

INGE3016

Algorithms and Computer Programming with MATLAB

Dr. Marco A. Arocha

June, 2013

1

Page 2: Course Overview

First Assignment

To be successful in the course student should bring Computer ( Laptop preferred ) Installed MATLAB application (preferable version 7.0

or latter) Textbook:

Essential of MATLAB Programming

S.J. Chapman; (Editorial Thomson Class Notes (in Yahoo Groups website) Agenda (instructor/student made)

2

Page 3: Course Overview

Presentations’ Index Course Overview-(51 slides)

Introduction to MATLAB (87)

Loops-(37)

Files-(38)

Array and Matrix Operations-(68)

Functions-(52)

Symbolic Math-(35)

3

Page 4: Course Overview

4

THE MOTIVATIONAL INTRODUCTION The rules to write these notes are that they would display inspiration, excitement, wonder and curiosity.

As a general objective, the class notes and the class presentation should be motivational and inspire students to pursue a degree in engineering, technology and/or science. In computing, turning the obvious into the useful is living definition of the word “frustration.” These class notes are intended to introduce programming to students with no prior programming experience. One of the lessons from the research on computing education is that one doesn’t just “learn to program.” One learn to program something [1,2]. How motivating is that something can make the difference between learning to program or not [3]. Some people are interested in learning programming just for programming’s sake ―but that’s not most people. Unfortunately, most introductory programming books are written and many professors give their lectures as if students have burning desire to learn to program. They emphasize programming concepts and give little thought to making the problems that are being solved interesting and relevant. They introduce new concepts without showing why the students should want to know about them. In this course students will learn about programming by writing programs to manipulate mathematical concepts withdrawn from the numerical methods field. Students will learn the mechanic of the problems in advance, such as the mathematical manipulation and number crunching make sense for them. Once the computer program is accomplished, the student will play with the solution by changing the parameters and variables so the problem and its solution reach another level of understanding for them. This course is about teaching people to program in order to acquire a skill of enormous importance in engineering. Engineering students who know how to program and know how to manipulate mathematical concepts aided by their computing and programming background have gigantic advantage in the solution of engineering problems while at college, at graduate school, and afterwards in the engineering field as compared to their programming-unskilled colleagues. Learn and Have Fun!! [1] Beth Andelson and Elliot Soloway. The role of domain experience in software design, IEEE Tfransactions on Software Engineering SE-11 (1985), no. 11, 1351-1360. [2] John T. Bruer, School for thought: A science of learning in the classroom, MIT Press, Cambridge, MA, 1993. [3] Amy Bruckman. Situated Support for Learning: Storm’s weekend with Rachael, Journal of the Learning Sciences 9(2000), no. 3, 329-372.

Page 5: Course Overview

Course Philosophy

5

Page 6: Course Overview

Course OverviewBasics: variables and constantsArithmetic operators Input/Output statementsControl Structures

Sequential Selection, if Repetition (also called Loops), for

ArraysFunctions

6

Page 7: Course Overview

THE BASICSComputer programs involve:

INPUT is data the program needs to work.

OUTPUT is the result(s) the program produces

as a result of CALCULATIONS processing the

data

7

INPUT computations OUTPUT

Page 8: Course Overview

Sequence Logic steps a program follows in order to be executed

successfully.

More than one correct sequence is possible.

A wrong sequence produces a logic error.

z = x^y;x = 2.0;y = 3.0;fprintf(‘%f ^ %f = %f ’, x, y, z);

8

Is there somethingwrong? How do you fix it?Is there more than one correct sequence?

Page 9: Course Overview

The Structure of a MATLAB Program

clc, clear

x = 2.0;y = 3.0;

z = x^y;

fprintf(‘%f ^ %f = %f ’, x, y, z);

9

INPUT

computations

OUTPUT

EXAMPLE

clc [clear the screen from information of previous running] clear [erase buffer memory]

Page 10: Course Overview

OUTPUT

The fprintf ( ) function

Allow us to print numbers and explanatory text on the screen.

Syntax:fprintf (‘control string’, v1, v2, …);

‘control string’ contains a description of text and commands (e.g., %f, %d) to print values and controls how you want each value to appear in the output.

v1, v2, etc. are the list of variables containing the values to be printed

10

Page 11: Course Overview

Example

clc, clearx = 3500;y = log(x);

fprintf(‘ln(%f) = %f ‘, x, y);

11

print commands

ln natural logarithm

log(x) computes the natural logarithm (uses an old notation)

Page 12: Course Overview

‘ln( ) = ‘

%f %f print commands)

x y variables

12

Control_string (caracteres de control)

Note the one-to-one correspondence between print commands and variables

Page 13: Course Overview

Print Commandsfprintf

Integers (int) %d

Real (double) %f, %e

Character (char) %c

13

Page 14: Course Overview

INPUT

The input ( ) function

Allows entering data using the keyboard during program execution.

Syntax:

[ variable list] =input(‘control string’);

14

Page 15: Course Overview

Program-1

x = 3500;

y = log(x);

fprintf(‘ln(%f) = %f’, x, y);

Program-2

x= input(‘Enter the value of x’);

y = log(x);

fprintf(‘ln(%f) = %f’, x, y);

15

Compare two different INPUT forms

Pro and cons of using each one

Page 16: Course Overview

Flow Chart SymbolsStarts/Stops Decision/Repetition

Input/Output Connector

Processing Off-page

Predefined Process

Annotation

Page 17: Course Overview

17

...

INPUT

OUTPUT

CALCULATIONS

Sequential Structure

FLOW CHART

Page 18: Course Overview

QuizWrite a piece of program to

calculate your final numerical grade.

Show the output on the screen

Use two approaches: i) MATLAB as a calculator (Command Window)ii) Use the MATLAB editor

18

Weigth (%) Score (%) Contribution20 98 2020 95 1920 93 1930 89 2710 85 9

ng 92

Page 19: Course Overview

The Selection Structure: The if statement

clc, clear

x = input(‘Enter a value for x ’);

if x>=0 y = sqrt(x); fprintf(‘sqrt(%f)= %f \n’, x, y);else fprintf(‘invalid input \n’);end

19

if statement

Page 20: Course Overview

20

INPUT

OUTPUT

CALCULATIONS

SelectionStructure

iftruefalse

... ...

FLOW CHART

Page 21: Course Overview

The Repetition Structure, Loops

Write a program to produce a table of x vs y:

x y=sqrt(x)

0.0 0.0

1.0 1.0

2.0 1.41

… …

9.0 3.0

21

Page 22: Course Overview

The Repetition Structure (Loops)

fprintf(‘ x y \n’);

for x=0:1:9if x>=0

y = sqrt(x); fprintf(‘%f %f \n’, x, y);

else fprintf(‘invalid input \n’);

endend

22

for loop

3 parameters

x=0 is the initial value; x = x+1 is the increment; loop works as long x<=9

CV

erase unnecessarystatements

Page 23: Course Overview

Quiz

How many parameters does the for loop have?

What is the meaning of each parameter?

How many iterations (or cycles) does the for loop perform?

What is the last value of the CV?

23

Page 24: Course Overview

QuizCould you tell the order of execution of the for

loop statements?

for x=0:1:9if x>=0

y = sqrt(x); fprintf(‘sqrt(%f)= %f \n’, x, y);

else fprintf(‘invalid input \n’);

endend

24

Statements

SOLUTION: Course Overview-Professor.ppt

Page 25: Course Overview

x=0 x<=9 { loop statements} x=x+1

x=1 x<=9 { loop statements} x=x+1

x=2 x<=9 { loop statements} x=x+1

x=3 x<=9 { loop statements} x=x+1

x=4 x<=9 { loop statements} x=x+1

x=5 x<=9 { loop statements} x=x+1

x=6 x<=9 { loop statements} x=x+1

x=7 x<=9 { loop statements} x=x+1

x=8 x<=9 { loop statements} x=x+1

x=9 x<=9 { loop statements} Exit the loop

25

itera

tions

Page 26: Course Overview

Exercise

Draw a flowchart for a generic program with a loop

26

Page 27: Course Overview

27

truefalsex=0 x<=9

x=x+1

ifSqrt(x)

truefalsefor loop

print table title

print resultsprint invalid input

Repetition Structure

Page 28: Course Overview

28

truefalsex=0 x<=9

x=x+1

Sqrt(x)for loop

print table title

print results

Repetition Structure

previous flow chart after deleting unnecessary statements:

Page 29: Course Overview

Quiz:

Write and run a program to salute the world 100 times

29

Page 30: Course Overview

Arrays

x=[5, 3 , 9, 7 ];

x(1) x(2) x(3) x(4)

5 3 9 7

30

Computer Memory:

index

Values: 5, 3, 9, 7Indeces: 1, 2, 3, 4

Page 31: Course Overview

Arrays

Problem Average:

Write a program to find the average of 5, 3, 9 and 7 using array variables

31

Page 32: Course Overview

Arrays, example

clc, clear

x=[5 ,3 ,9 ,7 ];

suma=0;

for i=1:1:4suma = suma +x( i );

end

ave=suma/4;

fprintf(‘average= %f \n’, ave);

32

Array Initialization

Accumulator

Page 33: Course Overview

How does the accumulator work? suma=suma+x(i)

Track the value of suma for each iteration (iter)

Initially suma=0

x(1)=5

x(2)=3

x(3)=9

x(4)=7

iter

i=1 suma=suma+x(i)5 = 0 + 5

i=2 suma=suma+x(i)8 = 5 + 3

i=3 suma=suma+x(i)17 = 8 + 9

i=4 suma=suma+x(i)24 = 17 + 7

33

tracking the values of suma as the loop progresses

Page 34: Course Overview

Arrays, example x=[5,3,9,7];

suma=0;N=4;

for i=1:1:Nsuma = suma +x(i);

endave=suma/N;fprintf(‘average= %f \n’, ave);

34

This instructionallows constructionof a moregeneralprogram

Page 35: Course Overview

Arrays, examplex=[5, 3, 9, 7];

suma=0;

N=length(x);

for i=1:1:N

suma = suma +x(i);

end

ave=suma/N;

fprintf(‘average= %f \n’, ave);

35

This instructionallows constructionof a moregeneralprogram

Page 36: Course Overview

Quiz

What is the value of the x(i) element?What is the value of the x(4) element?Could you track the value of x(i) for each i value?

Could you track the value of suma variable for each i value?

36

Page 37: Course Overview

FunctionsLibrary functions

abs(x)

sqrt(x)

exp(x)

log(x), log10(x)

sin(x), cos(x)

User-defined functions

minimun(x, y, z)

minimun2(x), where x is an array

37

functions

Page 38: Course Overview

Functions

function mini=minimum(x, y, z)

% This function finds the minimun of 3 numbers

mini=x;

if (y<mini)

mini=y;

end

if(z<mini)

mini=z;

end

end

38

INPUTOUTPUT

function body

Page 39: Course Overview

Functions

function mini=minimum(x, y, z)

% This function finds the minimun of 3 numbers

mini=x;

if (y<mini)

mini=y;

end

if(z<mini)

mini=z;

end

end

39

INPUT

OUTPUT

function body

Page 40: Course Overview

Functions

function mini=minimum2(size, x)

% Comment

mini=x(1);

for i = 2 : 1 : size

if x(i)<mini

mini=x(i);

end

end

end

40

/* an upgrade of the previous function using arrays */

x is an array variablewith a number of elements equal to “size”

Page 41: Course Overview

Quiz

Modify the previous function to construct a function which finds the maximum of a set of numbers stored in array x

41

Page 42: Course Overview

Using the minimum function(*)

clc, clear

x =[4]; y=[1]; z=[0];

minimo=minimum(x, y, z);

fprintf(‘The minimum of the set is %d’, minimo);

Another call alternative:fprintf(‘The minimum of the set is %d’, minimum(x, y, z)); 42

Function call

(*)Either in the Command Window or in a full program in the editor (m-file)

Page 43: Course Overview

Using the minimum2 function(*)

x =[4,1,0,3,5,8,9,7,2,6];

size =10;

minimo=minimum2(size, x);

fprintf(‘The minimum of the set is %d’, minimo);

Another alternative:fprintf(‘The minimum of the set is %d’, minimum2(size, x)); 43

Function call

(*)Either in the Command Window or in a full program (m-file)

Page 44: Course Overview

MATLAB compiler

Do you have it?

Is it working properly?

44

By now,

You should have it!

Page 45: Course Overview

Additional References:

http://www.mathworks.com/

access/helpdesk/help/techdoc/

45

Page 46: Course Overview

Software Lifecycle (optional)Software progresses in a cycle through the following stages:

RequirementsDesignCodingDebugging and testingUse and maintenance

46

Page 47: Course Overview

Software Lifecycle

o Traditionally, approximately 80% of the total lifecycle cost of a piece of software has been in the use and maintenance phase

o The biggest payoff is internal documentation that describes what the program is doing

47

Page 48: Course Overview

Utilities to develop a program

Development of a computer program occurs in a cycle involving various utilitiesCreation/modification (editing) of a program using a text editorPreprocessing of the program to eliminate unnecessary spacesTranslating the code to machine language using the compiler

(Compiling)(program_name.obj)Connecting the object program to the library functions with a

linker (Linking) creating an executable file (program_name.exe)Loading the program with the loader to be run by the specific

OS (operating system)Executing the program

48

Page 49: Course Overview

Utilities to develop a program

49Outputs

Loader

Executable Linker Object Compiler

PreprocessedProgram

Keystrokes Editor Source PreProcessor

Inputs

Libraries

Commercial compiler package

Computer O/S

Page 50: Course Overview

Utilities to develop a programoThe preprocessor cleans the source program (program_name.m) from unnecessary blank spaces and includes library functions declarations

oThe compiler translates MATLAB language to machine language line-by-line (program_name.obj)

oThe linker links (or builds) the program in machine language line-by-line with the library functions to produce an executable program (program_name.exe)

50