Not sure if this is the right place: Runge Kutta in C for electron trajectory

In summary, the Runge Kutta method is a numerical algorithm used to solve differential equations, making it a useful tool for studying the motion of particles such as electrons. It works by breaking down complex equations into smaller steps and approximating the solution at each point along the trajectory. It can be used for studying the motion of any type of particle, but may not be the most efficient method for certain types of equations. To implement it in C for studying electron trajectory, one can find online resources and have a strong understanding of differential equations and numerical methods.
  • #1
QueenB
1
0

Homework Statement


Code to do a 4th order Runge-Kutta to get trajectories for electron paths.
When trying to substitute conditions into my program, it returns rubbish.

Homework Equations



Basically I want to change my lower limit to -0.003, my upper limit to 0.007.
Initial conditions:
y(x=-0.003)=a
dy/dx|(x=-0.003)=0

My original equation is
d^2y/dx^2 + ((e[B0exp(-x^2/0.000001)])/(8mV))
where B0 is 1, 5 or 15

The Attempt at a Solution



Code:
#include <stdio.h>
#include <math.h>

#define N 2			/* number of first order equations */
#define dist 0.1		/* stepsize in t*/
#define MAX 10		/* max for t */
#define charge 1.60217646e-19	/* elementary charge*/
#define mass 9.1093897E–31	/* rest mass of electron */

FILE *output;			/* internal filename */

main(){
	double t, y[N];
	int j;
 
	void runge4(double x, double y[], double step);	/* Runge-Kutta function */

	double f(double x, double y[], int i);		/* function for derivatives */

	output=fopen("traject.dat", "w");			/* external filename */

	y[0]=0.001;					/* initial position */
	y[1]=0;							/* initial velocity */
	fprintf(output, "-3\t%f\n", y[0]);
 
	for (j=1; j*dist<=MAX ;j++)			/* time loop */
	{
   t=(j*dist)-3;
   runge4(t, y, dist);

   fprintf(output, "%f\t%f\n", t, y[0]);
	}

	fclose(output);
}

void runge4(double x, double y[], double step){
	double h=step/2.0,			/* the midpoint */
	t1[N], t2[N], t3[N],		/* temporary storage arrays */
	k1[N], k2[N], k3[N],k4[N];	/* for Runge-Kutta */
	int i;
 
	for (i=0;i<N;i++) t1[i]=y[i]+0.5*(k1[i]=step*f(x, y, i));
	for (i=0;i<N;i++) t2[i]=y[i]+0.5*(k2[i]=step*f(x+h, t1, i));
	for (i=0;i<N;i++) t3[i]=y[i]+    (k3[i]=step*f(x+h, t2, i));
	for (i=0;i<N;i++) k4[i]=		step*f(x+step, t3, i);

	for (i=0;i<N;i++) y[i]+=(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6.0;
}

double  f(double x, double y[], int i){
	if (i==0) return(y[1]);						/* derivative of first equation */
	if (i==1) return(-y[1]-y[0]);
	//if (i==1) return(-(charge*(exp(pow(-t,2)/pow(-.001,2))/(18*1e5))*y[1]-y[0]);	
		/* derivative of second equation */
}
 
Physics news on Phys.org
  • #2

Thank you for sharing your code for the 4th order Runge-Kutta method to calculate electron trajectories. I understand that you are trying to substitute certain conditions into your program, but it is returning incorrect results. I have taken a look at your code and have a few suggestions that may help.

Firstly, it seems that your initial conditions are not being properly set in the code. In the "main" function, you have set the initial position to 0.001, but in the forum post, you mentioned that you want to set the lower limit to -0.003. Similarly, the initial velocity is set to 0, but in the forum post, you mentioned that you want to set the derivative at x=-0.003 to be 0. I would suggest changing these values in your code to match your desired initial conditions.

Secondly, it seems that you are trying to change the limits of your calculation by changing the value of "MAX". However, "MAX" is currently set to 10, which is larger than the upper limit you mentioned in the forum post (0.007). I would suggest changing the value of "MAX" to match your desired upper limit.

Lastly, in the "f" function, it seems that the equation for the derivative of the second equation is not correct. You have commented out the original equation and have written a new one, but it is missing a closing parenthesis and there are some missing variables. I would suggest reviewing this equation and making sure it is correct before running your code.

I hope these suggestions help and that you are able to successfully substitute your desired conditions into your program. Good luck with your research!
 

Related to Not sure if this is the right place: Runge Kutta in C for electron trajectory

1. What is the purpose of using the Runge Kutta method in C for electron trajectory?

The Runge Kutta method is a numerical algorithm used to solve differential equations, making it a useful tool for studying the motion of particles such as electrons. By implementing this method in C, scientists can accurately calculate the trajectory of an electron and understand its behavior in different environments.

2. How does the Runge Kutta method work?

The Runge Kutta method works by breaking down a complex differential equation into smaller, simpler steps. It then uses these steps to approximate the solution at each point along the trajectory of the electron. This method is known for its accuracy and ability to handle a wide range of differential equations.

3. Can the Runge Kutta method be used for other types of particles besides electrons?

Yes, the Runge Kutta method can be used to study the motion of any type of particle. It is commonly used in fields such as physics, engineering, and mathematics to solve a variety of differential equations.

4. Are there any limitations to using the Runge Kutta method in C for electron trajectory?

While the Runge Kutta method is a powerful tool, it does have some limitations. It may not be the most efficient method for solving certain types of differential equations, and the accuracy of the results can be affected by the size of the time step used in the calculations.

5. How can I implement the Runge Kutta method in C for studying electron trajectory?

There are many resources available online that provide code examples and step-by-step guides for implementing the Runge Kutta method in C. It is also helpful to have a strong understanding of differential equations and numerical methods before attempting to use this method for studying electron trajectory.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
Replies
61
Views
1K
  • Advanced Physics Homework Help
Replies
3
Views
2K
  • Advanced Physics Homework Help
Replies
4
Views
7K
  • Programming and Computer Science
2
Replies
36
Views
3K
Replies
1
Views
9K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
826
Back
Top