Skip to content

Trouble with compound functions---differential equations

4 messages · Aimee Jones, Rolf Turner, Berend Hasselman

#
On 16/06/11 11:07, Aimee Jones wrote:
Actually your code should read:

T1<-function(t) {27.5-12.5*cos(2*pi*t/365)}

and

B1<-function(T1,t) {dnorm(T1(t),mean=22.5,sd=3.3)}

i.e. don't assign the value that you calculate in the code; this
is the value ***returned*** by the function.  What you is in effect
harmless here, but it is confusing and could cause problems in
other contexts.
Well, T1() a function of ***t*** only (where t is the variable against which
you expect the values of T1() to be plotted.  Whereas, B1 is a function of
two variables T1 and t, which confuses things.

Note that by calling plot() in this way you are in fact calling 
plot.function()
which is in fact a wrapper for curve().  As has been discussed recently on
this list, the syntax for curve() is a bit delicate.

A workaround for your problem is:

     plot(function(t){B1(T1,t)},0,3650)

HTH

     cheers,

         Rolf Turner
#
Aimee Jones wrote:
Because B1 is a function with 2 arguments.
plot calls B1 with 1 argument, which will be argument T1. So t is missing
since it hasn't received a value.

Redefine B1 as 

B1<-function(t) {B1<-dnorm(T1(t),mean=22.5,sd=3.3)}

and you will get your plot.

Berend


--
View this message in context: http://r.789695.n4.nabble.com/Trouble-with-compound-functions-differential-equations-tp3601070p3601403.html
Sent from the R help mailing list archive at Nabble.com.