N
Nick S
Hello everyone on Ruby-Lang!
I'm currently experimenting with writing an iteration-based
mathematical modeller* and would like to bounce some ideas off you.
Currently the models are defined in somewhat cumbersome ruby, but I'd
like the method of model creation to be somewhat more elegant, and I
was wondering if the following (currently hypothetical) code could be
made to be functional:
------------------- ** -------------------
@m = Modeller::Model.new
@m.step_size = 0.1
t = @m.independent_vart, 1.0)
x, v, a = @m.declare_varsx, :v, :a)
# x = 3*t + 2
@m.relationshiplinear, :x, :t) { |x, t|
x = 3*t + 2
}
# dx/dt = v
@m.relationshipdifferential, :x, :v, {:with_respect_to => :t}) {
|x, v|
d_dt(x) = v
}
# dv/dt = 2a^2
@m.relationshipdifferential, :v, :a {:with_respect_to => :t}) { |v,
a|
d_dt(v) = 2*(a**2)
}
@m.set_initial_valuesx => 5.0, :v => 0.0, :a => 0.0)
------------------- ** -------------------
I've given you a bit of context to show you how the rest of the model
is defined, but the important bit is the relationship definitions, and
I hope you can see how the relationships are meant to work. The
question really is -- is this possible? I need to extract a
relationship from the code passed into the blocks (it could just as
well be a string if that helps) ... and am at a loss as to how to parse
it/make it work.
To give a brief example, from the linear relationship above, what
should happen is I should be able to get 't' in terms of 'x' so I can
extract the raw value and adjust *that* with a linear relationship, and
then apply the whole process backwards to get the "actual" value of
'x'. In code:
------------------- ** -------------------
def update_value_with(var, upstream)
val = value_from_raw(
upstream.value/upstream.value(-1) *
raw_from_value(var.value)
)
var.set_value(val)
end
def raw_from_value(value)
(value - @const) / @factor
end
def value_from_raw(raw)
(raw * @factor) + @const
end
------------------- ** -------------------
I hope some of this is decipherable, and look forward to hearing your
innovative replies!
Many thanks in advance,
Nick S
* (à la Modellus [http://phoenix.sce.fct.unl.pt/modellus/] if any of
you know it -- although my modeller is currently at least 100-200 times
faster than Modellus -- admittedly with a more basic differential
equation algorithm)
I'm currently experimenting with writing an iteration-based
mathematical modeller* and would like to bounce some ideas off you.
Currently the models are defined in somewhat cumbersome ruby, but I'd
like the method of model creation to be somewhat more elegant, and I
was wondering if the following (currently hypothetical) code could be
made to be functional:
------------------- ** -------------------
@m = Modeller::Model.new
@m.step_size = 0.1
t = @m.independent_vart, 1.0)
x, v, a = @m.declare_varsx, :v, :a)
# x = 3*t + 2
@m.relationshiplinear, :x, :t) { |x, t|
x = 3*t + 2
}
# dx/dt = v
@m.relationshipdifferential, :x, :v, {:with_respect_to => :t}) {
|x, v|
d_dt(x) = v
}
# dv/dt = 2a^2
@m.relationshipdifferential, :v, :a {:with_respect_to => :t}) { |v,
a|
d_dt(v) = 2*(a**2)
}
@m.set_initial_valuesx => 5.0, :v => 0.0, :a => 0.0)
------------------- ** -------------------
I've given you a bit of context to show you how the rest of the model
is defined, but the important bit is the relationship definitions, and
I hope you can see how the relationships are meant to work. The
question really is -- is this possible? I need to extract a
relationship from the code passed into the blocks (it could just as
well be a string if that helps) ... and am at a loss as to how to parse
it/make it work.
To give a brief example, from the linear relationship above, what
should happen is I should be able to get 't' in terms of 'x' so I can
extract the raw value and adjust *that* with a linear relationship, and
then apply the whole process backwards to get the "actual" value of
'x'. In code:
------------------- ** -------------------
def update_value_with(var, upstream)
val = value_from_raw(
upstream.value/upstream.value(-1) *
raw_from_value(var.value)
)
var.set_value(val)
end
def raw_from_value(value)
(value - @const) / @factor
end
def value_from_raw(raw)
(raw * @factor) + @const
end
------------------- ** -------------------
I hope some of this is decipherable, and look forward to hearing your
innovative replies!
Many thanks in advance,
Nick S
* (à la Modellus [http://phoenix.sce.fct.unl.pt/modellus/] if any of
you know it -- although my modeller is currently at least 100-200 times
faster than Modellus -- admittedly with a more basic differential
equation algorithm)