Guest User

Untitled

a guest
Jul 18th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. % Author: Caio Rodrigues
  2. % Objective: Test higher order functions and anonymous functions.
  3. %
  4. % Limitations: Doesn't support assignment in lambda function and also doens't support multiple lines.
  5. % Other limitations are that functions cannot be stored in data structures or returned from functions.
  6. %
  7. octave:30> computeTable = @(xmin, dx, xmax, fn) [ (xmin:dx:xmax)' (fn(xmin:dx:xmax))']
  8. computeTable =
  9.  
  10. octave:15> computeTable(0.0, 0.1, 2.0, @(x)[2 * x + 1.0])
  11. ans =
  12.  
  13. 0.00000 1.00000
  14. 0.10000 1.20000
  15. 0.20000 1.40000
  16. 0.30000 1.60000
  17. 0.40000 1.80000
  18. 0.50000 2.00000
  19. 0.60000 2.20000
  20. 0.70000 2.40000
  21. 0.80000 2.60000
  22. 0.90000 2.80000
  23. 1.00000 3.00000
  24. 1.10000 3.20000
  25. 1.20000 3.40000
  26. 1.30000 3.60000
  27. 1.40000 3.80000
  28. 1.50000 4.00000
  29. 1.60000 4.20000
  30. 1.70000 4.40000
  31. 1.80000 4.60000
  32. 1.90000 4.80000
  33. 2.00000 5.00000
  34.  
  35. octave:34> computeTable(0.0, 0.1, 2.0, @exp)
  36. ans =
  37.  
  38. 0.00000 1.00000
  39. 0.10000 1.10517
  40. 0.20000 1.22140
  41. 0.30000 1.34986
  42. 0.40000 1.49182
  43. 0.50000 1.64872
  44. 0.60000 1.82212
  45. 0.70000 2.01375
  46. 0.80000 2.22554
  47. 0.90000 2.45960
  48. 1.00000 2.71828
  49. 1.10000 3.00417
  50. 1.20000 3.32012
  51. 1.30000 3.66930
  52. 1.40000 4.05520
  53. 1.50000 4.48169
  54. 1.60000 4.95303
  55. 1.70000 5.47395
  56. 1.80000 6.04965
  57. 1.90000 6.68589
  58. 2.00000 7.38906
  59.  
  60.  
  61. octave:12> thunk = @() disp('Hello world')
  62. thunk =
  63.  
  64. @() disp ('Hello world')
  65.  
  66. octave:13> thunk()
  67. Hello world
  68. octave:14>
  69.  
  70.  
  71. computeTableTrig = @(min, step, max, fn)[ (min:step:max)' (fn(pi / 180.0 * (min:step:max)))']
  72.  
  73. % Table with angles in degrees and sines
  74. octave:21> computeTableTrig(0.0, 15.0, 180.0, @sin)
  75. ans =
  76.  
  77. 0.00000 0.00000
  78. 15.00000 0.25882
  79. 30.00000 0.50000
  80. 45.00000 0.70711
  81. 60.00000 0.86603
  82. 75.00000 0.96593
  83. 90.00000 1.00000
  84. 105.00000 0.96593
  85. 120.00000 0.86603
  86. 135.00000 0.70711
  87. 150.00000 0.50000
  88. 165.00000 0.25882
  89. 180.00000 0.00000
  90.  
  91. % Table with angles in degrees, radians and tan
  92. computeTableTrig2 = @(min, step, max, fn)[ (min:step:max)' (pi / 180 * (min:step:max))' (fn(pi / 180.0 * (min:step:max)))']
  93.  
  94. octave:29> computeTableTrig2(0.0, 15.0, 180.0, @sin)
  95. ans =
  96.  
  97. 0.00000 0.00000 0.00000
  98. 15.00000 0.26180 0.25882
  99. 30.00000 0.52360 0.50000
  100. 45.00000 0.78540 0.70711
  101. 60.00000 1.04720 0.86603
  102. 75.00000 1.30900 0.96593
  103. 90.00000 1.57080 1.00000
  104. 105.00000 1.83260 0.96593
  105. 120.00000 2.09440 0.86603
  106. 135.00000 2.35619 0.70711
  107. 150.00000 2.61799 0.50000
  108. 165.00000 2.87979 0.25882
  109. 180.00000 3.14159 0.00000
Add Comment
Please, Sign In to add comment