define equation

Write your question here.
I'm trying to define two equations(x,y) in c++, the two equations describe how the particle diffuse in magnetic filed
I did not get any syntax error ,I do not know how can I get out put?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Declear Functions2

double XDiffusion (int D,int D11, int x, int s)
 {
    float DeltaX;
    return DeltaX;
 }
 
double YDiffusion (int D,int D11, int x, int s)
 {    
    float DeltaY;
    return DeltaY;
 }


int main()
{

int T =100;
int N = 1000;
float dt = float (T)/ N;
int x = 0; // the angle 
int D = 10;
int DII = 10000;
// define the magnetic filed:
double Bx[2] = {cos(x),-sin(x)};
double By[2]= {sin(x), cos(x)};
// s is random number
srand(time(0));
float   s = (float(rand())/(RAND_MAX)) + 1;// give random number between(0,1)
//the two function   
  float  DeltaX = Bx[0]*sqrt(2*D*dt)*s + Bx[1]*sqrt(2*DII*dt)*s;  
  float  DeltaY = By[0]*sqrt(2*D*dt)*s + By[1]*sqrt(2*DII*dt)*s;

  cout << "the value is: " <<D <<'\n'<< endl ;
  cout << " The value is:" << DeltaY<< '\n'<< endl;
  return 0;
}

Last edited on
I suggest you take a look at the tutorial page on functions:
http://www.cplusplus.com/doc/tutorial/functions/
I've taken a second look at this but I have difficulty understanding the code without anything to refer to to explain the mathematics and physics involved. Do you have the original question, or a reference to a web-page describing the calculations please.

My view is that some of the variables should be considered as initial values, while others are what I would loosely term working-values, that is they are used in the process of calculating the result.

The way I would organise the code is to first assign the appropriate initial values, then call the function or functions to calculate the results. As it is, all the calculation is done inside main() and the functions are left idle, doing nothing.

edit:
Ok, this is my take on how main() might look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    int T =100;
    int N = 1000;
    double dt = double (T)/ N;
    int x = 0; // the angle 
    int D = 10;
    int DII = 10000;
    
    // s is random number
    srand(time(0));
    double s = (double(rand())/(RAND_MAX)); // give random number between(0,1)
    
    // call the two functions   
    double DeltaX = XDiffusion (D, DII, x, s, dt);
    double DeltaY = YDiffusion (D, DII, x, s, dt);

    cout << "The value is: " << DeltaX << "  "<< DeltaY << '\n';
    return 0;
}


Now all of the calculation takes place inside the two functions.
Last edited on
Thank you for your reply Chervil.
The question is how the particles diffuse in magnetic field in two dimension. using these two equation:

DeltaX =cos(x)*sqrt(2*DII*dt)*randomnumber1 - sin(x)*sqrt(2*D*dt)*randomnumber2;
DeltaY = cos(x)*sqrt(2*D*dt)*randomnumber1 + sin(x)*sqrt(2*DII*dt)*randomnumber2

I am trying to follow the way that i have used in python(I wrote it in python, its work with me). I'm a beginner in c++
I defined the magnetic filed as an array:
// define the magnetic filed:

double Bx[2] = {cos(x),-sin(x)}; // x is any angle
double By[2]= {sin(x), cos(x)};

I defined the random number also as an array.

then I multiply(insert) these two array (of the magnetic filed ,and the random number) in the two equation above.
After defined the two equation correctly ,I will plot it.
Last edited on
I think I'm getting closer to understanding.

This was one of my earlier attempts at simply rewriting the code without necessarily understanding what I was doing.

1
2
3
4
5
double XDiffusion (int D, int DII, int x, int s, double dt)
{
    double Bx[2] = {cos(x),-sin(x)};    // define the magnetic field
    return Bx[0]*sqrt(2*D*dt)*s + Bx[1]*sqrt(2*DII*dt)*s;
}


The function YDiffusion() was similar.

Now I see from your description that there should be two random numbers, not one. Those could be passed to the function either as two separate values, or as an array. I don't think they can be calculated inside the function because both the x and y functions would want to use the same pair of random numbers I think.



Thank you very much:
the code now look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
double XDiffusion (int D, int DII, int x, int s, double dt)
   {
    double Bx[2] = {cos(x),-sin(x)};    // define the magnetic field
    return Bx[0]*sqrt(2*DII*dt)*s + Bx[1]*sqrt(2*D*dt)*s;
   }
 
double YDiffusion (int D, int DII, int x, int s, double dt)
   {
    double By[2]= {sin(x), cos(x)};    // define the magnetic field
    return By[0]*sqrt(2*DII*dt)*s + By[1]*sqrt(2*D*dt)*s;
   }


int main()
{

int T =100;
int N = 1000;
double dt = double (T)/ N;
int x = 0; 
int D = 10;
int DII = 10000;


// s is random number
srand(time(0));
float   s = (float(rand())/(RAND_MAX)) + 1;// give random number between(0,1)



  cout << "The value is: " << XDiffusion << "  "<< YDiffusion << '\n';
  return 0;
}



my question is :how can I see the result, when I make cout I did not see any thing ?
May you please tell me which program I have to use to plot my result ?
Last edited on
Wait I don't know if this is the problem but...

XDiffusion is a function containing 4 ints and 1 double data type to pass by, the same with YDiffusion.

So when you are calling it with your cout, shouldn't you be passing by 4 ints and 1 double? How does the functions know the values of d or d11 etc if those variables are local to the int main() but the functions have no way to access them?

How can I access the variables to the two function? what dose local means?
Last edited on
I would make a float like

float resultx,resulty;

resultx = XDiffusion(D,DII,...,...,...) whatever variables you want the function to use
resulty = YDiffusion(D,DII,...,..,...)

and then cout << resultx << resulty<<endl;
It looks like there are two carefully written functions, but you never actually call them.
I can only repeat:
I suggest you take a look at the tutorial page on functions:
http://www.cplusplus.com/doc/tutorial/functions/
I changed as you said,but I am still getting nothing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Call the two functions
double XDiffusion (int D, int DII, int x, int s, double dt)
   {
    double Bx[2] = {cos(x),-sin(x)};    // define the magnetic field
    return Bx[0]*sqrt(2*DII*dt)*s + Bx[1]*sqrt(2*D*dt)*s;
   }
 
double YDiffusion (int D, int DII, int x, int s, double dt)
   {
    double By[2]= {sin(x), cos(x)};    // define the magnetic field
    return By[0]*sqrt(2*DII*dt)*s + By[1]*sqrt(2*D*dt)*s;
   }


int main()
{

int T =100;
int N = 1000;
double dt = double (T)/ N;
int x = 0; 
int D = 10;
int DII = 10000;


// s is random number
srand(time(0));
float   s = (float(rand())/(RAND_MAX)) + 1;// give random number between(0,1)

float resultX,resultY;

resultX = XDiffusion(D,DII,x,s,dt);
resultY = YDiffusion(D,DII,x,s,dt);




  cout << "The result: " << resultX << "  "<< resultY << endl;
  return 0;
}
Chervil: hope you will forgive me, this is the first time that I am using c++ in my life. I am trying to do my best.
The thing is, I can help you to write valid code which will compile and run. But at the moment I'm still not sure of how it should all work (though you did give an explanation - thank you for that).

Earlier, you mentioned there was a program in Python. Maybe if you could post the Python code it might make things clearer, if you don't mind.
Sure, my python code is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
T=100
N=10000
dt=float(T)/N 
t=np.linspace(0,T,N+1)


D= 2
DII=10
i= 0.0

Bx= np.array([np.cos(i), -np.sin(i)])                
By= np.array([np.sin(i), np.cos(i)])                 

n= 1000
seed(2)

plt.figure()
plt.xlabel(r'$\ X$',size=16)
plt.ylabel(r'$\ Y$',size=16)
finalpositions=[]

for number in range(0, 100):
  x=[]
  y=[]
  x.append(0)
  y.append(0)

  for i in range(n):
      s = np.random.normal(0, 1, 2) 
      deltaX= Bx[0]*np.sqrt(2*DII*dt)*s[0] + Bx[1]*np.sqrt(2*D*dt)*s[1]	  
      deltaY= By[0]*np.sqrt(2*DII*dt)*s[0] +By[1]* np.sqrt(2*D*dt)*s[1]
      x.append(x[-1] + deltaX)
      y.append(y[-1] + deltaY)
  finalpositions.append([x[-1], y[-1]])
 
finalpositions = np.array(finalpositions) 
plt.plot(finalpositions[:,0],finalpositions[:,1],'*')
Last edited on
Thanks. It may take some time to digest that...
No problem, take your time
I appreciate your help.

Just to make every thing is clear
in line 22:
I make a loop to plot 100 particles(*), that diffuse from the origin(0,0)
then I plot them .
i=0, is the angle, that I can change it any time.
Last edited on
For if all, the angle x should be a double. Angles in the math library are represented in radians.

You need to add this to the beginning of the program:
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <cmath>
#include <random>

using std::cout;
using std::endl;
using std::cos;
using std::sin;
using std::sqrt;

The #include lines tell the compiler to read the standard iostream, math and random files. These files declare the pieces of the standard library that you use.

What parts of the standard library to you use? The ones that are in the using statements. The real names of sin, cos, sqrt, endl and cout actually have std:: prepended to them. The using statement tells the compiler that you will use these names without the std::. This may seem strange but it's there because the number of names in the standard library has grown so large that there's a real chance that any meaningful name you choose in your program (like map, or list or vector) will conflict with the same name in the standard library.
Topic archived. No new replies allowed.