Basic Programming: A Mathematical Application Exercises
Basic Programming: A Mathematical Application Exercises
algorithms + data structures = programming (Wirth) programming in Maple = putting interactive commands between proc and end
Simulation of Gambling
John and Sarah are tossing a coin. Each time it is tail, John pays Sarah one guilder; if it is head, Sarah pays John one guilder. The gambling is over when one player is bankrupt. John starts the gambling with four guilders and Sarah begins with five guilders. Simulate the gambling and make an estimate of the mean length of the gambling.
Tossing the Coin Head: 1 Tail: -1 > ZeroOne := rand(0..1); > die := proc( ) 2*ZeroOne( ) - 1 end; > die( ); The start of the gambling : > John := 4; Sarah := 5; One Toss: > p := die( ); > John := John + p; > Sarah := Sarah - p;
>
Interation
We repeat the previous step until the gambling is over Start:
> while (John <> 0 and Sarah <> 0) do > p := die( ): > John := John + p; > Sarah := Sarah - p; > counter := counter + 1; > od; >
The game as a Procedure:
> game := > proc(John, Sarah) > local j, s, p, counter; > counter := 0: > j := John; > s := Sarah; > while (j <> 0 and s <> 0) do > p := die( ): > j := j + p; > s := s - p; > counter := counter + 1; > od; > end: >
Games
One Game: > game(4, 5); > A Series of Games: > games := > [ seq( game(4, 5), i=1..15 ) ]; > Analysis of the Series:
o
Mean Value > add( i, i=games ); > " / nops( games ); > evalf("); >
Procedures
Definition: The simplest form of a Maple procedure is constructed by bracketing commands with proc(parameter_sequence) statements end where the parameter_sequence is in most cases a sequence of zero or more Maple names.
Two examples: > sgn := proc(n) (-1)^n end; > hello := proc( ) > print(`hello everybody`) > end; > hello( ); > Local and Global Variables: > restart; > poly1 := proc(x,n) > sum( x^i, i=0..n ) > end; > poly1(y,5); > i := 3: > poly1(y,6); > poly2 := proc(x,n) > local i; > sum( x^i, i=0..n ) > end: > poly2(y,5); > i := 3; > poly2(y,6); > What variables are global inside procedures? Those who are declared global used in the procedure body, but are not assigned a value. All others are local, if necessary made local by Maple itself. An example: the implementation of an on/off switch. > on := proc( ) > global switch; > switch := true > end: > state := proc( ) > global switch; > switch > end: > on( ):
> state( ); > What if you forget the declaration in the procedure definition? See below. > off := proc( ) > switch := false > end; > on( ): off( ): > state( ); > Recursion: Consider the computation of Fibonacci numbers: F1=1, F2=1, Fn=Fn-1+Fn-2. - First implementation: > F := proc(n) > if n=1 or n=2 > then 1 > else F(n-1) + F(n-2) > fi > end: > F(10); > The complexity is O(2n). - Second implementation : > Fib := proc(n) > option remember; > if n=1 or n=2 > then 1 > else Fib(n-1) + Fib(n-2) > fi > end: > Fib(100); > The complexity implementation is O(n).
dynamic type checking > sgn := proc( n::integer ) > (-1)^n > end: > seq( sgn(i), i=-5..5 ); > sgn( Pi ); > ADD := proc( x::anything, L::list ) > [ x, op(L) ] > end: > ADD( 1, [2,3,4] ); > ADD( 1, {2,3,4} ); >
type > fact := proc(n) > if not type( n, integer ) or n<0 > then ERROR(`wrong type of argument`) > fac(n) > fi > end; > > fac := proc(n) > option remember; > if n=0 > then RETURN(1) > else RETURN( n*fac(n-1) ) > fi > end; > fact(4); > fact(1/2); >
else
Consider the following function > f := x -> x^2 - 2; Choose as starting value > x0 := 1.0: Other approximations can be recursively computed by the following procedure. > x := proc(n) > option remember; > global f, x0; > if n<=0 > then x0 > else x(n-1)- f(x(n-1))/D(f)(x(n-1)) > fi > end; > x(5); > seq( x(n), n=0..5 ); >
Summary
Format of procedure definition: proc(parameter_sequence) local variables; global variables;
options names; statements; end For recursive procedures the option remember is useful. Return from a procedure can be forced by the procedure RETURN. Error messages can be made by ERROR. Type checking can be done - dynamically, via the binding operator :: - inside the procedure body, via the procedure type.
Exercises
1) In R2 we define a new inner product by <x|y>= 3x1y1-x1y2-x2y1+x2y2 , write a Maple procedure that computes the inner product of two vectors. 2) Write procedures mean and variance that compute the mean value and the variance of a list of numbers. I.e.,
where n is the number of elements in the list L and is the average of the numbers in the list. Your routines should print an error if the list is empty. 3) Define a procedure that plots the complex roots of a given univariate polynomial in the complex plane. Make an animation of the root loci of the polynomials x5 +3x4 +2x3 -x2 -3x+c, where c varies from -2 to 8 with stepsize 1/2.
4) Write a Maple procedure that computes the Legendre polynomial Ln(x). These polynomials satisfy the recurrence relation L0(x)=1, L1(x)=x, and
for n>1. Compute L7(x) and check your answer with the Maple procedure orthopoly[P]. Can your procedure compute L50(x). 5) The secant method is a way of root finding which is different from the NewtonRaphson method. It uses the formula
6) With Euler's Method you can solve the initial value problem f (x,y), y(x0)=y0 numerically. Apply this method for f:= (x,y)->x y, y(0)=1. Plot your solution and compare it with the graph of the exact solution. [Answers] [Main Page]