Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### If - else
- ```
- if x > 0 then
- print ('positive')
- else
- print('negative')
- end if;
- ```
- ### Do loops
- ```
- for i from 1 to 100 do
- i^2;
- end do;
- ```
- # Procedures
- ##### Procedures are maples wat of allowing us to break our programs into small features. Test this features independenly of each other
- e.g
- ```
- square := proc(x)
- x^2;
- end proc;
- ```
- ```
- sumOfSquares := proc(x, y)
- x^2 + y^2;
- end proc;
- ```
- The procedures take a positive integer, what to return the sequence of primes less than or equal to n
- ```
- primesAtMost := proc(n::integer)
- local s,i; # sequence and i - counter
- s := NULL;
- for i from 1 to n do
- if isprime(i) then
- s := s, i;
- end if;
- end do;
- return s;
- end proc;
- ```
- `primesAtMost(10) outputs 2,3,5,7`
- `primesAtMost(24) outputs 2,3,5,7,11,13,17,19`
- # Tracing and debugging
- e.g
- `trace(primeAtMost)`
- and then]
- `primeAtMost(10)`
- maple will return the value of variable at each step
- ```
- S := ()
- S := 2
- S := 2, 3
- etc.
- ```
- You can get rid of tracing run `untrace(primeAtMost)`
- `irem(n, i)` print the remainder when divding n by i
- ```
- isThisAPrime := proc(n)
- local i; # declare a local variable
- if n <= 1 then
- return print('no');
- end if;
- for i from 2..n-1 do
- if irem(n, i) = 0 then
- return print('no');
- end if;
- end do;
- return print("yes");
- end proc;
- ```
- ```
- myFactorial := proc(n)
- if n = 0 then
- return 1;
- else
- return n * myFactorial(n - 1);
- end if;
- end proc;
- ```
- Remark: `seq` is a usefull commnad for constructing
- e.g
- `seq(myFactorial(i), i = 0..10)` -> outputs 1(0!),1(1!), 2(2!), 6(3!)...
- # 3. Numerical approximations and errors
- #### Some number(e.g infinite numbers) cannot be stored in computer exactly and must be approximated by floating point. These apprxoimation can lead to erros in calculations.
- Good: Identify the sources of numerical erros and determine how large they are, so that we know how accurate our calculation are.
- # 3.1 Symbolic and numerical representations of number
- `sqrt(2) * sqrt(2)` -> outputs 2
- but on the other hand
- `evalf(sqrt(2)) * evalf(sqrt(2))`
- and `sqrt(2.0) * sqrt(2.0)`
- both output 1.999999 (10 decimal places by default)
- e.g
- 1. 9/2 = 4.5 has exactly floating-point representation 45 * 10^-1
- 2. -51/2500 = -0.0204 -> -204 * 10^-4
- 3. PI = 3.14159.... -> 3141 * 10^-3 (4 sign digits)
- 4. e = 2.71828... -> 27182 * 10^-4 (5 sign digits)
- 5. 101000 -> 101 * 10^3
- # 3.2 Absolute and relative errors
- #### If x is a real number and x' is an approximation of x, then the approximation error associated with approximating x by x' is `dx = | x - x' |`
- e.g `x = sqrt(2) and x' = 1.414 then dx = 0.00213562....`
- e.g y = 1/3 y' = 0.34 then `dy = | 34/100 - 1/3 | = 1/150` dy = 6667 * 10^-6
- so since
- x = x' ± dx
- #### the relative error `r = dx/|x| = |x - x'| / |x|`
- # 3.3 Numerical errors in floatin point arithmetic
- #### As we mentioned, approximation in representations of number can lead to erros in computating performed with these numbers
- e.g x + (y + z) = (x+y) + z
- however this is not neccsarry true when we approximate x,y,z by floating points
- ```
- e.g x = 100 = 1 * 10^2, y = 0.004 = 4 * 10^-3 and z = 0.006 = 6 * 10^-3. Lets work with only 5 significant digits
- x + y = 100 + 0.004 ≈ 100.00 = 10000 * 10^-2
- therefore,
- (x+y)+z ≈ 100.00 + 0.006 = 100.006 ≈ 100.00
- on other hand
- y + z = 0.004 + 0.006 = 0.01 = 1 * 10^-2
- x+(y+z) = 100 + 0.01 = 100.01
- ```
- i.e __(x+y)+z ~~=~~ x+(y+z)__
- #### We can avoid error to subtract numbers that are similar size
- # 3.4 Truncation errors
- #### when infinite number truncate we go truncate error
Add Comment
Please, Sign In to add comment