Mathematica Optimizing parameters without specifying them (Mathematica)

Click For Summary
The discussion focuses on optimizing a likelihood function using NMinimize in Mathematica, where the user seeks a way to automate the declaration of parameters based on the dimensions of a vector. Currently, the user must hard code the parameters for each vector size, which is inefficient. They inquire if Mathematica can utilize anonymous variables or automatically generate parameter names like parameter1, parameter2, etc. The provided solution demonstrates how to create a function that generates these parameters dynamically based on the input size. This approach streamlines the optimization process by eliminating the need for manual adjustments in the code.
yourgoldteeth
Messages
9
Reaction score
0
I use NMinimize to optimize a likelihood function. The number of parameters included depends on the dimensions of a vector I pass. I would like to have Mathematica check the dimensions of the vector, declare the correct number of parameters for me, and then solve for them.

Right now, I have to hard code my notebook for each different size of vector I hand it. Is there anyway to have Mathematica either use anonymous variables, or declare parameter1, parameter2... parameterN variables for me, automatically, and use them?

Thanks
 
Physics news on Phys.org
myNMinimize[f_,n_]:=NMinimize[f,Table[ToExpression["parameter"<>ToString],{i,1,n}]]

Then
myNMinimize[(parameter1-3)^2+2(parameter2+1)^4+parameter3^2,3]
returns
{parameter1->3., parameter2->-1., parameter3-> 0.}
 
Last edited:

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
6K