Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming The ideal style of programming is Structured or

Similar presentations


Presentation on theme: "Programming The ideal style of programming is Structured or"— Presentation transcript:

1 Programming The ideal style of programming is Structured or
Modular programming Break down a larger goal into tasks Develop a module for each task A module has a single entrance and exit Modules are reusable

2 A structured program for building a house:
Excavation Foundation Framing subroutine - floor 1 Framing subroutine - floor 2 Walls Bathroom subroutine etc. Excavation routine Foundation routine Framing routine

3 Framing routine measure wood cut wood assemble pieces put frame in place attach frame Each of these can have subroutines

4 Comments!!! Another aspect of good programming:
You must include comments in your programs you turn in - otherwise we will have great difficulty knowing what you are doing

5 For example, here some cryptic code without comment
for j=0 to 2 k=(2-j)*(1+3*j)/2 end What does it do? Put a comment in /* turns (0,1,2) into (1,2,0) */

6 Matlab is a numerical analysis system
Can write “programs”, but they aren’t compiled Should still use structured programming Should still use comments Comments are indicated by % at the beginning of the line

7 Matlab’s basic component is a vector
All operations are optimized for vector use Loops run slower in Matlab than in Fortran (not a vector operation) Really good help command

8 Matlab topics and how it differs from Fortran
Syntax Data types Input-output Mathematics Do loops If then structures

9 Syntax No complicated rules Perhaps the most important thing to remember is semicolons at the end of a line surpress output Type more on to keep text from leaving screen too fast

10 Data types All numbers are double precision Text is stored as arrays of characters. You don’t have to declare the type of data (defined when running)

11 Vectors Everything in Matlab is a vector (or matrix) Even single value variables size command gives size of a vector

12 Example Pi= a= » pi » a=[1 2; 3 4] ans = 3.1416 a = 1 2 » size(pi) 3 4
» a=[1 2; 3 4] a = » size(a) ans = Pi= a=

13 Input-output Values can be assigned by typing vector name = value Matlab automatically prints the results of any calculation (unless surpressed by ;) Use disp to print out text to screen

14 To read files in - if the file is an ascii table, use load - if the file is ascii but not a table, file i/o needs fopen and fclose Reading in data from file using fopen depends on type of data (binary or text).

15 Mathematics All the standard operators +,-,*,/,^ Built-in functions too sin(), cos(), exp(), etc. IMPORTANT These operators are vectorized.

16 Example: Vectorized operators
But a*b gives an error because dimensions are incorrect. Need to use .*

17 Likewise

18 for loops They do exist in Matlab, and sometimes you need them for i=1:n for j=1:n a(i,j)=1/(i+j-1); end

19 Because of vectorization , can often do loops more efficiently
for i=1:3 b=sin(i) end i=1:3 b=sin(i) is much faster

20 If - then also exists The general form of the IF statement is IF expression statements ELSEIF expression ELSE END

21 Possible comparisons are
= = equals < less than > greater than <= less than or equal to >= greater than or equal to ~= not equal to

22 Programming In Matlab, one can write a set of commands in a text file (called an m-file, since the ending must be .m) These commands can be a program Type the name of the m-file in the Matlab environment to run the program

23 Difference betweem scripts and functions
Scripts share variables with the main workspace Functions do not

24 One of the best things about Matlab is graphics
plot is the one you will be using most often many other 3 dimensional plotting functions


Download ppt "Programming The ideal style of programming is Structured or"

Similar presentations


Ads by Google