Message-ID: <64EEA4E0-418A-4539-9F6C-C6D1731B02E5@comcast.net>
Date: 2013-12-19T19:24:38Z
From: David Winsemius
Subject: A function which is a sum of other functions...
In-Reply-To: <8F1C96F7-DBB7-4C6D-A998-E793A529F955@gmail.com>
On Dec 19, 2013, at 11:05 AM, Onur Uncu wrote:
>
> Dear R Users
>
> I have a list of functions. Each function in the list is a function of single variable. I would like to create a function (of one variable) which represents the sum of all the functions in the list. So, if the functions in my list are f1(x),..,f5(x) then I would like a new function f(x)=f1(x)+f2(x)+...f5(x)
>
> Appreciate any suggestions on how to do this.
>
> I need the above f(x) function because I would like to minimise it with respect to x using the nlm function.
> fbig <- function(x, f1=I, f2=I, f3=I, f4=I, f5=I){
f1(x)+f2(x)+f3(x)+f4(x)+f5(x)}
> fbig(2)
[1] 10
> fbig(2, exp)
[1] 15.38906
> fbig(2, exp, log)
[1] 14.0822
Since "+" is vectorized this should not need to be wrapped in sapply as long as each function is itself vectorized.
David Winsemius
Alameda, CA, USA