How to properly use the nlm function in R for minimizing functions?

Click For Summary
To minimize functions in R using the nlm function, the function must be defined to accept an array of variables rather than individual parameters. The original function f should be modified to take an array x, where each element corresponds to a variable. The initial guess values must also be provided as an array p that matches the dimensions of x. The corrected function definition is f <- function(x) { x[1]^2 + x[2]^2 + 10 - 5*x[1] - x[2] }, and the initial guesses can be set with p = array(c(1, 0), dim=c(2,1)). The nlm function is then called with these parameters, allowing it to optimize both x[1] and x[2]. This approach ensures that nlm operates correctly by manipulating all independent variables defined in the function.
the_dane
Messages
29
Reaction score
0
I try to find out how to minimize functions i R by using nlm function:
> f<-function(x,y){x^2+y^2+10-5*x-y}
> nlm(f,0.1,0.1)

That only gives me an estimate for x. How would write the code to get x and y?
 
Technology news on Phys.org
From
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)

I gather p must be an array, not just a number like 0.1

(as becomes a good fortran programmer, I know nothing of R... :rolleyes: )
 
  • Like
Likes FactChecker
BvU said:
From
nlm(f, p, ..., hessian = FALSE, typsize = rep(1, length(p)),
fscale = 1, print.level = 0, ndigit = 12, gradtol = 1e-6,
stepmax = max(1000 * sqrt(sum((p/typsize)^2)), 1000),
steptol = 1e-6, iterlim = 100, check.analyticals = TRUE)

I gather p must be an array, not just a number like 0.1

(as becomes a good fortran programmer, I know nothing of R... :rolleyes: )
Also the independent input values of the function f must be an array x of dimension the same as p. (see https://stat.ethz.ch/R-manual/R-devel/library/stats/html/nlm.html )
 
Your code is calling nlm incorrectly. And the function is defined incorrectly for using nlm.

All the independent variables of f that nlm should manipulate should be in the array x and all their starting guess values should be in a matching array p. There are no constant parameters to pass to f.

Try something like this:
Code:
# Define a function of array x.
 f<-function(x){x[1]^2+x[2]^2+10-5*x[1]-x[2]}

# Initial guess values for x
 p = array( c( 1, 0), dim=c(2,1) )

# Call nlm
 ans <- nlm(f,p)

# print answer
 print(ans)
 
Last edited:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 0 ·
Replies
0
Views
656
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
1K
  • · Replies 0 ·
Replies
0
Views
1K
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
6K