C++ and Radial Probability Density

Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to calculate the radial probability density for the 1s and 2s states of a hydrogen atom. Participants are exploring how to implement the calculations based on given wave functions and the Bohr radius, focusing on the programming aspects and the mathematical formulation involved.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • Participants discuss the need to calculate the radial probability density Pr(r) using the provided wave functions for the hydrogen atom.
  • There is uncertainty about how to implement the for loop in C++ to iterate from r=0 to r=10 Bohr radii with a step size of 0.01a.
  • Some participants suggest using constants for the Bohr radius and provide examples of how to set up the loop.
  • Concerns are raised about how to display the values of r correctly and the need for output formatting in C++.
  • Participants express confusion about the mathematical expressions needed to calculate Pr(r) and how to structure the output for three columns.
  • There are requests for clarification on the specific calculations required for each of the three output columns.

Areas of Agreement / Disagreement

Participants generally agree on the need to calculate the radial probability density and the structure of the program, but there is no consensus on the specific implementation details or the mathematical expressions needed for the calculations.

Contextual Notes

Some participants mention the Bohr radius and its formula, but there is uncertainty about how to apply it in the context of the program. Additionally, there are unresolved issues regarding the output of the loop and the calculations for Pr(r).

Who May Find This Useful

Students and programmers interested in computational physics, particularly those working on problems related to quantum mechanics and programming in C++.

  • #31
Okay, I think my mistake lies in the exponent.

Here is my new code:

Code:
#include <iostream>
#include <cmath>


using namespace std;

int main()
{
  float r;

  const double Bohr_radius = 5.2917721092E-11;
  const double Pi = 3.14159256;

  for (r = 0.0; r <= 10*Bohr_radius; r += .01*Bohr_radius)
    {
      cout << r << "\t" << 4*Pi*r*r*(pow((1/(sqrt(Pi)))*(1/(pow(Bohr_radius,3/2)))*(exp*(-r/Bohr_radius))),2) <<  endl;
    }

  return 0;
}

However, it still doesn't work. :confused:
 
Physics news on Phys.org
  • #32
You've got two problems. e^{foo} is exp(foo). You also have a pow expression with no power (check your parentheses... I'm not sure what editor you're using to write this code, but most have some type of parentheses match function that is really useful for debugging).

You'd make your life much, much easier if you simplified the expression 4\pi r^2(\frac{1}{\pi^{1/2}a^{3/2}}e^{-r/a})^2 so that you weren't using that outer pow expression (and at least one constant drops out). Also, you have a bunch of parentheses that you don't need, which makes it harder to figure out where the parentheses are mismatched.
 
  • #33
stallionx said:
Sir, isn't Pi=acos(-1.0) ?

Or

Pi=2.0*acos(0.0) ?
Yes, either of these would work. Mine was incorrect and would produce pi/2, not pi.
stallionx said:
Can't we, here, multiply x*sqrt(x) ?
Sure, that would work.
 
Last edited:
  • #34
erok81 said:
Okay, I think my mistake lies in the exponent.

Here is my new code:

Code:
#include <iostream>
#include <cmath>


using namespace std;

int main()
{
  float r;

  const double Bohr_radius = 5.2917721092E-11;
  const double Pi = 3.14159256;

  for (r = 0.0; r <= 10*Bohr_radius; r += .01*Bohr_radius)
    {
      cout << r << "\t" << 4*Pi*r*r*(pow((1/(sqrt(Pi)))*(1/(pow(Bohr_radius,3/2)))*(exp*(-r/Bohr_radius))),2) <<  endl;
    }

  return 0;
}

However, it still doesn't work. :confused:

You really should calculate the values first before putting them into the pow function.
Your problem with the pow function is that you are thinking that 3/2 = 1.5. That's not true. 3/2 = 1, an int value.
C and C++ have two kinds of division : integer and floating point. If both operands of a division expression are integral types (such as int, long, char, signed int, etc.), the division that is performed is integer division, and the result will be that same type.

If at least one of the operands is a floating point type, then floating point division is performed.

So, 3/2 = 1 (an int)
but 3.0/2 = 1.5 (a double)

I would advise using 1.5 as the exponent in pow.
 
  • #35
ladyada said:
You've got two problems. e^{foo} is exp(foo). You also have a pow expression with no power (check your parentheses... I'm not sure what editor you're using to write this code, but most have some type of parentheses match function that is really useful for debugging).

You'd make your life much, much easier if you simplified the expression 4\pi r^2(\frac{1}{\pi^{1/2}a^{3/2}}e^{-r/a})^2 so that you weren't using that outer pow expression (and at least one constant drops out). Also, you have a bunch of parentheses that you don't need, which makes it harder to figure out where the parentheses are mismatched.

Ah got it. The exp was throwing me off because I had no idea how to use it. I think I tried it both ways and kept getting the errors, so I changed it back. I'll fix it.

I'm using emacs through PuTTY. It seems to try to match them but I ended up with about eight of them at end which I know wasn't right. I think my last code section has them right. I may not have them right in the code since it was late last night when I cleared those up.

I'll also take a look at simplifying it. I agree it is a mess right now.


Mark44 said:
You really should calculate the values first before putting them into the pow function.
Your problem with the pow function is that you are thinking that 3/2 = 1.5. That's not true. 3/2 = 1, an int value.
C and C++ have two kinds of division : integer and floating point. If both operands of a division expression are integral types (such as int, long, char, signed int, etc.), the division that is performed is integer division, and the result will be that same type.

If at least one of the operands is a floating point type, then floating point division is performed.

So, 3/2 = 1 (an int)
but 3.0/2 = 1.5 (a double)

I would advise using 1.5 as the exponent in pow.

I actually has 1.5 in the beginning. But it looked off, so I switched it to 3/2 just in case. I'll go back and change that as well. The 3/2=1 was my original problem when I was getting all zeros. Thanks for the description though, that helped with the 3.0/2. Makes more sense now.

Thanks again for the help, both of you. It's a little depressing that we are at three pages now and I can't get such a simple program to work. It's only what, five lines? :redface:
 
  • #36
That's the way it usually goes when you start to program. People sometimes have the misconception that computers are "smarter" than they actually are. When you write code, you are in essence telling the computer exactly what to do, and that's what it does, even that's not what you really meant.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K