• Support PF! Buy your school textbooks, materials and every day products Here!

Loop in Mathematica

  • Thread starter Juliane
  • Start date
  • #1
8
0
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
 

Answers and Replies

  • #2
272
0
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).
 
  • #3
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.
 
  • #4
8
0
Thank you so much, now it works.
 
  • #5
alphysicist
Homework Helper
2,238
1
Thank you so much, now it works.
You're welcome! By the way, there is a better place here to post questions about mathematica. From the front page, choose the "computer science" forum, and then choose the "math & science software" subforum.

Or follow this link:

https://www.physicsforums.com/forumdisplay.php?f=189
 
  • #6
8
0
Oh, okay thanks
 

Related Threads on Loop in Mathematica

Replies
28
Views
4K
Replies
9
Views
2K
  • Last Post
Replies
12
Views
6K
  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
5
Views
8K
  • Last Post
Replies
5
Views
4K
  • Last Post
Replies
7
Views
1K
  • Last Post
Replies
4
Views
2K
  • Last Post
Replies
1
Views
838
Top