NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
May 29, 2020, 08:53:07 pm

An interaction between two opposites produces a unique outcome. ..."Jules Henri Poincare(1854-1912, One of France's greatest mathematicians)"

 Pages: [1]   Go Down
 Author Topic: how make loop for array for y coordinates?  (Read 16265 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message on: April 20, 2010, 10:13:54 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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

}

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."
 arrayydiection.png (9.66 KB, 640x400 - viewed 1565 times.)  arrayxdiectionworks.PNG (3.14 KB, 258x57 - viewed 1379 times.) Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #1 on: April 20, 2010, 11:19:03 am » posted from:Taipei,T\'ai-pei,Taiwan

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]?
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #2 on: April 20, 2010, 12:05:15 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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!
 *** There are 1 more attached files. You need to login to acces it! « Last Edit: April 20, 2010, 12:08:42 pm by lookang » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #3 on: April 20, 2010, 01:43:24 pm » posted from:Taipei,T'ai-pei,Taiwan

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?
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #4 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.

 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #5 on: April 20, 2010, 09:38:25 pm » posted from:Singapore,,Singapore

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

}
}
 « Last Edit: April 20, 2010, 09:41:11 pm by lookang » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #6 on: April 20, 2010, 09:40:52 pm » posted from:Taipei,T'ai-pei,Taiwan

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.

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!

 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #7 on: April 20, 2010, 09:43:28 pm » posted from:Singapore,,Singapore

this is fantastic!!
i was thinking how to rotate, will learn a lot again.

thanks a million!!
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #8 on: April 22, 2010, 02:53:30 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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!!
 compassBcalculationwrong.gif (1079.77 KB, 640x480 - viewed 752 times.) *** There are 1 more attached files. You need to login to acces it! « Last Edit: April 22, 2010, 03:10:32 pm by lookang » Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #9 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!

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!

 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #10 on: April 24, 2010, 11:49:48 pm » posted from:Taipei,T\'ai-pei,Taiwan

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!
 Logged
ahmedelshfie
Ahmed
Hero Member

Offline

Posts: 954

 « Embed this message Reply #11 on: April 25, 2010, 12:50:29 am » posted from:Uberaba,Minas Gerais,Brazil

Nice work prof i hope one day improve like you in code
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #12 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.

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!
 Logged
ahmedelshfie
Ahmed
Hero Member

Offline

Posts: 954

 « Embed this message Reply #13 on: April 25, 2010, 10:32:55 pm » posted from:Uberaba,Minas Gerais,Brazil

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

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!

 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3085

 « Embed this message Reply #14 on: May 14, 2010, 11:22:41 pm » posted from:Taipei,T\'ai-pei,Taiwan

You are welcomed to check out Ejs Open Source 2 Bar Magnet & Compass Model java applet

You can drag two magnets and rotate each one of them and find out the field pattern.
 Logged
 Pages: [1]   Go Up
An interaction between two opposites produces a unique outcome. ..."Jules Henri Poincare(1854-1912, One of France's greatest mathematicians)"