0

This has been a recurring problem for me, so I'll have to address it here. I'm trying to build a non-linear optimization problem using optim or fsolve, and I need certain variables as fixed parameters.

So by f(a;b), I mean a function that defines b as given, and immovable for the optimization. Evidently matlab has a way to define b as just a parameter set & only allow a to be moved in the optimization; in R this doesn't seem to apply in the same way as matlab. Both optim & fsolve require that I specify the initial values of both a & b, and they calculate the results shifting b as well as a.

If anyone could let me know of a good way out of this that would be much appreciated. Thank you!

John Shin
  • 33
  • 4
  • 1
    Could you give an example of the signature of the function you're trying to optimize? `optim` expects a function with a single parameter vector argument, I believe. My suggestion would be a function factory that returns a function with `b` fixed, though not sure if that works for you – Calum You Mar 21 '22 at 21:25
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Likely some sort of closure would help but it's hard to say for sure without a more specific example. – MrFlick Mar 22 '22 at 01:22
  • Sure - the function has a vector (a), an id number (b), and a constant (c). the function f(a; b, c) does the following: - convert b into a vector of the same length as "a" (call it "v"). - take a fraction of some exponential combination of b and v to compute another vector "s"; so s is a function of a and v. - return a vector linear combination of "a", "s", and "c" (call it "result"). I want the optimization to return me a vector a (a*) that makes the final linear combination 0. I can write a criterion function Q = (result)^2 to use optim on, but it will also shift values b and c. – John Shin Mar 22 '22 at 13:03

1 Answers1

0

You can pass b via the ... argument of optim.

Enrico Schumann
  • 1,278
  • 7
  • 8