# If statement inside a for loop to find a function?

I am trying to find the value of two functions with two variables by using for loop. Then I want them to satisfy a condition and if that holds then I want to write only those sets of the variables for which the rate just becomes less than exp and then go to the next value. I have tried like this but didn't work. It is printing all those sets for which rate < exp. How can I do that?

rate= 0.5 (T^5/rt^4);
exp=10^(-18) T^2;
For[rt=10^3,rt <= 10^5,rt=rt+10
For[T=1,T <= 10^3,T=T+10
Evaluate[exp];
Evaluate[rate];
If[rate < exp, Print[{rt, T}]]
]
]

• You are not providing complete code to run what you show. what is interactionrate and what is Hubble? – Nasser Apr 26 at 6:02
• @Nasser I have corrected the question. Thanks. – D.Nanda Apr 26 at 6:14
• Evaluate doesn't do what you think it does. You almost never need Evaluate. – Roman Apr 26 at 8:02

## 2 Answers

Something like this?

rate = 1/2 (T^5/rt^4);
exp = 10^(-18) T^2;

Assuming[T > 0 && rt > 0,
Reduce[rate < exp, {T, rt}] // FullSimplify]
(*    rt > 10000 2^(1/4) Sqrt[5] T^(3/4)    *)

% // N
(*    rt > 26591.5 T^(3/4)    *)


Using the numbers you gave, it will take forever to finish. (it is also using For loop, which is slow).

I fixed few things to make it run, and changed couple of numbers to make it finish quickly.

It is not recommended to use For in Mathematica. But here is your For version which now works. Using a functional approach will make it faster. May be someone can make a functional version of this if they like.

ClearAll[rate, exp, T, rt];
rate[T_, rt_] := 0.5 (T^5/rt^4);
exp[T_] := 10^(-1) T^2;  (*10^(-18) T^2; makes it grow too slow*)
First@Last@Reap@For[rt = 10^3, rt <= 10^4, rt = rt + 10;
For[T = 1, T <=10, (*10^3, makes it take very long time*)
T = T + 10 exp[T];
If[rate[T, rt] < exp[T],
Sow[{rt, T}]
]
]
]