1 / 50

Course Overview

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:

odele
Download Presentation

Course Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

  3. Presentations’ Index • Course Overview-(51 slides) • Introduction to MATLAB (87) • Loops-(37) • Files-(38) • Array and Matrix Operations-(68) • Functions-(52) • Symbolic Math-(35)

  4. Course Philosophy

  5. Course Overview • Basics: variables and constants • Arithmetic operators • Input/Output statements • Control Structures • Sequential • Selection, if • Repetition (also called Loops), for • Arrays • Functions

  6. 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

  7. 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?

  8. 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]

  9. 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. 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

  11. Control_string (caracteres de control) Note the one-to-one correspondence between print commands and variables ‘ln( ) = ‘ %f %f  print commands) x y  variables

  12. Print Commands

  13. INPUT The input ( ) function • Allows entering data using the keyboard during program execution. • Syntax: [ variable list] =input(‘control string’);

  14. 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

  15. Flow Chart Symbols

  16. FLOW CHART INPUT Sequential Structure CALCULATIONS ... OUTPUT

  17. 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

  18. 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

  19. FLOW CHART INPUT Selection Structure false if true CALCULATIONS ... ... OUTPUT

  20. The Repetition Structure, Loops Write a program to produce a table of x vs y:

  21. 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

  22. 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. 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

  24. iterations

  25. Exercise Draw a flowchart for a generic program with a loop

  26. 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

  27. 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

  28. Quiz: Write and run a program to salute the world 100 times

  29. Arrays x=[5, 3 , 9, 7 ]; index Computer Memory: Values: 5, 3, 9, 7 Indeces: 1, 2, 3, 4

  30. Arrays Problem Average: Write a program to find the average of 5, 3, 9 and 7 using array variables

  31. 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

  32. 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

  33. 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);

  34. 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);

  35. 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. 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

  37. 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

  38. 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

  39. 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”

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

  41. 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)

  42. 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)

  43. MATLAB compiler • Do you have it? • Is it working properly? By now, You should have it!

  44. Additional References: http://www.mathworks.com/ access/helpdesk/help/techdoc/

  45. Software Lifecycle (optional) Software progresses in a cycle through the following stages: • Requirements • Design • Coding • Debugging and testing • Use and maintenance

  46. 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

  47. 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

  48. 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

  49. 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)

More Related