Guest User

Untitled

a guest
Jan 20th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. ### If - else
  2.  
  3. ```
  4. if x > 0 then
  5. print ('positive')
  6. else
  7. print('negative')
  8. end if;
  9. ```
  10.  
  11. ### Do loops
  12.  
  13. ```
  14. for i from 1 to 100 do
  15. i^2;
  16. end do;
  17.  
  18. ```
  19. # Procedures
  20. ##### Procedures are maples wat of allowing us to break our programs into small features. Test this features independenly of each other
  21. e.g
  22.  
  23. ```
  24. square := proc(x)
  25. x^2;
  26. end proc;
  27.  
  28. ```
  29.  
  30. ```
  31. sumOfSquares := proc(x, y)
  32. x^2 + y^2;
  33. end proc;
  34.  
  35. ```
  36.  
  37. The procedures take a positive integer, what to return the sequence of primes less than or equal to n
  38.  
  39.  
  40. ```
  41. primesAtMost := proc(n::integer)
  42. local s,i; # sequence and i - counter
  43. s := NULL;
  44.  
  45. for i from 1 to n do
  46. if isprime(i) then
  47. s := s, i;
  48. end if;
  49. end do;
  50.  
  51. return s;
  52. end proc;
  53.  
  54. ```
  55.  
  56.  
  57. `primesAtMost(10) outputs 2,3,5,7`
  58. `primesAtMost(24) outputs 2,3,5,7,11,13,17,19`
  59.  
  60.  
  61. # Tracing and debugging
  62. e.g
  63.  
  64. `trace(primeAtMost)`
  65. and then]
  66.  
  67. `primeAtMost(10)`
  68.  
  69. maple will return the value of variable at each step
  70.  
  71. ```
  72. S := ()
  73. S := 2
  74. S := 2, 3
  75. etc.
  76.  
  77. ```
  78.  
  79. You can get rid of tracing run `untrace(primeAtMost)`
  80.  
  81.  
  82. `irem(n, i)` print the remainder when divding n by i
  83.  
  84. ```
  85.  
  86. isThisAPrime := proc(n)
  87. local i; # declare a local variable
  88.  
  89. if n <= 1 then
  90. return print('no');
  91. end if;
  92.  
  93. for i from 2..n-1 do
  94. if irem(n, i) = 0 then
  95. return print('no');
  96. end if;
  97. end do;
  98.  
  99. return print("yes");
  100. end proc;
  101.  
  102. ```
  103.  
  104. ```
  105. myFactorial := proc(n)
  106.  
  107. if n = 0 then
  108. return 1;
  109. else
  110. return n * myFactorial(n - 1);
  111. end if;
  112.  
  113. end proc;
  114.  
  115. ```
  116.  
  117. Remark: `seq` is a usefull commnad for constructing
  118. e.g
  119. `seq(myFactorial(i), i = 0..10)` -> outputs 1(0!),1(1!), 2(2!), 6(3!)...
  120.  
  121. # 3. Numerical approximations and errors
  122. #### 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.
  123.  
  124. Good: Identify the sources of numerical erros and determine how large they are, so that we know how accurate our calculation are.
  125.  
  126.  
  127. # 3.1 Symbolic and numerical representations of number
  128.  
  129. `sqrt(2) * sqrt(2)` -> outputs 2
  130.  
  131. but on the other hand
  132.  
  133. `evalf(sqrt(2)) * evalf(sqrt(2))`
  134. and `sqrt(2.0) * sqrt(2.0)`
  135. both output 1.999999 (10 decimal places by default)
  136.  
  137.  
  138.  
  139. e.g
  140. 1. 9/2 = 4.5 has exactly floating-point representation 45 * 10^-1
  141. 2. -51/2500 = -0.0204 -> -204 * 10^-4
  142. 3. PI = 3.14159.... -> 3141 * 10^-3 (4 sign digits)
  143. 4. e = 2.71828... -> 27182 * 10^-4 (5 sign digits)
  144. 5. 101000 -> 101 * 10^3
  145.  
  146. # 3.2 Absolute and relative errors
  147. #### 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' |`
  148.  
  149. e.g `x = sqrt(2) and x' = 1.414 then dx = 0.00213562....`
  150.  
  151.  
  152. e.g y = 1/3 y' = 0.34 then `dy = | 34/100 - 1/3 | = 1/150` dy = 6667 * 10^-6
  153.  
  154. so since
  155. x = x' ± dx
  156.  
  157.  
  158. #### the relative error `r = dx/|x| = |x - x'| / |x|`
  159.  
  160.  
  161.  
  162. # 3.3 Numerical errors in floatin point arithmetic
  163. #### As we mentioned, approximation in representations of number can lead to erros in computating performed with these numbers
  164.  
  165. e.g x + (y + z) = (x+y) + z
  166.  
  167.  
  168. however this is not neccsarry true when we approximate x,y,z by floating points
  169.  
  170. ```
  171. 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
  172.  
  173. x + y = 100 + 0.004 ≈ 100.00 = 10000 * 10^-2
  174. therefore,
  175. (x+y)+z ≈ 100.00 + 0.006 = 100.006 ≈ 100.00
  176.  
  177. on other hand
  178. y + z = 0.004 + 0.006 = 0.01 = 1 * 10^-2
  179.  
  180. x+(y+z) = 100 + 0.01 = 100.01
  181. ```
  182. i.e __(x+y)+z ~~=~~ x+(y+z)__
  183.  
  184. #### We can avoid error to subtract numbers that are similar size
  185.  
  186. # 3.4 Truncation errors
  187. #### when infinite number truncate we go truncate error
Add Comment
Please, Sign In to add comment