C++ and Radial Probability Density

In summary: I am understanding correctly, you want to print out the values of r, Pr(r) for the 1s state, and Pr(r) for the 2s state at each step in the loop? In that case, you could do something like this:const double Bohr_radius = 5.2917721092E-11;for (r = 0.0; r <= 10 * Bohr_radius; r += .01*Bohr_radius){ cout << r << " " << Pr(r) << " " << Pr(r) << endl;}This will print out the value of r, followed by the values of Pr for the 1s and 2s states at that value of
  • #1
erok81
464
0

Homework Statement



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)

Homework Equations



None.

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:
Physics news on Phys.org
  • #2
erok81 said:

Homework Statement



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)

Homework Equations



None.

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.

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:
const double Bohr_radius = 5.2917721092E-11;

for (r = 0.0; r <= 10 * Bohr_radius; r += .01*Bohr_radius)
{
   // Do something
}
 
  • #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:
  • #4
erok81 said:

Homework Statement



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)

Homework Equations



None.

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.

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.
 
  • #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.
 
  • #6
erok81 said:
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.

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.
 
  • #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.
 
  • #8
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.
 
  • #9
erok81 said:

Homework Statement



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)

Homework Equations



None.

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.

As far as what I have learned 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

http://hyperphysics.phy-astr.gsu.edu/hbase/quantum/hydrng.html#c1

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

OR

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:
for (  double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr;
{


}

And before that declare Bohr in the following Fashion :


Code:
double  Bohr=5.2917721092*Math.pow(10,-11.) ;
 
  • #10
stallionx said:
the likes of which are alike to the following java piece of code

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


}

This for loop will not compile. Post #2 shows one that can be used.
 
  • #11
Mark44 said:
This for loop will not compile. Post #2 shows one that can be used.

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 ) ?
 
  • #12
Mark44 said:
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.

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:
#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:
  • #13
Mark44 said:
This for loop will not compile. Post #2 shows one that can be used.

stallionx said:
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 ) ?
That wasn't the problem. It was this line.
Code:
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.
 
  • #14
Mark44 said:
That wasn't the problem. It was this line.
Code:
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.

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.
 
  • #15
for ( double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr)
{


}
 
  • #16
This was post #2.
Mark44 said:
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:
const double Bohr_radius = 5.2917721092E-11;

for (r = 0.0; r <= 10 * Bohr_radius; r += .01*Bohr_radius)
{
   // Do something
}
Is your post an improvement over what was already in this post?
stallionx said:
for ( double i=0.0;i<=Bohr*10.0;i+=0.01*Bohr)
{


}
 
  • #17
Mark44 said:
This was post #2.
Is your post an improvement over what was already in this post?

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 ?
 
  • #18
stallionx said:
Yes and No, you are writing in C++, I am writing in Java.,
A for loop is pretty much the same in C, C++, Java, and C#.
stallionx said:
Please refer to my Posts #9 and #11
Yes, I saw those posts, and commented on the code you had in them.
stallionx said:
So I am trying to learn, trying to help.
And we appreciate members who help others.
stallionx said:
OK, you are the Professional but Can't I contribute, too ?
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.
 
  • #19
Oh..what do you guys thing about post #12? :wink:
 
  • #20
erok81 said:
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:
#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?

I think the top line of the for loop should be
Code:
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:
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).
 
  • #21
This is what I am finding...this probably isn't the best/cleanest method, but I think it should work? rather than creating extra variables, I was just going to type it all out on one line. Since neither expression has imaginary numbers, I'm just going to square it.

Original problem:
ψ1s = 1/π1/2 1/a3/2 e-r/a
ψ2s = 1/(32π)1/2 1/a3/2(2-r/a)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ψ*ψ,

For r we have that taken care of.

For my expression 2 (I didn't type this out in code. I'll to figure that out)

4πr2(1/π1/2 1/a3/2 e-r/a)2

And similarly for expression 3

4πr2(1/(32π)1/2 1/a3/2(2-r/a)e-r/(2a))2

Where a will be replaced by my Bohr_radius constant.

Will that work, or do I need to something other than that?

And from what I can gather say I wanted to write x2 in C++. That would go as pow(x,2)? I tried that in my program as a test, but it didn't work. Is there something I need to call out in the top?
 
Last edited:
  • #22
Okay, yeah. That isn't even close. Here is what I tried. I haven't done expression 3 since I wanted to get through expression 2 first. This pumped out almost half a page of errors.

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

using namespace std;

int main()
{
  float r;

  const double Bohr_radius = 5.2917721092E-11;

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

  return 0;
}

One of the errors mentioned I need to call pow(double). So I know I must be missing a few things. I think I have all of my parenthesis in place okay.
 
  • #23
I don't see why it wouldn't work.

To use pow(), you need to include the right header file, math.h.
#include <math.h>

If all you're doing is squaring a number, it's simpler just to multiply the number by itself, as x * x.

For square roots, there is the sqrt function. You can get pi this way, using the acos function (arccosine):
pi = acos(0.0);
 
  • #24
1. Calculate pi once, and store it in a variable.
2. Calculate your expressions and store them in variables. That way your cout statements will be much shorter.
3. Operations (such as multiplication) that are implied in math expressions are NOT OK in programming. I can write 2x or 3(a + b) in a math context, but I have to write 2*x and 3 * (a + b) in most programming languages.
 
  • #25
Maybe my error is something else then. I agree. Where I had r2 I just typed out r*r like you suggested. However I have quite a few with fractional powers.

Good point on the sqrt's. I'll replace those and see how it goes.

I was also going to declare pi as a constant at the top instead of continually typing it in. Although your method looks pretty easy as well.

I swap some stuff around and see how it goes. Do you know if there is something like sqrt for my 3/2's power?

You beat me to posting. This post applies to your first post.
 
  • #26
For the 3/2 power, use pow. There's no special function (like sqrt) for this power.
 
  • #27
Mark44 said:
1. Calculate pi once, and store it in a variable.
2. Calculate your expressions and store them in variables. That way your cout statements will be much shorter.
3. Operations (such as multiplication) that are implied in math expressions are NOT OK in programming. I can write 2x or 3(a + b) in a math context, but I have to write 2*x and 3 * (a + b) in most programming languages.

Okay, I missed that one. You'd think with all of my Maple work I'd have those down. Guess not.

I haven't shortened my expressions yet. I want to get it to work before I start messing with it. I definitely will though.

These are the errors I am getting.

hatom.cc: In function 'int main()':
hatom.cc:15: error: no matching function for call to 'pow(double)'
/usr/include/iso/math_iso.h:63: note: candidates are: double pow(double, double)

And this is my code.

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

using namespace std;

int main()
{
  float r;

  const double Bohr_radius = 5.2917721092E-11;

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

  return 0;
}

I don't think I am missing any * and my parenthesis should be straight.
 
  • #28
Mark44 said:
I don't see why it wouldn't work.

To use pow(), you need to include the right header file, math.h.
#include <math.h>

If all you're doing is squaring a number, it's simpler just to multiply the number by itself, as x * x.

For square roots, there is the sqrt function. You can get pi this way, using the acos function (arccosine):
pi = acos(0.0);


Sir, isn't Pi=acos(-1.0) ?

Or

Pi=2.0*acos(0.0) ?

Mark44 said:
For the 3/2 power, use pow. There's no special function (like sqrt) for this power.

Can't we, here, multiply x*sqrt(x) ?
 
Last edited:
  • #29
Okay, I cleaned it up a little bit and have this...

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*(pow(-r/Bohr_radius))),2) <<  endl;
    }

  return 0;
}

Now...my errors are looking better. Now I only have the following. But I can't see where they come in.

hatom.cc: In function 'int main()':
hatom.cc:14: error: expected ',' or ';' before 'for'
hatom.cc:14: error: expected `;' before ')' token
 
Last edited:
  • #30
For some reason I am getting these errors. The one above was a fluke.

prog.cpp: In function ‘int main()’:
prog.cpp:18: error: no matching function for call to ‘pow(double)’
/usr/include/bits/mathcalls.h:154: note: candidates are: double pow(double, double)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:380: note: long double std::pow(long double, int)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:376: note: float std::pow(float, int)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:372: note: double std::pow(double, int)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:367: note: long double std::pow(long double, long double)
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/cmath:363: note: float std::pow(float, float)
prog.cpp:18: error: expected `)' before ‘;’ token

I have zero idea what that stuff is. :rofl:
 
  • #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:
 
  • #32
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.
 
  • #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. [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:
 
<h2>1. What is C++ and how is it related to Radial Probability Density?</h2><p>C++ is a high-level programming language commonly used for developing software applications. It is often used in scientific and engineering fields due to its performance and efficiency. Radial Probability Density is a mathematical concept used in quantum mechanics to describe the likelihood of finding a particle at a certain distance from the nucleus. C++ can be used to write programs that calculate and analyze radial probability density functions.</p><h2>2. How is Radial Probability Density represented in C++?</h2><p>Radial Probability Density is typically represented as a function in C++. The function takes in parameters such as the distance from the nucleus and the quantum numbers to calculate the probability density at that specific distance. The function can then be used in a program to plot the radial probability density curve or to calculate other related values.</p><h2>3. Can C++ be used to visualize Radial Probability Density?</h2><p>Yes, C++ can be used to create visual representations of Radial Probability Density. With the help of libraries such as OpenGL or OpenCV, C++ programs can generate 3D plots or animations of the radial probability density function. This can help in understanding the concept better and in visualizing the behavior of particles in different energy states.</p><h2>4. Are there any limitations of using C++ for Radial Probability Density calculations?</h2><p>One limitation of using C++ for Radial Probability Density calculations is that it requires a good understanding of both the language and the mathematical concepts involved. Writing efficient and accurate programs for complex calculations can be challenging and time-consuming. Additionally, C++ may not be the best choice for quick prototyping or for handling large volumes of data.</p><h2>5. How can C++ and Radial Probability Density be applied in real-world scenarios?</h2><p>C++ and Radial Probability Density are commonly used in scientific research and engineering fields. They can be applied in various areas such as materials science, chemistry, and physics to study the behavior of particles and atoms. For example, they can be used to analyze the electron distribution in different molecules or to predict the properties of new materials based on their atomic structure.</p>

1. What is C++ and how is it related to Radial Probability Density?

C++ is a high-level programming language commonly used for developing software applications. It is often used in scientific and engineering fields due to its performance and efficiency. Radial Probability Density is a mathematical concept used in quantum mechanics to describe the likelihood of finding a particle at a certain distance from the nucleus. C++ can be used to write programs that calculate and analyze radial probability density functions.

2. How is Radial Probability Density represented in C++?

Radial Probability Density is typically represented as a function in C++. The function takes in parameters such as the distance from the nucleus and the quantum numbers to calculate the probability density at that specific distance. The function can then be used in a program to plot the radial probability density curve or to calculate other related values.

3. Can C++ be used to visualize Radial Probability Density?

Yes, C++ can be used to create visual representations of Radial Probability Density. With the help of libraries such as OpenGL or OpenCV, C++ programs can generate 3D plots or animations of the radial probability density function. This can help in understanding the concept better and in visualizing the behavior of particles in different energy states.

4. Are there any limitations of using C++ for Radial Probability Density calculations?

One limitation of using C++ for Radial Probability Density calculations is that it requires a good understanding of both the language and the mathematical concepts involved. Writing efficient and accurate programs for complex calculations can be challenging and time-consuming. Additionally, C++ may not be the best choice for quick prototyping or for handling large volumes of data.

5. How can C++ and Radial Probability Density be applied in real-world scenarios?

C++ and Radial Probability Density are commonly used in scientific research and engineering fields. They can be applied in various areas such as materials science, chemistry, and physics to study the behavior of particles and atoms. For example, they can be used to analyze the electron distribution in different molecules or to predict the properties of new materials based on their atomic structure.

Similar threads

  • Quantum Physics
Replies
2
Views
1K
  • Quantum Physics
Replies
3
Views
2K
Replies
1
Views
587
  • Atomic and Condensed Matter
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
12
Views
3K
  • Quantum Physics
Replies
8
Views
4K
Replies
1
Views
2K
  • Calculus and Beyond Homework Help
Replies
6
Views
2K
Back
Top