# Function cost minimization

I have to do the minimization of a cost function based on parameters

Is there a decent ruby gem (that works with rails) to do this?

Rodrigo R. wrote in post #1040708:

I have to do the minimization of a cost function based on parameters

Is there a decent ruby gem (that works with rails) to do this?

From what I can tell “gradient descent” is just math:

# From calculation, we expect that the local minimum occurs at x=9/4

x_old = 0
x_new = 6 # The algorithm starts at x=6
eps = 0.01 # step size
precision = 0.00001

def f_prime(x):
return 4 * x3 - 9 * x2

## while abs(x_new - x_old) > precision: x_old = x_new x_new = x_old - eps * f_prime(x_new) print "Local minimum occurs at ", x_new

Maybe just translating that to Ruby will give you what you want. Note:
I’m no mathematician. You may have to consult someone who is the above
needs any tweaking.

The problem is that the function I’m trying to minimize is a lot more
complicated (so is its derivative), so I wanted a function that could do
the minimization for me rather than implementing one myself

On Friday, January 13, 2012, Robert W. [email protected] wrote:

while abs(x_new - x_old) > precision:
Posted via http://www.ruby-forum.com/.

You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at

What libraries did you use?

Rodrigo R. wrote in post #1040810:

The problem is that the function I’m trying to minimize is a lot more
complicated (so is its derivative), so I wanted a function that could do
the minimization for me rather than implementing one myself

I thought it might be something like that. Sorry I don’t know of
anything like that either. Is this something that can be solved with
matrix math? I noticed mention of that in what I was reading. I’ve used
libraries in the past to solve some differential equations, but I had
the advantage of having the matrix equation given to me by the company
that created the device where it was needed.