2

I should probably delete this question, since I found an answer elsewhere on stackoverflow.

Using 'Vectorize' solved the problem.

outer(x,y,Vectorize(model))

I'll leave this up for a bit in case this helps anyone else. The issue I had is described below.

I am an occasional user of R, mostly for Scientific Programming. I want to evaluate a function on a grid of x,y values. Something similar to this simple example:

> x=seq(1,5,1)
> y=seq(1,5,1) 
> model = function(a,b){a+b}


> (outer(x,y,model))
[,1] [,2] [,3] [,4] [,5]
[1,]    2    3    4    5    6
[2,]    3    4    5    6    7
[3,]    4    5    6    7    8
[4,]    5    6    7    8    9
[5,]    6    7    8    9   10

The input and output of my actual function looks like this (the volumetric flow rate of a 'Cross' fluid in a circular pipe):

    > SimpleCrossModelCircPipe(eta_0 = 176.45, lambda = 2.4526, m=0.83122, mesh=1000,
pipe_length=1.925, press_drop=20.3609, pipe_diameter=0.0413)
[1] 0.005635064

I want to evaluate the function on a grid of values, for example, press_drop and pipe_diameter. So, define a function of these two variables.

> model = function(a,b){SimpleCrossModelCircPipe(eta_0 = 176.45, lambda = 2.4526, 
m=0.83122, mesh=1000,pipe_length=1.925, press_drop=a, pipe_diameter=b)}

For just one pair of values this does work:

> model(1,2)
[1] 107249.8

But, if I use with 'outer', there is an error.

> (outer(x,y,model))

Error in seq.default(tau_wall, 0, length.out = mesh + 1) : 'from' must be of length 1

If I place a print statement for the local variable, tau_wall, using outer generates all 25 values, before moving to the line, which generates the error, since tau_wall should have length 1, not 25.

(outer(x,y,model))
 [1]  352.5289  705.0579 1057.5868 1410.1158 1762.6447  705.0579 1410.1158 2115.1736
 [9] 2820.2315 3525.2894 1057.5868 2115.1736 3172.7605 4230.3473 5287.9341 1410.1158
[17] 2820.2315 4230.3473 5640.4630 7050.5788 1762.6447 3525.2894 5287.9341 7050.5788
[25] 8813.2235

Is there a simple fix to make this work?

Best,

Steve

SteveK
  • 53
  • 5

0 Answers0