NTNUJAVA Virtual Physics Laboratory
Enjoy 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 20, 2010, 10:13:54 am



Title: how make loop for array for y coordinates?
Post by: lookang on April 20, 2010, 10:13:54 am
Hi everyone,

I tried to create a loop for distributing the y coordinate of an array dipoles[20][20]
Code:
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i <nd; i+=2) {
  dipoles[0][ i ]=-0.5+i*dy;
}

i am puzzled why it didn't draw like this picture (http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1582.0;attach=2197)
i am trying to draw two lines vertically up to position the dipoles of the magnet, to create a upright distribution of the dipoles instead of the current horizontal distribution.

the code works for x coordinates distribution
Code:
double dx=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i< nd; i+=2) {
  dipoles[ i ] [0]=-0.5+i*dx;   // x position of first line of dipoles bottom
dipoles[i+1][0]=-0.5+i*dx;   // x position of 2nd line of dipoles top
  

 dipoles[ i ][1]=-0.04;         // y position move down by -0.04 of first line of dipoles bottom
  dipoles[i+1][1]=0.04;       // y position move up by +0.04 of top line of dipoles bottom

}
(http://www.phy.ntnu.edu.tw/ntnujava/index.php?action=dlattach;topic=1582.0;attach=2201;image)

when i make
Code:
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i < nd; i+=2) {
  dipoles[i][0]=-0.5+i*dy;   // y position of first line of dipoles bottom
}
then it distribute left to right which i thought change it to dipoles[0][ i ]=-0.5+i*dy; should be y distribution.
what am i doing wrongly?
i tried to read up http://www.um.es/fem/EjsWiki/Main/ElementsArrow2D but it doesn't say much about the array
"Position []   The double[] array with the coordinates of the shape.   A double array."


Title: Re: how make loop for array for y coordinates?
Post by: Fu-Kwun Hwang on April 20, 2010, 11:19:03 am
In EJS :
1. You need to provide X[],Y[] for shapeSet. How do you related X[],Y[] to dipole[][]???
i.e. The Pos X and Pos Y should be an one-dimension array like X[20] or Y[20] (a double array)

And in the following loop you have: i+=2 so half of the index for dipoles[0] were not defined.
Code:
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i < n d; i+=2) {
  dipoles[0][ i ]=-0.5+i*dy;
}
By the way what is the meaning of dipoles[][]?
Why the dimension is dipoles[20][20]? instead of dipoles[20][2]?


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 20, 2010, 12:05:15 pm
In EJS :
1. You need to provide X[],Y[] for shapeSet. How do you related X[],Y[] to dipole[][]???
i.e. The Pos X and Pos Y should be an one-dimension array like X[20] or Y[20] (a double array)
In this simulation http://www.compadre.org/osp/items/detail.cfm?ID=9414 , i am making sense of the variables by trial and error, testing and drawing objects to test what each codes are doing.
i have remix it here http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1210.0
from my experience tinkering with the codes, dipoles is an array [nd][2] where nd is integer = 20, i am not sure why but there is no X[] and Y[] to draw the dipoles but by testing out, i keyed in dipoles in the ArrowSet Position[] as "dipoles" and it drew the position of the dipoles, without X[]and Y[].


And in the following loop you have: i+=2 so half of the index for dipoles[0] were not defined.
strangely when i change it to i+=1, no more B field drawn?, it is using the empty dipoles to create the magnetic field?
i change to i+= 4, then it draw B field again


Code:
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i < n d; i+=2) {
  dipoles[0][ i ]=-0.5+i*dy;
}
By the way what is the meaning of dipoles[][]?
[/quote]
dipoles[][] is the array to "positions of dipoles" so by changing values, i come to understand that dipoles[X][Y] is the way the array is made.



Why the dimension is dipoles[20][20]? instead of dipoles[20][2]?

oh, i changed it to test out whether i need [20][20] because i thought i need the [X][Y] the Y to be 20 because i was trying to draw (positions of dipoles) vertically up.
original is dipoles[20][2] = so it drawn

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 which is a [X][Y]=[20][2]

so i thought to draw (positions of dipoles)
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0  i need [X][Y]=[2][20]

but since the codes need to work for all cases, i just make it [X][Y]=[20][20] thinking it will not adverse affect the calculation of Custom Functions.

i attached my xml for ease of discussion and so that you can see everything.
Thanks!


Title: Re: how make loop for array for y coordinates?
Post by: Fu-Kwun Hwang on April 20, 2010, 01:43:24 pm
According to http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1210.0
dipoles was defined as dipoles[nd][2]

Code:
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i < n d; i+=2) {
  dipoles[0][ i ]=-0.5+i*dy;
}
The above code will produce error message because you try to access dipoles[0][ i ] (where i might be larger than 1).
The above code did not make sense.

You should try to understand the code instead of did it by "trial and error".


The dipoles was used to simulate the magnet (it consists of magnetic dipole at different x (same y).
The needles which show direction of magnetic field are draw with VectorField2D (which defined by xField and yField)
And it was calculated with computeField () -- call getBComputeField(x,y); to set xField and yField.

May I know what you really want to do?


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 20, 2010, 01:58:14 pm
Currently it is horizontal.
I want to make the magnet vertical.
In other words, it is 'like' rotate 90 degree.
 :D


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 20, 2010, 09:38:25 pm
got it!!!!
i see now
dipoles[ i ][ 0 ] = dipoles[ i ][X]
dipoles[ i ][1] = dipoles[ i ][Y]
no worries!
my earlier posts are wrong.
next i will try to make a general angle, see if i can understand the codes more.


Code:
if (earth==true){
double dy=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i  dipoles[i][1]=-0.5+i*dy;   // y position of first line of dipoles bottom
dipoles[i+1][1]=-0.5+i*dy;   // y position of 2nd line of dipoles top
  

 dipoles[i][0]=-0.04;         // x position move down by -0.04 of first line of dipoles bottom
  dipoles[i+1][0]=0.04;       // x position move up by +0.04 of top line of dipoles bottom
// }

}
}

  else {

double dx=1.0/(nd-1);    //set up dipoles under bar magnet
for(int i=0; i  dipoles[i][0]=-0.5+i*dx;   // x position of first line of dipoles bottom
dipoles[i+1][0]=-0.5+i*dx;   // x position of 2nd line of dipoles top
  

 dipoles[i][1]=-0.04;         // y position move down by -0.04 of first line of dipoles bottom
  dipoles[i+1][1]=0.04;       // y position move up by +0.04 of top line of dipoles bottom

}
}


Title: Re: how make loop for array for y coordinates?
Post by: Fu-Kwun Hwang on April 20, 2010, 09:40:52 pm
Please check out the version I modified from the original one.
Now, you can rotate the magnet to any angle you want (including vertically).

 What I did was make a transformation:
$x'=x \cos\theta+ y\sin\theta$
$y'=-x\sin\theta+ y\cos\theta$
for all the coordinates (magnet, compass, field)

The magnetic field is also transformed by
$B'_x=B_x \cos\theta-B_y \sin\theta$
$B'_y=B_x \sin\theta+B_y \cos\theta$

The following is a picture shows magnetic field structure of a magnet.
(http://upload.wikimedia.org/wikipedia/commons/thumb/5/57/Magnet0873.png/220px-Magnet0873.png)

[ejsapplet]




Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 20, 2010, 09:43:28 pm
this is fantastic!!
i was thinking how to rotate, will learn a lot again.

thanks a million!!


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 22, 2010, 02:53:30 pm
i think there is a bug in the calculation of B field.

the animated gif (click on the thumbnail to view) shows 2 different reading of B for the same position from the North pole.

horizontal B = 16.9
vertical B = 2.68
same direction from North pole, something is wrong, can help me? i read the code but no idea how to fix it :)
thanks

i attached my remix in case need, i am slowly add in


oh!!! i did it!!
fixed the bug in the calculation of B field.

What I did was make a transformation:
$x'=x \cos\theta+ y\sin\theta$
$y'=-x\sin\theta+ y\cos\theta$
for all the coordinates (magnet, compass, field)
Code:
  double dx=x-(dipoles[i][0]*cs+dipoles[i][1]*sc)-(xMagnet*cs+yMagnet*sc);//FKH
    double dy=y-(-dipoles[i][0]*sc +dipoles[i][1]*cs)-(-xMagnet*sc+yMagnet*cs);//FKH

not to worry. hurray!!


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 24, 2010, 08:06:57 pm
i managed to get the reading of the B field to work for both the new rotated axis and the universal x and y axis.

it took a long time (but it is worth it) for me to narrow down to which code affected the reading and it was a challenge to transform the axis 2 times to get the universal x and y axis.

Thanks for your tips and wisdom!

[eye]
[ejsapplet]
[/eye]


Title: Re: how make loop for array for y coordinates?
Post by: Fu-Kwun Hwang on April 24, 2010, 11:49:48 pm
I know that you can do it and solve the problem by yourself. What I need to do is provide enough hits.
I am happy to know that you learn how to do the transofrmation (coordinate rotation + coordinate translation).
One suggestion: Could you make the message area smaller.
Good job! :D


Title: Re: how make loop for array for y coordinates?
Post by: ahmedelshfie on April 25, 2010, 12:50:29 am
Nice work prof i hope one day improve like you in code  :)


Title: Re: how make loop for array for y coordinates?
Post by: lookang on April 25, 2010, 10:09:27 am
One suggestion: Could you make the message area smaller.
All agreed. modified to be smaller

Nice work prof i hope one day improve like you in code  :)
Yes. you can do it (self directed) through this professional learning community forum (collaboration).
when you find meaning in what you do, you will be motivated to learn. ;D


Title: Re: how make loop for array for y coordinates?
Post by: ahmedelshfie on April 25, 2010, 10:32:55 pm
Yes Prof,, i tried make yellow shape more smaller like suggestion prof Hwang
But no have succeed any way i modified interface to be more interesting  ;)

[eye]
[ejsapplet]
[/eye]


Title: Re: how make loop for array for y coordinates?
Post by: Fu-Kwun Hwang on May 14, 2010, 11:22:41 pm
You are welcomed to check out Ejs Open Source 2 Bar Magnet & Compass Model java applet (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1602.msg6216#msg6216)

You can drag two magnets and rotate each one of them and find out the field pattern.