How can I make this code in 2 dimention?
Oct 28, 2014 at 11:39am UTC
Hi all,
The code below calculates the dx/dt=u equation by euler method. This calculates the u for the position of each particles (np) and calculates the distance of particles (dist). My teacher told me by using for lop make this in 2 dimension? would you please tell me how?
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
#include <iostream>
#include <cmath>
using namespace std;
int np=100;
int dt=0.1;
double dist;
double euler(double u, double u_previous_time_step){
double a;
a=(dt/2)*(u_previous_time_step + u);
return a;
}
int main(){
int n; int i;
int tmax, t;
int n1,n2;
double x[n], u[n];
double u_previous_time_step[n];
while (t<tmax)
{
for (n=0; n<np; n++){
x[n]+=euler(u[n], u_previous_time_step[n]);
}
t=t+dt;
for (n1=0; n1<np; n1++)
for (n2=0; n2<np; n2++)
{
dist= (x[n2])-(x[n1]);
}
}
return 0;
}
Oct 28, 2014 at 12:01pm UTC
That does not look like a functional/logically correct program. Pretty much everything is uninitialized.
What do you mean by "two dimensions"? Do you refer to position, which (presumably) is now one-dimensional?
Oct 28, 2014 at 12:10pm UTC
yes for example in line 22 we make it
double x[n][2], u[n][2];
Oct 28, 2014 at 12:42pm UTC
Frankly, I have no idea what you mean by
calculate the dx/dt=u equation by euler method
and the code does not explain it either. Please explain with more details and examples.
The code you have cannot be "correct". Shouldn't you first do the 1D right, before we expand to 2D?
Topic archived. No new replies allowed.