Want more features on Pastebin? Sign Up, it's FREE!
Guest

biot sarvat law

By: a guest on Nov 20th, 2012  |  syntax: C++  |  size: 2.10 KB  |  views: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include<cmath>
  2. #include<iostream>
  3. #include<cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. struct point{
  8.   double a;
  9.   double b;
  10.   double c;
  11. };
  12. float mag(point v);
  13. double random_num(double a,double b);
  14. point b_field_at_point(point p);
  15. point unit(point v);
  16.  
  17. int main(){
  18.   srand(332);
  19.   double i;
  20.   double j;
  21.   double k;
  22.   int boxsize=6;
  23.   for(k=-boxsize;k<boxsize;k=k+1){
  24.     for(i=-boxsize;i<boxsize;i=i+1){
  25.       for(j=-boxsize;j<boxsize;j=j+1){
  26.         point temp;
  27.         point return_temp;
  28.         temp.a=i;temp.b=j;temp.c=k;
  29.      
  30.         cout<<i<<" "<<j<<" "<<k<<" ";
  31.         return_temp=b_field_at_point(temp);
  32.         //cout<<sqrt(return_temp.a*return_temp.a+return_temp.b*return_temp.b+return_temp.c*return_temp.c)<<" ";
  33.         if(mag(return_temp) <=1000){
  34.           cout<<return_temp.a<<" "<<return_temp.b<<" "<<return_temp.c<<endl;
  35.         }else{
  36.           cout<<0<<" "<<0<<endl;};
  37.       };
  38.     };}
  39.   return 0;}
  40.      
  41.  
  42. double random_num(double a,double b){
  43.   return (rand()/(double)RAND_MAX)*(b-a)+a;}
  44.  
  45. point b_field_at_point(point p){
  46.   double radius=8;
  47.   point I;
  48.   I.a=I.b=I.c=0;
  49.  
  50.   double theta=0;
  51.   double constants=0;
  52.   //in amps
  53.   double current=10000000;
  54.   double i;
  55.   double iterations=50000;
  56.  
  57.   for(i=0;i<iterations;i++){
  58.    
  59.     theta=random_num(0,2*3.14159265359);
  60.    
  61.     point dis_vect;
  62.     point dl;
  63.     dis_vect.a=p.a/*+(int)(random_num(-5,5))*/;
  64.     dis_vect.b=p.b-radius*cos(theta);
  65.     dis_vect.c=p.c-radius*sin(theta);
  66.  
  67.     constants=(4*3.14159265359*pow(10,-7)*current/(4*3.14159265359))*(1/(pow(mag(dis_vect),3)));
  68.      
  69.    
  70.  
  71.     //this is the cross product
  72.     I.a=I.a+constants*(radius*sin(theta)*(p.c-radius*sin(theta))+radius*cos(theta)*(p.b-radius*cos(theta)));
  73.     I.b=I.b+constants*(radius*p.a*cos(theta));
  74.     I.c=I.c+constants*(-radius*p.a*sin(theta));
  75.  
  76.   }
  77.  
  78.   I.a=(2*3.141)*I.a/iterations;
  79.   I.b=(2*3.141)*I.b/iterations;
  80.   I.c=(2*3.141)*I.c/iterations;
  81.   return I;
  82.  
  83. }
  84.    
  85. point unit(point v){
  86.  
  87.   point temp;
  88.   double mag=sqrt(v.a*v.a+v.b*v.b+v.c*v.c);
  89.   temp.a=v.a/mag;
  90.   temp.b=v.b/mag;
  91.   temp.c=v.c/mag;
  92.  
  93.   return temp;}
  94.  
  95. float mag(point v){
  96.  
  97.   return sqrt(v.a*v.a+v.b*v.b+v.c*v.c);}
clone this paste RAW Paste Data