I’ve only been programming in Ruby for a few days (so be gentle), but I
wrote the beginning of an object that could do numerical approximations
of Differential Equations, below, as well as a simple test to see if the
function works (it’s the ODE y’ - y = 0). However, I’m not sure how to
pass functions as instance variables in Ruby. Any help here? (I’m sure
there’s already a gem that does DE approximations, I’m just practicing!)
The problem is that when I use .call on @function, I get an error saying
that I’ve supplied 0 of 2 arguments needed for the function. I’m not
sure why this happens.
class EulerDE
def initialize(x0, y0, function)
@x = x0
@y = y0
@function = function
end
def eulermethod(h, max)
count = 1
puts “#{count}) x = #{@x}, y = #{@y}”
while count < 10
x = @x + h
y = @y
y = @y + h * @function.call(x, y)
count += 1
puts “#{count}) x = #{@x}, y = #{@y}”
end
end
end
def func(x,y)
y
end
de = EulerDE.new(0, 1, &func)
de.eulermethod(0.03, 10)