I have a function f(n) to be computed into Column E in my worksheet. The table is a dynamic range which grows with new input.
The formula describes the kinetics of a dose-response. On day 1, there are no terms to sum. On day 2, there is one summation term. On day 3, there are two summation terms and so on. More weightage is provided to w(i) closer to day of calculation and lesser to those earlier through the exponential term.
Function description as shown in the following image :
Function f(n)
[1
n = number of days, basically how many ever rows are present.
k1 & tau1 are constants provided already.
w(i) is provided on each row which is an input to the function f(n).
Could you please advise a computationally cheap way to compute this sigma summation over several hundred rows in Excel, thanks!
UPDATED :
How worksheet looks:
I tried this formula :
$B$4*SUMPRODUCT(D2*EXP(-1*(ROW(INDIRECT($A$2&":"&C2-1)))/$B$3))
B4 = k1, D2 =w(n), A2 = Start, C2 = n, B3 = tau.
See worksheet and formula assignment: https://i.stack.imgur.com/ULjI0.jpg
The implementation is not correct because if I go to any row and set w(n) = 0, then f(n) tends to go to 0 which doesn't seem right. Appreciate any help to correct the error, thanks.
Related
I have the following table:
My objective is to spread out the 10,000 units across the 5 months, given the weightings below each month. The row adjacent the 10,000 is my best attempt at getting this (I have spent a few hours on this). The yellow is what I am after.
The logic I have tried to use to get the calculation is as follows: Calculate the even spread across the months (in this example 10,000 / 5 = 2,000 = X). Multiply each month weighting to get the weighted amount (in this example to get M2 = 1,600 and M4 = 3,000: X + (X * M2 weighting) etc). You can then take the total 10,000 minus the summation of these which will give you 5,400, which you can then distribute across the months don't don't have a weighting. There must be an easy way to do this with a SUMPRODUCT but I can't seem to figure it out.
My best attempt so far is this:
=IF(B3=0,SUMPRODUCT(ABS((10000/5)*$B3:$F3)),(10000/5)+((10000/5)*B3))
Try the following:
Formula in B3:
=IF(B$2=0,($A3-SUMPRODUCT(($B2:$F2<>0)*(($A3/5)*(1+$B2:$F2))))/COUNTIF($B2:$F2,0),($A3/5)*(1+B$2))
Drag right.
Logic:
=IF(B$2=0,<true>,<false>) - Check if cell above equals zero
If above is false (so other than 0) then: ($A3/5)*(1+B$2) - First divide (10000/5) and multiply that outcome by (1-<percentage>)
If IF yields true - Check which cells in range are other than zero and multiply by the same logic as per step 2. The sum gained with SUMPRODUCT can then be deducted from 10000 and devided by the count of cells that actually do hold zero (hence the COUNTIF).
Is there a single excel formula that can take integer inputs N and K and generate the binomial coefficient (N,K), for positive or negative (or zero) values of N?
The range of N and K should be fairly small e.g. -11 < N < +11 and -1 < K < +11. Otherwise large numbers will be generated that exceed excel's capabilities.
CONTEXT
Excel does not provide a Binomial function. So how how to get around this? The binomial function for positive N is straightforward:- Binomial(N,K) = Factorial(N)/(Factorial(N-K)*Factorial(K)). But this doesn't work for negative N.
For information on Binomial Coefficients there is useful stuff in Ken Ward's pages on Pascals Triangle and Extended Pascal's Triangle.
I wanted to make a similar tabular resource in Excel...but with one single table covering positive, zero and negative values of $N$.
One efficient way of doing this is to define a single formula which can be used in every cell of the table. The formula should discriminate between values of N which are negative, zero, or positive and use appropriate logic to obtain the correct output in each case.
Of course, rather than build a whole table, the same formula can be used to calculate the binomial coefficient for a single (N,K) input pair of values.
For anyone else who ends up here through google, Excel actually does have COMBIN for N >= K >= 0. If you know the inputs would otherwise be valid, one option for handling K > N would be IFERROR(COMBIN(N, K), 0), with the advantage that you only specify N and K once, and the disadvantage of hiding when your assumptions inevitably turn out to be wrong.
To bring it around to an actual answer (though honestly I'd have preferred just leaving a comment if the site would let me), the other answer's formula can then be simplified to
IF(A1>-1,IF(B1>A1,0,COMBIN(A1,B1)),(-1)^(B1)*COMBIN(-A1-1+B1,B1))
as a bonus, it seems to be able to handle a larger range of inputs, as however COMBIN is implemented avoids the issue of the FACTs temporarily exceeding 1.8e308 even though much of that would be cancelled out in the division.
You can simulate a binomial function by using a conditional formula in a single Excel cell which takes as input the contents of two other cells.
e.g. if worksheet cells A1 and A2 contain the numeric values corresponding to N,K in the binomial expression (N,K) then the following conditional formula can be put in another worksheet cell (e.g. A3)...
=IF(A1>-1,IF(B1>A1,0,(FACT(A1)/(FACT(B1)*FACT(A1-B1)))),(-1)^(B1)*(FACT(-A1-1+B1)/(FACT(B1)*FACT(-A1-1+B1-B1))))
This will handle positive and negative (and zero) values of N. In this solution both N and K must be integers (including zero). There is a limit to the size of N and K that excel will be able to cope with (but I havent tested the limits beyond the range -11
The excelf formula uses the conditional construct: IF(test,operation if true, operation if false).
In pseudo-code the logic is as follows:-
IF(N>-1) THEN
IF(K>N) THEN
Result = 0
ELSE
Result = (N!)/(K!*(N-K)!)
ENDIF
ELSE
Result = (-1)^(K) * (-N-1+K)! / ( (K)! * (-N-1+K-K)! )
ENDIF
Note the formula uses the Upper Negation Identity to determine coefficients when N is negative:-
(-N,K) = (-1)^K * (K-N-1,K).
Pascal's Triangle Table
To create a "Pascal's Triangle"-type table for negative and positive values of N, proceed as follows.
(1) Create a new blank excel worksheet.
(2) In column B put the integer N values (starting at cell B4 and proceeding downwards):-
e.g Nmin,Nmin-1,...-2,-1,0,1,2,3,...,Nmax-1,Nmax.
(3) In row 3 put the integer K values (starting at cell C3 and proceeding rightwards):-
0,1,2,3,...Kmax.
(4) Then in cell (C4) enter the conditional formula:-
=IF($B4>-1,IF(C$3>$B4,0,(FACT($B4)/(FACT(C$3)*FACT($B4-C$3)))),(-1)^(C$3)*(FACT(-$B4-1+C$3)/(FACT(C$3)*FACT(-$B4-1+C$3-C$3))))
(5) Copy cell C4 and paste it to all cells in the grid bounded (at left and at top) by your N and K values.
The grid cells will then contain the binomial ceofficient corresponding to (N,K).
First of all, let me show you guys the equation in question.
In this equation S, V, and t are known constants. CFL is also known. We have an initial value for D, and we have no idea what k is.
What I need to do is find ideal values for both D and k that would minimize the residuals squared of a calculated CFL and a measured CFL. Using residuals squared is just a way for me to check if they're the best possible values, but it's fine if there's another way to go about this that uses some other method.
The residual squared is just the absolute value of the difference between the calculated and measured CFLs, which is then squared. The lower the residual squared, the better the fit we have. So I need the smallest possible residual squared resulting from putting both k and D into the equation. That'll result in a calculated CFL, which I can then compare to a measured CFL, allowing me to calculate the residual squared.
My first idea for how to do this, since I'm not sure how to use Excel equations, was to fix the value of D (since we have an initial starting value to work from) and then vary through different values of k, putting them into the equation to find a calculated CFL, and comparing that to the measured to find the residuals squared, until I find one that results with the smallest residuals squared. Then I fix k at that ideal value, and vary D until I find the smallest residual there as well. Then I fix D again, and go back to varying k. My idea was that I could keep bouncing back and forth like that until both D and k were within a certain percentage of their previous values. I assumed it would reach some sort of equilibrium with this method
However, the numbers just go crazy, and end up either going to zero or going to infinity. So I need to rework my process. Which is where you guys come in!
How would you go about finding the most ideal values for both D and k, which would result in a calculated CFL closest to the measured one, assuming you are given values for every variable above apart from k? Remember to factor in that the value of D given initially is simply a starting place to work from, and is not the most ideal value.
I've been working on this program for a long time (at least a month), and I'm just stuck as hell and desperate. I was hoping you guys could help me out.
Here are some initial values to work with:
S = 19.634954
V = 12.271846
D (initial) = 0.01016482
CFL (measured) = 0.401
t = 4
k = ?
Thank you for any ideas you might have.
As Dean said, your system has two unknowns, and in the general case an infinite number of solutions (different pairs of (D,k)). By fixing D, CFL is a continuous function of k, and as such, you should be able to find a k that gives the CFL you measured (within some accuracy). For this problem (i.e., finding k given CFL) you can use the Goal Seek tool. Here is how:
1) Problem setup:
Use the name of the variables to name the cells in which you input their values (Go to Formulas--> Defined Names --> Define Name and give some the name of each variable to a cell). Then input the values of your parameters in these cells, (give k an arbitrary value, eg = 1), and input the formula in cell CFL like:
=(S/V)*SQRT(D/k)*(ERF(SQRT(k*t))+SQRT(k*t/PI())*EXP(-k*t))
Again, note that S,V,D,k and t are defined as named ranges.
2) Problem Solution:
Go To Data --> Data Tools --> What-If Analysis --> Goal Seek and enter the following parameters:
Set Cell: CFL
To value: 0.401
By changing cell: k
This gave me k=0.151759378, which results in CFL = 0.401261265054823.
I hope this helps?
Edit: Finding some solution pairs using VBA:
1) Place the measured CFL value in a cell (I chose H2).
2) Replace named ranges k, D and CFL. I used rngK, rngD and rngCFL, each one starting from row 2 till row 20.
3) Fill down rngD with a step (I took 0.01) using the formula =INDEX(rngD,ROW()-ROW($C$2))+0.01. The first entry of rngD is in cell C2 and has the value 0.01016482. The formula is copied down to all other cells in the range.
4) Fill down rngK with some initial values (I took =1).
5) Fill down the rngCFL range with the formula =(S/V)*SQRT(INDEX(rngD,ROW()-ROW($G$1))/INDEX(rngK,ROW()-ROW($G$1)))*(ERF(SQRT(INDEX(rngK,ROW()-ROW($G$1))*t))+SQRT(INDEX(rngK,ROW()-ROW($G$1))*t/PI())*EXP(-INDEX(rngK,ROW()-ROW($G$1))*t)). I use the ROW() and INDEX() functions to refer to the Range element I need.
6) Finally, use this code in a sub:
Dim iCnt As Long
For iCnt = 1 To Range("rngk").Count
Range("rngCFL")(iCnt).GoalSeek goal:=Range("H2"), changingCell:=Range("rngK")(iCnt)
Next iCnt
The above generates 19 pairs (D,k) that give the measured CFL value.
You can't solve for two unknown variables in a 1 formula system. However if I take D as given then you have a 1 unknown/1 formula system.
I just simply used 1 column as a guess of k (for me column B. I used another column to represent the calculated CFL with the guessed k (for me column C). I have another column that has either a 1 or -1 (for me column D). Lastly I have a column that represents the absolute value by which I want to increment my guess.
I named cells with the given values of the variables to make it easier to use them.
I started with a guess of k=1.
Here are my formulas in my first row which was 7.
B7=.1
C7 =(s/v)*(d/B7)^0.5*(ERF(((B7*t)^0.5))+((B7*t)/PI())^0.5*EXP(-1*B7*t))
nothing in D7 or E7
in row 8:
B8=B7+E8+D8
C8==(s/v)*(d/B8)^0.5*(ERF(((B8*t)^0.5))+((B8*t)/PI())^0.5*EXP(-1*B8*t))
D8=1
E8=.01
in row 9 the B and C column is just copied down but D and E are as follows
D9==IF(C9>cfl,1,-1)
E9==IF(D9=D8,E8,E8/10)
Once you get those in you can just copy down however many rows you want.
What this does is every time the residual of the CFL switches signs the increment's sign will also flip. Additionally, the absolute value of the increment will also shrink by a factor of 10 to give more precision as it goes.
This is by no means the best way to solve your problem but it is a way.
I have a parameter in A1 that influences "TOTAL" in a random and very high standard deviation. Lets say A1 is 2...then TOTAL Values could be 1...5...17...3...2..2...etc If A1 is 1 then TOTAL Values could be 1....3...5..15...9...10..etc
I would like solver to figure out which value in A1 would equate to the best AVERAGE of TOTAL after X runs. Where I can define X.
In my example you can tell that A1=1 is better on average after 6 runs. However, if you run solver normally it would say A1=2 is the best, because it produced a value of 17.
This doesn't seem to be the kind of problem you solve with solver. Why not write a macro that loops through the values of A1, X times, keeping a running sum of the TOTAL values for each A1? When it's all over, the largest sum is also the largest average.
The inner loop will be something like this:
Redim tSum(1 to maxA1)
for i = 1 to maxA1
tSum(i) = 0
for j = 1 to X
[A1] = i
Application.calculate
tSum(i) = tSum(i) + TOTAL
next j
next i
'now step through tSum. The index of the largest value
' is the value of A1 desired. Put it in a handy cell.
It has to be a macro, not a function because it changes A1.
How can I use sigma with boundaries in excel 2007? For example I want to calculate this value:
sigma [e^(m-n)*i(m)]
in which n values are a column starting from 0 to 100 and for each n, m is started from 0 ended to n and i(m) is corresponding value specified in Raw m and form a column. For example for n=100:
sigma [e^(m-100)*i(m)] and m is 0 to 100.
Please don't tell to write for each raw separately and then sum up because for n=1 to 100 I should do this operation for each n, n times which leads to 1+2+3+...+100=100*101/2=5050 times.
#javad, your description is very difficult to follow e.g. "i(m) is corresponding value specified in Raw m and form a column"???
My guess: You want to tabulate the values of function F(n) for n=0,...,100. F(n) is defined as the sum over m=0,...,n of the expression e^(m-n)*i(m) -- where i(m) is some function of m. Abbreviate this as F(n) = sigma(0,n) of e^(m-n)*i(m)
Is that correct? Whether it's correct or not, please edit your question to provide a clear unambiguous description of what you want.
You should also manually calculate the first few values (say F(0) to F(3)) and publish those as well as i(0) to i(3) for use as test data.
You might also give an idea of what kind of precision you expect.
Here's a tentative start on a solution:
Firstly rewrite F(n) as (sigma(0,n) of e^m * i(m)) / e^n
Then fill in the cells like this:
a1: heading n, a2 to a5: 0,1,2,3
b1: heading i(n), b2 to b5: i(0), ..., i(3)
c1: heading e^n, c2: =exp(a2) and copy down
d1: heading i(n)*e^n, d2: =b2*c2 and copy down
e1: heading accum, e2: =d2, e3: =e2+d3 and copy down
f1: heading F1(n), f2: =e2/c2 and copy down
g1: heading F2(n), g2: =sum(d$2:d2)/c2 and copy down
F1(n) and F2(n) ate two slightly different ways of calculating your F(n). F2 looks very much like what you say you don't want ("Please don't tell to write for each raw separately and then sum up") -- you may like to explain why you think you don't want that, because (1) 5000 calculations is a trivially small number and (2) I've filled out the above table to n=100 and the recalculation time is not noticeable. You'll notice that the F2(n) doesn't use the clunky "accum" (running total) column.
Can you use SERIESSUM() to accomplish this?
For one-dimensional sums of series, you can use the Excel function
SUMPRODUCT( yourformulahere( ROW(1:100) ))
to generate the sigma from 1 to 100 of your arbitrary f(n).
ROW(1:100) expands to the series {1,2,...100}.
It sounds like you are doing a double summation here, though, with the inner dependent on the outer. This is left as an exercise for the reader. HTH.