Computational Photonics Seminar 02, 30 April 2012 Programming in MATLAB controlling of a program s flow of execution branching loops loop control several programming tasks 1
Programming task 1 Plot the sinc-function which is defined as sin(x)/x within the interval of [-3pi,+3pi] using 301 points. Try to avoid in an appropriate manner the singularity which occurs at x =0! Necessary functions: colon operator: : division element by element:./ v=0 sine: sin function for ploting a function: plot
Programming task 2 Write down and test a program to compute the sum of all the elements of a matrix. Header of the program: function sm=supersum(a) % Sum over all elememnts of a matrix % Command sm=supersum (A) % A: arbitrary 2D-matrix Commands you will need: sum, (size) Test with: A=magic(9); Compute the sum of all the elements of a magic square.
Controlling the flow of a program branching if branching conditional execution of a commands Evaluation of a logical expression and a subsequent evaluation of a set of commands if the logical expression is true, otherwise there might be an alternative with else, otherwise nothing Notation: if log. expression 1 set of commands 1 elseif log. expression 2 set of commands 2 else set of commands 3 Example: if a>b else max=a; max=b;
Controlling the flow of a program loops I for - loop Iterative repetion of a set of commands for a predefined number of times Notation: for control variable = starting value : terminating value set of commands Example: factorial=1; for i=2:n factorial=factorial*i;
Controlling the flow of a program loops II while - loop Iterative repetion of a set of commands as long as a certain logical expression remains true. Notation: while logical expression set of commands Example: factorial=1; i=2 while i<=n factorial=factorial*i; i=i+1;
Controlling the flow of a program loop control I continue - command Pass control to the next iteration of FOR or WHILE loop. It avoids all commands that do appear subsequently in the present loop. Notation: while logical expression 1 set of commands 1 if logical expression 2 continue set of commands 2
Controlling the flow of a program loop control II break - command Terminates the execution of WHILE or FOR loop. It does not execute any of the commands that would appear subsequently within the loop. Notation: while logical expression 1 set of commands 1 if logical expression 2 break set of commands 2
Programming task 3 Computation of the root of a function within a predefined interval by using the bisection method What is given: What we are looking for: continuous function f(x) boundaries of an interval xa and xb Value of x with f(x)=0 Application of the bisection method: Reducing the interval which includes a root successively by a factor of 2. a1 a2 b3 b2 b1
Programming task 3b Write an extension to the previous bisection program to ensure that the case of multiple roots is properly handled by the program. For this purpose separate the interval into a predefined number of intervals and search individually for the roots.