Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Plotting and Integrating difficult functions/Maple or mathematica

  1. Aug 9, 2011 #1
    Hi Everybody,

    So basically I spent the summer working on some optics related stuff and now they want me to present my research work. This means I'm going to have to do numerical simulations for diffraction of Laguerre-Gaussian beam modes. In a word, the integrals are ridiculously long and complicated if they're integrable. Often my computer will be running a long time just to add an integral sign to my expression.

    Anyway the question is: When trying to calculations on a laptop (Macbook Pro, 13", 2.26 ghz processor and 4gb of RAM), which software is better to use: Mathematica or Maple. I'm more acquainted with mathematica but the problem with it is it'll take forever trying to calculate the integral before just putting an integral sign in front of my expression and then trying to plot the absolute value of the function (bc i want the intensity distribution) takes FOREVER. I've never used maple before but I can probably learn the basics quickly.

    So Maple or Mathematica or maybe just not use my laptop.

  2. jcsd
  3. Aug 10, 2011 #2


    User Avatar
    Gold Member

    Do you have an integral for us to try? Usually mathematica takes a long time when the integral is not easily solved in a closed form. BUT computers are dumb, and something as simple as declaring certain variables to be real will sometimes make the integral instant, as the software no longer has to look at all possibilities.

    Give me an example, online or otherwise that your mathematica can't solve and I'll see if theres something that I can whip up to help.
  4. Aug 10, 2011 #3
    Do you need a closed form result or is it just to compare against your data?
    If it's the latter, you should probably use NIntegrate.

    As Hepth, post an example of your integral (along with some reasonable numerical values for any parameters), and someone might be able to give more specific advice.
  5. Aug 10, 2011 #4
    Ok so I defined a function U[x,y,m,l] where m and l are terms in the associated Laguerre polynomial (m being the bottom index, l being the top). I've kept m=0 and I've been varying l.

    U[x0,y0,0,3]=(Sqrt[2]*r0/w0)^3*LaguerreL[m, l, 2*r0^2/w0^2]*Exp[-r0^2/w0^2]*

    where r0^2=x^2+y^2 and theta0=ArcTan[x/y]
    w0 is the beam waist and is approximately 2.4*10^-4 meters.

    So U is the electric field distribution and the entire diffraction integral is (I'm putting my screen 10^4 meters away. That's not an important number, just wanted it in the far field. Probably overshot it):

    U1[x0, y0, 4, 0]*I*k/(2*Pi*10^4)*Exp[-I*k*10^4]*
    Exp[-I*k/(2*10^4)*((x0^2 + y0^2) - 2 (x*x0 + y*y0) + (x^2 + y^2))]


    (-7.56023*10^15 - 1.72943*10^17 I) E^(-1.65486*10^7 (x0^2 + y0^2) - (0. +
    496.459 I) (x^2 + x0^2 + y^2 + y0^2 - 2 (x x0 + y y0)) +
    4 I ArcTan[x0/y0]) (x0^2 + y0^2)^2

    What I was trying to do was a double integral over x0 and y0 taking each value from -.0003 to .0003 (that value isnt too important i was also using .0004 &.0005)

    I haven't declared my variables as real (not sure how to do that) and I don't want my integral in a closed form, I just want to plot the data and compare to my experimental values. I haven't used NIntegrate before, I'll try that out.
  6. Aug 10, 2011 #5
    Another thing is that I want to plot the results when I'm done because I need to compare my intensity distribution with the theoretical results. I've been using Plot3D so far.
  7. Aug 10, 2011 #6


    User Avatar
    Gold Member

    Yeah, try using NIntegrate. Basically make a function like:

    F[xx_,yy_] := NIntegrate[(...),{x0,-xx,xx},{y,-yy,yy}]

    Then Plot3D[F[0.003,0.003],{x,-1,1},{y,-1,1}]

    The " := " instead of a " = " means for it to run the function when it is called, rather than before.
  8. Aug 10, 2011 #7
    It's better to use

    F[xx_?NumericQ,yy_?NumericQ] := NIntegrate[(...),{x0,-xx,xx},{y,-yy,yy}]

    so that NInntegrate won't be called for symbolic arguments.

    If the NIntegrate is really expensive and you're going to do lots of plots, it might be worth caching the results:

    F[xx_?NumericQ,yy_?NumericQ] := F[xx,yy] = NIntegrate[(...),{x0,-xx,xx},{y,-yy,yy}]
  9. Aug 10, 2011 #8
    Thanks for the advice. I'm using NIntegrate with the ContourPlot function, but I haven't defined any new functions yet because I still need to manipulate what's under the integral sign.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Plotting and Integrating difficult functions/Maple or mathematica