Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Author: Caio Rodrigues
- % Objective: Test higher order functions and anonymous functions.
- %
- % Limitations: Doesn't support assignment in lambda function and also doens't support multiple lines.
- % Other limitations are that functions cannot be stored in data structures or returned from functions.
- %
- octave:30> computeTable = @(xmin, dx, xmax, fn) [ (xmin:dx:xmax)' (fn(xmin:dx:xmax))']
- computeTable =
- octave:15> computeTable(0.0, 0.1, 2.0, @(x)[2 * x + 1.0])
- ans =
- 0.00000 1.00000
- 0.10000 1.20000
- 0.20000 1.40000
- 0.30000 1.60000
- 0.40000 1.80000
- 0.50000 2.00000
- 0.60000 2.20000
- 0.70000 2.40000
- 0.80000 2.60000
- 0.90000 2.80000
- 1.00000 3.00000
- 1.10000 3.20000
- 1.20000 3.40000
- 1.30000 3.60000
- 1.40000 3.80000
- 1.50000 4.00000
- 1.60000 4.20000
- 1.70000 4.40000
- 1.80000 4.60000
- 1.90000 4.80000
- 2.00000 5.00000
- octave:34> computeTable(0.0, 0.1, 2.0, @exp)
- ans =
- 0.00000 1.00000
- 0.10000 1.10517
- 0.20000 1.22140
- 0.30000 1.34986
- 0.40000 1.49182
- 0.50000 1.64872
- 0.60000 1.82212
- 0.70000 2.01375
- 0.80000 2.22554
- 0.90000 2.45960
- 1.00000 2.71828
- 1.10000 3.00417
- 1.20000 3.32012
- 1.30000 3.66930
- 1.40000 4.05520
- 1.50000 4.48169
- 1.60000 4.95303
- 1.70000 5.47395
- 1.80000 6.04965
- 1.90000 6.68589
- 2.00000 7.38906
- octave:12> thunk = @() disp('Hello world')
- thunk =
- @() disp ('Hello world')
- octave:13> thunk()
- Hello world
- octave:14>
- computeTableTrig = @(min, step, max, fn)[ (min:step:max)' (fn(pi / 180.0 * (min:step:max)))']
- % Table with angles in degrees and sines
- octave:21> computeTableTrig(0.0, 15.0, 180.0, @sin)
- ans =
- 0.00000 0.00000
- 15.00000 0.25882
- 30.00000 0.50000
- 45.00000 0.70711
- 60.00000 0.86603
- 75.00000 0.96593
- 90.00000 1.00000
- 105.00000 0.96593
- 120.00000 0.86603
- 135.00000 0.70711
- 150.00000 0.50000
- 165.00000 0.25882
- 180.00000 0.00000
- % Table with angles in degrees, radians and tan
- computeTableTrig2 = @(min, step, max, fn)[ (min:step:max)' (pi / 180 * (min:step:max))' (fn(pi / 180.0 * (min:step:max)))']
- octave:29> computeTableTrig2(0.0, 15.0, 180.0, @sin)
- ans =
- 0.00000 0.00000 0.00000
- 15.00000 0.26180 0.25882
- 30.00000 0.52360 0.50000
- 45.00000 0.78540 0.70711
- 60.00000 1.04720 0.86603
- 75.00000 1.30900 0.96593
- 90.00000 1.57080 1.00000
- 105.00000 1.83260 0.96593
- 120.00000 2.09440 0.86603
- 135.00000 2.35619 0.70711
- 150.00000 2.61799 0.50000
- 165.00000 2.87979 0.25882
- 180.00000 3.14159 0.00000
Add Comment
Please, Sign In to add comment