IF function: SQRT equation as value if true - excel

I have data for re-sightings of individuals in terms of their X (column G) and Y (column H) co-ordinates and the time (day) recorded in column B.
I have several thousand data entries and want to manipulate an equation so I can autofill an entire column to plot.
Where time = 0, that is the release point and I want the distance moved column to remain empty. So by using the IF function, I want to look at daily movement by calculating the Euclidean distance with a simple pythagoras equation, the SQRT function.
The equation I have written is as follows:
=IF(B2 > 0, "=SQRT(((G2-H2)^2)+((G3-H3)^2))", " ")
Where t=0, the cells remain empty as I wished. However, instead of providing me with a numerical value according to the equation, it just writes the equation exactly as it appears. When removing the inverted commas I get TRUE, so that's not it.
What am I doing wrong?

Using quotation marks as you did around "=SQRT(((G2-H2)^2)+((G3-H3)^2))" means just that, to have the result be the string between the quotation marks. What you want is to drop the quotation marks and the =:
=IF(B2 > 0, SQRT(((G2-H2)^2)+((G3-H3)^2)), "")
However, I think you got your formula wrong as you are mixing X and Y. Should it not be:
SQRT((G3-G2)^2 + (H3-H2)^2))
Also, you might be able to do away with the IF altogether by just starting the formula one cell down.

Related

Provide a single Excel Formula for calculating Binomial Coefficients (N,K) in Excel with positive or negative N

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).

Reverse Match Search in Excel

I need to do a match/index search in reverse right to left
each cell with have an x i need do go from right to left find what column the x is in and report the position so can then go to the top of that column and pull that data. I basically need to find out what column is the last X in.
A B C D E F G H I J
State 27-Aug 28-Aug 29-Aug 30-Aug 31-Aug 1-Sep 2-Sep 3-Sep 4-Sep
VI X X X X X X
in above example 3 rows 10 columns if i want to see that the last X is in Column G(7) then i use the index to go to that column(7), row A to 1-sep as the answer.
This will find the last cell with a value, regardless of value:
=INDEX($A$1:$J$1,MATCH("zzz",A2:J2))
If you want to find the last X, regardless of what is or is not in any of the other cells, then use this formula:
=INDEX($A$1:$J$1,AGGREGATE(14,6,COLUMN(A2:J2)/(A2:J2="X"),1))
One note: this is an array type formula and will be slower than the prior formula. If you only have one it will not make a difference. If you have hundreds you will see a difference.
But if you have other text strings after the X and you want the X then it is the way to go.
try out
=INDEX(1:1,1,COUNTIF(2:2,"X")-1)
Assuming there are no gaps within the stream of X's
=MATCH(2,IF(A2:J2="x",1,FALSE))
This is an array formula.
This will give you last position of x. Then, like you indicated, put the result inside INDEX function and you should be good. I like this option because i can put any condition inside the if statement, like >0.

Difference between 2 values in a row in excel formula

How to get this in excel
excel table
F column is the result column
For the following answer I am going to assume you only ever have two numbers in any row, but they can be in any cell along the row and they are always greater than 0.
If you just wish to find the difference between the two numbers without worrying about which number is bigger, a simple equation using maximum and minimum can be used, eg in Cell F1 you would have
=MAX(A1:E1)-MIN(A1:E1)
However, from your example, it seems more likely that you want to know the difference between the first number and the second number.
The difficulty here, is that the cells in columns B, C and D could contain either the first number, the second number, or no number! The solution is to use the following equation in Cell F1
=(MAX(A1:E1)-MIN(A1:E1))*IF(MAX(A1:E1)=INDEX(A1:E1,MATCH(0,A1:E1,-1)),-1,1)
This formula works as follows:
We still start off with the simple difference between the max and min, and then this is multiplied by 1 or -1 depending on which way around the numbers are.
MATCH(0,A1:E1,-1)
This part of the equation looks along the row for a 0, and assumes they are in descending order, so it will return the position of the second number.
This is then inserted into the INDEX function and checked to see if it is the same as the maximum number and the IF function returns either -1 or 1 as required.
Paste this formula on F1, then copy to F2 and F3
=INDEX(A1:E1,MATCH(TRUE,INDEX(A1:E1<>"",),0)) - LOOKUP(9.99E+307,A1:E1)

formula for comparing columns with tolerance in Excel

I'm saerching for a formula comparing two columns f and g with h and i taking a tolerance into account. I have coordinates f and g and coordinates h and i and I want to know how often f equals or is simlar to h and g to i here with a tolerance of 2. But both criteria Need to be met for the Count.
I've tried things like this without succes:
=SUMPRODUCT((F:F=H:H)(G:G=I:I)(F:I>0))/4
=SUMMPRODUCT(((ABS(F:F)-(H:H)<=2))(ABS((G:G)-(I:I)<=2))(F:I>0))
COUNTIF didn't work at all.
With some random data I've made up this is working: assume F1:I6 are your coordinate pairs.
={SUM(IF((ABS(F1:F6-H1:H6)<=4)*(ABS(G1:G6-I1:I6)<=4);1;0))}
Enter as a matrix formula (Ctrl-Shift-Enter). The 4 stands for a tolerance of +- 2, adjust for your needs.
If you really have to cope with cells containing text within that range you need to test the cell first before subtracting:
{=SUM(IF((ABS(IF(ISTEXT(F1:F6);-9999;F1:F6)-IF(ISTEXT(H1:H6);-8888;H1:H6))<=4)*(ABS(IF(ISTEXT(G1:G6);-9999;G1:G6)-IF(ISTEXT(I1:I6);-8888;I1:I6))<=4);1;0))}
This looks ugly but it works. The formula substitutes a cell with text with a token value of -9999 or -8888. These values should never occur in the real data. I used 2 distinct values to cover the case where only 1 or both columns contain text. The difference of the values needs to be greater than the tolerance.

Can I use a built-in Excel solver to solve this equation somehow? If not, how would you go about it?

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.

Resources