500 likes | 588 Views
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:
E N D
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: • Essential of MATLAB Programming S.J. Chapman; (Editorial Thomson • Class Notes (in Yahoo Groups website) • Agenda (instructor/student made)
Presentations’ Index • Course Overview-(51 slides) • Introduction to MATLAB (87) • Loops-(37) • Files-(38) • Array and Matrix Operations-(68) • Functions-(52) • Symbolic Math-(35)
Course Overview • Basics: variables and constants • Arithmetic operators • Input/Output statements • Control Structures • Sequential • Selection, if • Repetition (also called Loops), for • Arrays • Functions
THE BASICS Computer 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 computations OUTPUT INPUT
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); Is there something wrong? How do you fix it? Is there more than one correct sequence?
The Structure of a MATLAB Program EXAMPLE clc, clear x = 2.0; y = 3.0; z = x^y; fprintf(‘%f ^ %f = %f ’, x, y, z); INPUT computations OUTPUT clc [clear the screen from information of previous running] clear [erase buffer memory]
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
Example log(x) computes the natural logarithm (uses an old notation) clc, clear x = 3500; y = log(x); fprintf(‘ln(%f) = %f ‘, x, y); print commands ln natural logarithm
Control_string (caracteres de control) Note the one-to-one correspondence between print commands and variables ‘ln( ) = ‘ %f %f print commands) x y variables
INPUT The input ( ) function • Allows entering data using the keyboard during program execution. • Syntax: [ variable list] =input(‘control string’);
Compare two different INPUT forms 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); Pro and cons of using each one
FLOW CHART INPUT Sequential Structure CALCULATIONS ... OUTPUT
Quiz Write 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
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 if statement
FLOW CHART INPUT Selection Structure false if true CALCULATIONS ... ... OUTPUT
The Repetition Structure, Loops Write a program to produce a table of x vs y:
The Repetition Structure (Loops) 3 parameters CV fprintf(‘ x y \n’); for x=0:1:9 if x>=0 y = sqrt(x); fprintf(‘%f %f \n’, x, y); else fprintf(‘invalid input \n’); end end erase unnecessary statements for loop x=0 is the initial value; x = x+1 is the increment; loop works as long x<=9
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?
Quiz Could you tell the order of execution of the for loop statements? for x=0:1:9 if x>=0 y = sqrt(x); fprintf(‘sqrt(%f)= %f \n’, x, y); else fprintf(‘invalid input \n’); end end Statements SOLUTION: Course Overview-Professor.ppt
Exercise Draw a flowchart for a generic program with a loop
print table title Repetition Structure false true x=0 x<=9 x=x+1 false if true for loop Sqrt(x) print invalid input print results
previous flow chart after deleting unnecessary statements: print table title Repetition Structure false true x=0 x<=9 x=x+1 Sqrt(x) for loop print results
Arrays x=[5, 3 , 9, 7 ]; index Computer Memory: Values: 5, 3, 9, 7 Indeces: 1, 2, 3, 4
Arrays Problem Average: Write a program to find the average of 5, 3, 9 and 7 using array variables
Arrays, example Array Initialization clc, clear x=[5 ,3 ,9 ,7 ]; suma=0; for i=1:1:4 suma = suma +x( i ); end ave=suma/4; fprintf(‘average= %f \n’, ave); Accumulator
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 tracking the values of suma as the loop progresses
Arrays, example This instruction allows construction of a more general program x=[5,3,9,7]; suma=0; N=4; for i=1:1:N suma = suma +x(i); end ave=suma/N; fprintf(‘average= %f \n’, ave);
Arrays, example This instruction allows construction of a more general program x=[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);
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?
Functions Library 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 functions
Functions OUTPUT INPUT 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 function body
INPUT Functions OUTPUT 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 function body
Functions /* an upgrade of the previous function using arrays */ function mini=minimum2(size, x) % Comment mini=x(1); for i = 2 : 1 : size if x(i)<mini mini=x(i); end end end x is an array variable with a number of elements equal to “size”
Quiz Modify the previous function to construct a function which finds the maximum of a set of numbers stored in array x
Using the minimum function(*) Function call 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)); (*)Either in the Command Window or in a full program in the editor (m-file)
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)); Function call (*)Either in the Command Window or in a full program (m-file)
MATLAB compiler • Do you have it? • Is it working properly? By now, You should have it!
Additional References: http://www.mathworks.com/ access/helpdesk/help/techdoc/
Software Lifecycle (optional) Software progresses in a cycle through the following stages: • Requirements • Design • Coding • Debugging and testing • Use and maintenance
Software Lifecycle • Traditionally, approximately 80% of the total lifecycle cost of a piece of software has been in the use and maintenance phase • The biggest payoff is internal documentation that describes what the program is doing
Utilities to develop a program Development of a computer program occurs in a cycle involving various utilities • Creation/modification (editing) of a program using a text editor • Preprocessing of the program to eliminate unnecessary spaces • Translating 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
Utilities to develop a program Keystrokes Editor Source PreProcessor Preprocessed Program Libraries Compiler Object Executable Linker Computer O/S Loader Commercial compiler package Inputs Outputs
Utilities to develop a program • The preprocessor cleans the source program (program_name.m) from unnecessary blank spaces and includes library functions declarations • The compiler translates MATLAB language to machine language line-by-line (program_name.obj) • The linker links (or builds) the program in machine language line-by-line with the library functions to produce an executable program (program_name.exe)