ap3114 lecture wk3 programming

Click here to load reader

Upload: guoxuanchan

Post on 12-Dec-2015

227 views

Category:

Documents


0 download

DESCRIPTION

Physics

TRANSCRIPT

AP3114/Computational Methods for Physicists and Materials Engineers

Dr. Jun FanJan. 26, 2015AP3114/Computational Methods for Physicists and Materials Engineers3. Basic Programming

1Review of Lecture 2Matrix

Functions to generate matrix (eye, ones, zeros, etc)Empty matrix []Query matrices sizeAccessing elements of matrix A(i,j)a:b:c$: the end of the matrix+,-*,/,\XB=A => X=A/B=AB-1 AX=B => X=A\B=A-1BElementwise operations: .Conjugate transpose: Sum, mean, median, st_deviation

real number/matrix => transpose Complex number/matrix => transpose and conjugatesElementary single quote . only transpose, no conjugating the matrix, be it real or complex.2ExampleSuppose we have invested some money that draws 10% interests per year, compounded. We want to know how long it will take for the investment to double. More specifically, we want a statement of the account each year until the balance has doubled.

3OutlineLooping and branching statementsifselect/caseforwhilebreakContinuereturnFunctionUser-defined functionsUser-defined libraryDebugging: pause, abort, resume

4Order of commandsSequence: one by one consecutively

Command 1Command 3Command 25Branching: ifSelection/Decision Among Multiple Cases

Condition?True BlockTrue

Commands Commands 6Branching: ifSelection/Decision Among Multiple Cases

Condition?True BlockTrueFalse BlockFalseCommands Commands 7

8Branching: select/caseMultiple possibilities

Test expressionCase 2Command Case 1Case 4Case 39The select/case statement

10FlowchartCommand 1Command 3Command 2Condition?True BlockTrueFalse BlockFalseTest expressionCase 2Command Case 1Case 4Case 3ifselectcase11The for statementThe for statement allows to perform loops

12The for statement

13The while statementwhile: allows to perform a loop while a boolean expression is true

Alternatively14forwhileBlock 1Block 2Condition?Block 1Block 2FalseCondition is TrueGiven a number of repetitions15The break statementThe break statement allows to interrupt a loop

16The continue statementThe continue statement allows to go on to the next loop, so that the statements in the body of the loop are not executed this time.For instance, we want to calculate the sum of odd number smaller than 10: 1+3+5+7+9

17ExampleSuppose we have invested some money that draws 10% interests per year, compounded. We want to know how long it will take for the investment to double. More specifically, we want a statement of the account each year until the balance has doubled.

18Example

19Functionsy=f(x); internal function y=sin(x)

20Functions (2)

21Functions (3)How about the following?>>clear

>>z=myfunction(9)

22Functions(4)Multiple output arguments

When there is no output argument, the value of the first output will be store in the ans variable (different from Matlab)

Scilab/MATLAB:function f=simplef(x1,x2)f(1)=2*x1;f(2)=3*x2;endfunctioin/end >simplef(1,2)>ans= 2 623Functions(5)A function can call another function

?24Functions(6)return statementreturn statement allow to immediately return, i.e., quits the current function.Suppose we want to calculate the sum of integers from istart to iend only if istart