1. Limited time only! Sign up for a free 30min personal 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!

Homework Help: C++ and Radial Probability Density

  1. Sep 18, 2011 #1
    1. The problem statement, all variables and given/known data

    In a neutral hydrogen atom, the electronic ground state 1s, and excited state 2s, are given by wave functions

    ψ1s = 1/π1/2 1/a3/2 e-r/a
    ψ2s = 1/(32π)1/2 (2-r/a)2 e-r/(2a)

    where "r" is the radial distance to the nucleus, and "a" is the Bohr radius.
    Write a C++ program hatom.cc that calculates the radial probability density,

    Pr(r) = 4πr2ψ*ψ,
    from r=0 to 10 Bohr radii (r=10a) with a stepsize of dr=0.01a. At each radial step, print out three columns to standard output: r (units of Bohr radii), Pr(r) for the 1s state, and Pr(r) for the 2s state.
    To check your output, confirm that your values of Pr are such that the integral of Pr out to 10a gives you something close to unity. (An integral may be crudely approximated using summation of the function at a series of uniformly separated points times the separation (delta x) between adjacent points)

    2. Relevant equations


    3. The attempt at a solution

    I can see where to get started here, but need a little help understanding the problem.

    This part loses me...from r=0 to 10 Bohr radii (r=10a) with a stepsize of dr=0.01a. At each radial step, print out three columns to standard output: r (units of Bohr radii), Pr(r) for the 1s state, and Pr(r) for the 2s state.

    I need a for loop that will calculate r=0 to r=10 with an interval of 0.01a. First I am not sure how to make the loop calculate in steps, but can probably figure that. It's the r=10a part that I don't get. I know from my physics classes that the Bohr radius is given by [itex]a_0 = \frac{\hbar}{m_{e} c \alpha}[/itex]. Does it look like you run loop as 0, 1a, 2a, 3a,.....10a? With a given by my Bohr radius formula?

    I'll probably need a little help with the C++ part as well after I get past this part.
    Last edited: Sep 18, 2011
  2. jcsd
  3. Sep 18, 2011 #2


    Staff: Mentor

    You can set a constant to the value of the Bohr radius, which wikipedia gives as 5.2917721092(17)×10−11 m.

    Here's a loop that I think will do what you want.

    Code (Text):

    const double Bohr_radius = 5.2917721092E-11;

    for (r = 0.0; r <= 10 * Bohr_radius; r += .01*Bohr_radius)
       // Do something
  4. Sep 18, 2011 #3
    Oh nice. That also helps me setting constants. I've seen those on a few examples during my search to solve this problem. Good to actually see how to use it.

    When I am calculating something like these, is it possible just to type it out like you would in Maple or Mathematica? It seems to me that the first column r shouldn't need much more than the loop since there isn't much to calculate there?

    What I was going to try to do was get the first column, r, to work and then move on to my other columns.

    For the first column I tried a couple things for the //do something. I can only get it to output zeros.
    Last edited: Sep 18, 2011
  5. Sep 18, 2011 #4
    Maybe, if you could elaborate as to what you want to be calculated in detail. That is you give mathematical equivalents of what variables will be calculated, I can give it a try in java.
  6. Sep 18, 2011 #5
    Well for my first column I am trying to get the values of r displayed for each of the step levels. At least that is what I am assuming is to be done.

    So 0.00a, 0.01a, 0.02a, 0.03a......9.98a, 9.99a, 10a. Where a is the Bohr Radius.
  7. Sep 18, 2011 #6
    Please elaborate like the following example :

    3 quantities to be calculated : var1 = blah blah
    var2=blah blah
    var3 = mathematical expression

    What are to be output and, before that the formulas.
  8. Sep 19, 2011 #7
    For my first run I am only trying to get var 1 to work. Nothing with the other two yet. Since they are going to be laid out in three separate columns, I was just going to try to get the easier of the three sorted out first.

    So using that for loop above (which I can't get to display anything but a constant stream of 0's) I just need to calculate 0.00a, 0.01a, 0.02a, 0.03a......9.98a, 9.99a, 10a. Where a is the Bohr Radius or 5.2917721092E-11. It seems to me that loop would do the trick, I can't figure out what to put in the //do something field. I've tried r, r*a, etc etc. It's my lack of understanding I guess that I don't know what needs to be under the loop.
  9. Sep 19, 2011 #8


    Staff: Mentor

    If you want to see something displayed, you need to use something to do output. In C++, the cout object and the >> operator are typically used.

    You said earlier that you are getting a stream of 0s, which implies that you know how to do output. Please show us the code you are using, and we can figure out why you're getting 0s instead of what you want.

    When you show us the code here, put a (code) tag at the beginning and a (/code) tag at the end. Use brackets, though [], instead of parentheses.
  10. Sep 20, 2011 #9
    As far as what I have learnt by now :

    (1) For the Probability density function of H atom in 1s : The most probable position of the electron is one Bohr Radius away from the Nucleus, that it unifies to one "1" when the radius to be calculated is equal to Bohr's Radius ( it is a constant and you do not need to calculate that )

    (2) You can either go integrate


    the first integral on the linking page, which requires that you use RIEMANN SUMs


    evaluated form where you can plug in the distance the electron's from the Nucleus and find from zero to r

    For 2s, I need the equation, but the Logic applies

    In your program , You need Loops

    the likes of which are alike to the following java piece of code

    Code (Text):

    for (  double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr;

    And before that declare Bohr in the following Fashion :

    Code (Text):

    double  Bohr=5.2917721092*Math.pow(10,-11.) ;
  11. Sep 20, 2011 #10


    Staff: Mentor

    This for loop will not compile. Post #2 shows one that can be used.
  12. Sep 20, 2011 #11
    Yes, when you leave the blanks between


    blank, but I am not allowed to put the code that fits in between am I ( even if it's in java ) ?
  13. Sep 20, 2011 #12
    I got the zero part figured out finally and also learned a couple things in the process.

    I was defining r as int r - which come to find means integer. Since my value was so small it showed up as zeros. I never let it run long enough to get close to one.

    Now I have it changed to double r which gives me a value!

    So now it's just adding columns (?) and defining two more calculations...at least I think.

    To display the r column I have the following code:

    Code (Text):

    #include <iostream>
    #include <cmath>

    using namespace std;

    int main()
      double r;

      const double Bohr_radius = 5.2917721092E-11;

      for (r = 0.0; r <= 10; r += .01*Bohr_radius)
          cout << r << endl;

      return 0;
    Besides the columns, I think two more cout's along with their assigned values (like the double r) should do it. If I have the one for loop can I apply three different couts that will run off of the one loop?
    Last edited: Sep 20, 2011
  14. Sep 20, 2011 #13


    Staff: Mentor

    That wasn't the problem. It was this line.
    Code (Text):
    for (  double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr;
    If you can't spot what's wrong with this, then you shouldn't be offering programming help.
  15. Sep 20, 2011 #14
    Yes OK I forgot (rather skipped my eye ) to end the paranthesis and omit the ;

    I really do not know why you say like that, Could not it be that my eyesight is poor ??

    And I have the fully working code, btw.
  16. Sep 20, 2011 #15
    for ( double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr)

  17. Sep 20, 2011 #16


    Staff: Mentor

    This was post #2.
    Is your post an improvement over what was already in this post?
  18. Sep 20, 2011 #17
    Yes and No, you are writing in C++, I am writing in Java.,

    Please refer to my Posts #9 and #11

    So I am trying to learn, trying to help.

    OK, you are the Professional but Can't I contribute, too ?
  19. Sep 20, 2011 #18


    Staff: Mentor

    A for loop is pretty much the same in C, C++, Java, and C#.
    Yes, I saw those posts, and commented on the code you had in them.
    And we appreciate members who help others.
    You are certainly welcome to contribute. My point is that the code you provided was essentially a duplicate of the code in post #2, except that it had some pretty obvious errors. I recognize that all of us make mistakes, and that none of us are perfect, but you should take a quick look at what you're about to post, and fix really obvious problems.
  20. Sep 20, 2011 #19
    Oh..what do you guys thing about post #12? :wink:
  21. Sep 20, 2011 #20


    Staff: Mentor

    I think the top line of the for loop should be
    Code (Text):
    for (r = 0.0; r <= 10 * Bohr_radius; r += .01*Bohr_radius)
    You can add two more columns, not by putting in another cout statement, but by adding some more things to display in your one cout statement. Something like this:
    Code (Text):
    cout << r << "\t" << expression_2 << "\t" << expression_3 << endl;
    The "\t" strings are strings that contain a tab character, for spacing.

    expression_2 and expression_3 are the other things that you want to put on the same line. I don't remember you saying what else you wanted to display, so you'll need to figure out what these represent (and you should use your own variables).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook