Hi! I've just begun writing a program that searches for the minimum of a function with golden section search. In order to do this in a nice way I need a function that takes a function name and an argument and returns the function value for that argument, i .e just like Matlab's 'feval'. Is there any? Thanks before hand! Best regards, Fredrik Thuring, Codan Insurance A/S Research Department ------------------------------------------------------------------------------ This e-mail and any attachment may be confidential and may also be privileged. If you are not the intended recipient, please notify us immediately and then delete this e-mail and any attachment without retaining copies or disclosing the contents thereof to any other person. Thank you. ------------------------------------------------------------------------------ [[alternative HTML version deleted]]
Hello Fredrik
do.call() constructs and executes a function call from the name of
the function and a list of arguments to be passed to it.
R's equivalent to fminsearch is optim() and that of fmin is optimize()
HTH
[neither of these are in R-and-octave2.txt, which I really really ought
to update]
rksh
On Jun 24, 2005, at 10:14 am, Fredrik Thuring wrote:
>
> Hi!
>
> I've just begun writing a program that searches for the minimum of a
> function with golden section search. In order to do this in a nice way
> I
> need a function that takes a function name and an argument and returns
> the
> function value for that argument, i .e just like Matlab's
'feval'. Is
> there any?
>
> Thanks before hand!
>
> Best regards,
> Fredrik Thuring, Codan Insurance A/S Research Department
>
>
> -----------------------------------------------------------------------
> -------
> This e-mail and any attachment may be confidential and may also be
> privileged.
> If you are not the intended recipient, please notify us immediately
> and then
> delete this e-mail and any attachment without retaining copies or
> disclosing
> the contents thereof to any other person.
> Thank you.
> -----------------------------------------------------------------------
> -------
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
On Fri, 24 Jun 2005, Fredrik Thuring wrote:> I've just begun writing a program that searches for the minimum of a > function with golden section search. In order to do this in a nice way I > need a function that takes a function name and an argument and returns the > function value for that argument, i .e just like Matlab's 'feval'. Is > there any?Really? Take a look at the interface for optimize(): in R you almost always want to pass the function not its name. You could use feval <- function(fn, x) get(fn)(x) but it would be rather inefficient to keep looking up the function (and there are scoping issues to consider here). You could also use do.call(fn, list(x)), but that is even less direct. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
maybe you want something like this:
f <- function(x, FUN, ...){
FUN <- match.fun(FUN)
FUN(x, ...)
}
f(seq(-3, 3, 0.5), dnorm, sd = 1.1)
f(seq(-3, 3, 0.5), "pnorm", sd = 1.1)
but also take a look at ?optimize(), which will probably take you out
of the trouble of writing a new function yourself.
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Fredrik Thuring" <frt at Codan.dk>
To: <r-help at stat.math.ethz.ch>
Sent: Friday, June 24, 2005 11:14 AM
Subject: [R] Counterpart for Matlab's 'feval'?
>
> Hi!
>
> I've just begun writing a program that searches for the minimum of a
> function with golden section search. In order to do this in a nice
> way I
> need a function that takes a function name and an argument and
> returns the
> function value for that argument, i .e just like Matlab's
'feval'.
> Is
> there any?
>
> Thanks before hand!
>
> Best regards,
> Fredrik Thuring, Codan Insurance A/S Research Department
>
>
>
------------------------------------------------------------------------------
> This e-mail and any attachment may be confidential and may also be
> privileged.
> If you are not the intended recipient, please notify us immediately
> and then
> delete this e-mail and any attachment without retaining copies or
> disclosing
> the contents thereof to any other person.
> Thank you.
>
------------------------------------------------------------------------------
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>