1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Loop in Mathematica

  1. Feb 27, 2009 #1
    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
     
  2. jcsd
  3. Mar 1, 2009 #2
    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).
     
  4. Mar 2, 2009 #3

    alphysicist

    User Avatar
    Homework Helper

    If you want to keep that form of your program, you can wrap it in a Table expression like this:

    Code (Text):

    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.
     
  5. Mar 2, 2009 #4
    Thank you so much, now it works.
     
  6. Mar 2, 2009 #5

    alphysicist

    User Avatar
    Homework Helper

    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
     
  7. Mar 2, 2009 #6
    Oh, okay thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Loop in Mathematica
  1. Loop the loop (Replies: 1)

  2. Loop the Loop (Replies: 10)

  3. Loop the loop (Replies: 3)

  4. Loop a loop (Replies: 4)

Loading...