Loop in Mathematica

  • Thread starter Juliane
  • Start date
Hi, can anyone help me?

I have the following expression:

NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] t^2 BesselI[0, t/
i^4] (BesselI[-1, t/i^4] + BesselI[1, t/i^4]), {t, 0,
1}]/(2 NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] + BesselI[0, t/i^4]^2, {t, 0, 1}])

For (u,i)=(0,4) it gives 0.000244141. What I wont is to find the pairs (u,i) which also give this value.
Because of the Besselfunktions it must be done numerically.

I have written the following program in mathematica:

Catch[Do[If[
NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] t^2 BesselI[0, t/
i^4] (BesselI[-1, t/i^4] + BesselI[1, t/i^4]), {t, 0, 1}]/(
2 NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] + BesselI[0, t/i^4]^2, {t, 0, 1}]) <
0.000244141, Throw[{u, i}]], {u, 0, 4}, {i, 3, 4, 0.001}]]

But it stops as soon as it finds values for (u,i) which fullfills the inequality. How do I make the program go on for u=2,3, 4...?

I think that maybe I can use Table to make this work, but I don´t know how to do it.
Please help me
Juliane
 
If this is anything like Matlab then you need to use a for loop. An if statement will terminate as soon as the if condition is satisfied, whereas a for loop will continue to evaluate until told to stop by the user.

Try searching through the Mathematica help browser to see if you can do a for statement (it will have something like a for loop, though it may under a different name).
 

alphysicist

Homework Helper
2,238
1
I have written the following program in mathematica:

Catch[Do[If[
NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] t^2 BesselI[0, t/
i^4] (BesselI[-1, t/i^4] + BesselI[1, t/i^4]), {t, 0, 1}]/(
2 NIntegrate[
Exp[-((2 t^2 u^2)/i^4)] + BesselI[0, t/i^4]^2, {t, 0, 1}]) <
0.000244141, Throw[{u, i}]], {u, 0, 4}, {i, 3, 4, 0.001}]]

But it stops as soon as it finds values for (u,i) which fullfills the inequality. How do I make the program go on for u=2,3, 4...?

I think that maybe I can use Table to make this work, but I don´t know how to do it.
Please help me
Juliane
If you want to keep that form of your program, you can wrap it in a Table expression like this:

Code:
Table[
Catch[Do[If[
   NIntegrate[
     Exp[-((2 t^2 u^2)/i^4)] t^2 BesselI[0, t/
       i^4] (BesselI[-1, t/i^4] + BesselI[1, t/i^4]), {t, 0, 1}]/(
    2 NIntegrate[
      Exp[-((2 t^2 u^2)/i^4)] + BesselI[0, t/i^4]^2, {t, 0, 1}]) < 
    0.000244141, Throw[{u, i}]], {i, 3, 4, 0.001}]]  ,{u,1,4}]
and it will find values for u=1,2,3,4.
 
Thank you so much, now it works.
 

alphysicist

Homework Helper
2,238
1
Oh, okay thanks
 

Want to reply to this thread?

"Loop in Mathematica" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving

Top Threads

Top