NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
October 23, 2017, 07:45:56 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
Wisdom consists of the anticipation of consequences. ..."Norman Cousins(1913-1990, American author)"
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: how to make magnet1 and magnet2 in a single applet with different rotation?  (Read 4092 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
on: April 27, 2010, 03:53:59 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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#msg6056

i 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 Compass
public double[] getBcomputeField(double x, double y) {
  double[] b=new double[4];
  for(int i=0; i<=nd; i++) { // lookang change i// double dx=x-dipoles[i][0]-xMagnet;
// double dy=y-dipoles[i][1]-yMagnet;
   double dx=x-dipoles[i][0]-(xMagnet*cs+yMagnet*sc);//FKH
   double dy=y-dipoles[i][1]-(-xMagnet*sc+yMagnet*cs);//FKH
// double dx=x-dipoles[i][0]-dipoles2[i][0]-(xMagnet*cs+yMagnet*sc)-(xMagnet2*cs2+yMagnet2*sc2);//FKH
// double dy=y-dipoles[i][1]-dipoles2[i][1]-(-xMagnet*sc+yMagnet*cs)-(-xMagnet2*sc2+yMagnet2*cs2);//FKH
   double dx2=x-dipoles2[i][0]-(xMagnet2*cs2+yMagnet2*sc2);//FKH
   double dy2=y-dipoles2[i][1]-(-xMagnet2*sc2+yMagnet2*cs2);//FKH

double 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[0]+=m*(3*cos*cos-1)/r3; //x component
 b[1]+=m*3*sin*cos/r3; //y component
  }
  else if (magnet2==true&&magnet1==false)
  {
 b[0]+=m*(3*cos2*cos2-1)/r33; //x component
  b[1]+=m*3*sin2*cos2/r33; //y component
}

else if (magnet1==true&&magnet2==true)
{
   b[0]+=m*(3*cos*cos-1)/r3 +m*(3*cos2*cos2-1)/r33; //x component
b[1]+=m*3*sin*cos/r3 +m*3*sin2*cos2/r33; //y component
 
}

else {
  b[0]=0;
  b[1]=0;
  }


// b[0]+=m*((3*cos*cos-1)*cs+(3*sin*cos)*sc)/r3; //x component
 // b[1]+=m*((3*cos*cos-1)*(-cs)+(3*sin*cos)*sc)/r3; //y component
  
   if(r2==0||r4==0) {
   b[0]=b[1]=0;
   }
  }
  b[2] = Math.sqrt(b[0]*b[0]+b[1]*b[1]); // magnitude
  b[3] = Math.atan2(b[1],b[0]); // angle
  return b;
}

/*
Dipole field:
B=mu/4/pi/r^3[(m*rhat)rhat-m] //vector form
K=mu*M/4/pi
alpha=0
Bx=K*[(3*cos(theta)^2-1)*cos(alpha)+3*sin(theta)cos(theta)*sin(alpha)]/r^3
By=K*[(3*sin(theta)^2-1)*sin(alpha)+3*sin(theta)cos(theta)*cos(alpha)]/r^3
*/

and

Code:
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 magnitude

if (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[2];
}
else if (magnet1==true)
{
double[] b=getBcomputeField(x*cs+y*sc,-x*sc+y*cs);//FKH
   //xField[i][j] = b[0];
   //yField[i][j] = b[1];

 // old codes
xField[i][j] = b[0]*cs-b[1]*sc;//FKH
yField[i][j] = b[0]*sc+b[1]*cs;//FKH
magField[i][j]=b[2];
}

else if (magnet2==true)
{
double[] b=getBcomputeField(x*cs2+y*sc2,-x*sc2+y*cs2);//FKH
   //xField[i][j] = b[0];
   //yField[i][j] = b[1];

 // old codes
xField[i][j] = b[0]*cs2-b[1]*sc2;//FKH
yField[i][j] = b[0]*sc2+b[1]*cs2;//FKH
magField[i][j]=b[2];
}

else {
double[] b=getBcomputeField(x*cs+y*sc+ x*cs2+y*sc2,-x*sc+y*cs -x*sc2+y*cs2); // likely the codes here wrong
xField[i][j] = b[0]*cs-b[1]*sc + b[0]*cs2-b[1]*sc2;//FKH
yField[i][j] = b[0]*sc+b[1]*cs + b[0]*sc2+b[1]*cs2;//FKH
magField[i][j]=b[2];
}


 

   }
  }
}

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.


* bug2magnet.png (106.05 KB, 1024x738 - viewed 307 times.)
« Last Edit: April 27, 2010, 04:00:13 pm by lookang » Logged
lookang
Hero Member
*****
Offline Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
Embed this message
Reply #1 on: May 01, 2010, 01:50:08 pm » posted from:Singapore,,Singapore

hi prof hwang,
i doubt anyone else is going to give any sample codes to refer to on this Smiley
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#msg6056

the problem happens when cta =! cta2.

thanks!




* bug2magnet.png (80.47 KB, 792x604 - viewed 293 times.)
« Last Edit: May 01, 2010, 01:51:40 pm by lookang » Logged
Pages: [1]   Go Up
  Print  
Wisdom consists of the anticipation of consequences. ..."Norman Cousins(1913-1990, American author)"
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
is it possible to make applet make a sound when i condition is met?
Questions related to EJS
lookang 8 10200 Last post December 22, 2008, 12:10:01 pm
by lookang
How to make an applet that is controlled by F (length) and angle (cta)
Questions related to EJS
lookang 4 7899 Last post March 02, 2009, 10:22:45 pm
by Fu-Kwun Hwang
Rotation in 3D (Does the order of rotation matter?)
misc
Fu-Kwun Hwang 0 7577 Last post February 07, 2010, 03:49:45 pm
by Fu-Kwun Hwang
Rotation in 3D (Does the order of rotation matter?)
misc
ahmedelshfie 0 9504 Last post May 21, 2010, 01:46:52 am
by ahmedelshfie
how do make Ejs applet run forever unless the pause or reset
Questions related to EJS
lookang 3 4861 Last post June 21, 2010, 07:52:30 pm
by Fu-Kwun Hwang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.211 seconds with 23 queries.since 2011/06/15