How to have sigma of something while plotting?

106 Views Asked by At

I am using plotFun function of mosaic package in R. I am trying to plot a 3D plot. Following is my code snippet:

plotFun(2*l*(w/log(beta*kspill*lambda^2+(1+(w/x-w)/10)*(lambda^2*5+lambda^1*5+1))+(w/x-w)/log((1+((w/x-w)/10)^1)*(lambda^2*5+lambda^1*5+1))) ~ x  & lambda ,
        x.lim=range(0.8,1), lambda.lim=range(1,3),
        l=60,w=40,kspill=10,f=1,beta=0.5,surface=TRUE)

This is working fine. Now suppose I want to fix lambda and introduce a new variable t such that if t=2 we get lambda^2*5+lambda^1*5+1 as in the above case. If t=3 we get lambda^3*5+lambda^2*5+lambda^1*5+1 and so on. So now I have t.lim=range(1,3) for a fixed lambda :

plotFun(2*l*(w/log(beta*kspill*lambda^2+(1+(w/x-w)/10)*("depends on t"))+(w/x-w)/log((1+((w/x-w)/10)^1)*("depends on t"))) ~ x  & lambda ,
        x.lim=range(0.8,1), t.lim=range(0.5,1),
        l=60,w=40,kspill=10,f=1,beta=0.5,lambda=1,surface=TRUE)

What to write in the "depends on t" part above. I guess we can't put a for loop there to calculate 5* {summation i=0 to i=t}lambdai. How to go about doing this?

1

There are 1 best solutions below

0
On BEST ANSWER

You can define your "depends on t" with

depends_on_t <- makeFun(5 * sum(lambda^(1:round(t))) + 1 ~ t + lambda, lambda = 1)

But you still have some issues to resolve:

1) Your plotFun() command is creating a plot using x and lambda, but I'm guessing you meant x and t.

2) t can only be an integer if you are going to use it in a sum of the type you are suggesting. But you are creating a plot that assumes continuous variables for the axes. I inserted round(t) as one way to move from real values to integer values before computing the sum. The makes the function work for non-integer values, but it might not be what you really want.

Finally, some additional suggestions:

3) x & lambda should be replaced with x + lambda. The use of & here dates back to the very early days of the mosaic package, and although it is still supported (I think, we don't really test for it anymore), we prefer x + lambda.

4) I recommend separating out the definition of your function from the plotFun() command. You can use mosaic::makeFun() as illustrated above, or the usual function() to define your function and whatever default values you want for its arguments. Then you can do sanity checks on the function, or use it in multiple plots rather than including all of the function's definition in each plot.

5) Using spaces and returns would make your code much more readable. (As would simplifying your example to a minimal example that demonstrates the issue you are asking about.)

6) I think you might want

depends_on_t <- makeFun(5 * sum(lambda^(0:round(t))) ~ t + lambda, lambda = 1) rather than the formula as you describe it, but without more context, I can't really know.