# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on April 27, 2010, 03:53:59 pm

 Title: how to make magnet1 and magnet2 in a single applet with different rotation? Post by: lookang on April 27, 2010, 03:53:59 pm how to make magnet1 and magnet2 in a single applet with different rotation?Hi everyone,i have been successful in making magnet1 and magnet2 in a single applet if the have the same orientation.http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1602msg6056;topicseen#msg6056i even made codes flexible enough to trouble shoot.Code:// computes magnetic field components and magnitude from dipoles// rename this getB to getBcomputeField because need to separate the calculation of Field from Compasspublic double[] getBcomputeField(double x, double y) {  double[] b=new double;  for(int i=0; i<=nd; i++) { // lookang change i//    double dx=x-dipoles[i]-xMagnet;//    double dy=y-dipoles[i]-yMagnet;    double dx=x-dipoles[i]-(xMagnet*cs+yMagnet*sc);//FKH   double dy=y-dipoles[i]-(-xMagnet*sc+yMagnet*cs);//FKH//   double dx=x-dipoles[i]-dipoles2[i]-(xMagnet*cs+yMagnet*sc)-(xMagnet2*cs2+yMagnet2*sc2);//FKH//    double dy=y-dipoles[i]-dipoles2[i]-(-xMagnet*sc+yMagnet*cs)-(-xMagnet2*sc2+yMagnet2*cs2);//FKH    double dx2=x-dipoles2[i]-(xMagnet2*cs2+yMagnet2*sc2);//FKH    double dy2=y-dipoles2[i]-(-xMagnet2*sc2+yMagnet2*cs2);//FKHdouble r2=dx*dx+dy*dy;    double r=Math.sqrt(r2);    double r3=r2*r;    double cos=dx/r;    double sin=dy/r;      double r4=dx2*dx2+dy2*dy2;    double r22=Math.sqrt(r4);    double r33=r4*r22;    double cos2=dx2/r22;    double sin2=dy2/r22;          // taken from http://en.wikipedia.org/wiki/Magnetic_moment if (magnet1==true&&magnet2==false) { b+=m*(3*cos*cos-1)/r3;   //x component b+=m*3*sin*cos/r3;   //y component  }  else if (magnet2==true&&magnet1==false)  { b+=m*(3*cos2*cos2-1)/r33;   //x component  b+=m*3*sin2*cos2/r33;   //y component}else if (magnet1==true&&magnet2==true){    b+=m*(3*cos*cos-1)/r3 +m*(3*cos2*cos2-1)/r33;   //x componentb+=m*3*sin*cos/r3 +m*3*sin2*cos2/r33;   //y component }else {  b=0;  b=0;  }//   b+=m*((3*cos*cos-1)*cs+(3*sin*cos)*sc)/r3;   //x component //   b+=m*((3*cos*cos-1)*(-cs)+(3*sin*cos)*sc)/r3;   //y component        if(r2==0||r4==0) {      b=b=0;    }  }  b = Math.sqrt(b*b+b*b);  // magnitude  b = Math.atan2(b,b);  // angle  return b;}/*Dipole field:B=mu/4/pi/r^3[(m*rhat)rhat-m]         //vector formK=mu*M/4/pialpha=0Bx=K*[(3*cos(theta)^2-1)*cos(alpha)+3*sin(theta)cos(theta)*sin(alpha)]/r^3By=K*[(3*sin(theta)^2-1)*sin(alpha)+3*sin(theta)cos(theta)*cos(alpha)]/r^3*/andCode:public void computeField () {  for (int i=0; i    double x=_view.vectorField2D.indexToX(i);    for (int j=0; j      double y=_view.vectorField2D.indexToY(j);      //double[] b=getB(x,y);//  Gets the components and magnitudeif (magnet1==false&&magnet2==false){  double[] b=getBcomputeField(x*cs+y*sc+ x*cs2+y*sc2,-x*sc+y*cs -x*sc2+y*cs2);// xField[i][j] = 0;yField[i][j] = 0;magField[i][j]=b;}else if (magnet1==true){double[] b=getBcomputeField(x*cs+y*sc,-x*sc+y*cs);//FKH      //xField[i][j] = b;      //yField[i][j] = b; // old codesxField[i][j] = b*cs-b*sc;//FKHyField[i][j] = b*sc+b*cs;//FKHmagField[i][j]=b;}else if (magnet2==true){double[] b=getBcomputeField(x*cs2+y*sc2,-x*sc2+y*cs2);//FKH      //xField[i][j] = b;      //yField[i][j] = b; // old codesxField[i][j] = b*cs2-b*sc2;//FKHyField[i][j] = b*sc2+b*cs2;//FKHmagField[i][j]=b;}else {double[] b=getBcomputeField(x*cs+y*sc+ x*cs2+y*sc2,-x*sc+y*cs -x*sc2+y*cs2); // likely the codes here wrongxField[i][j] = b*cs-b*sc + b*cs2-b*sc2;//FKHyField[i][j] = b*sc+b*cs + b*sc2+b*cs2;//FKH  magField[i][j]=b;}     }  }}where did the codes i did wrong?, independently, each magnet works on its own, but when both are turn on, the bug appears to be opposite the actual position of the magnet. Title: Re: how to make magnet1 and magnet2 in a single applet with different rotation? Post by: lookang on May 01, 2010, 01:50:08 pm hi prof hwang,i doubt anyone else is going to give any sample codes to refer to on this :)chance to help me out here?  how to make magnet1 and magnet2 in a single applet with different rotation with vector sum of the magnetic field vectors?http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1602msg6056;topicseen#msg6056the problem happens when cta =! cta2.thanks!