C++ and Radial Probability Density

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
35 replies · 7K views
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
You've got two problems. [itex]e^{foo}[/itex] 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 [itex]4\pi r^2(\frac{1}{\pi^{1/2}a^{3/2}}e^{-r/a})^2[/itex] 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.
 
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:
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.
 
ladyada said:
You've got two problems. [itex]e^{foo}[/itex] 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 [itex]4\pi r^2(\frac{1}{\pi^{1/2}a^{3/2}}e^{-r/a})^2[/itex] 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:
 
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.