Very strange occurence in Java

  • Java
  • Thread starter CAF123
  • Start date
  • #1
CAF123
Gold Member
2,936
88
I am using JAVA to generate a graph of the E field close to a conducting plane. The result which I want to graph is $$E_z (x)= \frac{Q}{2\pi \epsilon_o} \left(\frac{d}{(x^2+d^2)^{3/2}} - \frac{h}{(x^2+h^2)^{3/2}}\right).$$

The code is below:
Code:
import java.io.Console;
import java.io.*;
import java.lang.Math;
import java.lang.Boolean;
import ptolemy.plot.*;

public class Edipole {
	public static void main (String args[]) {
	
	Console myConsole = System.console();

	Plot disc = new Plot();
	disc.setTitle("Graph of the field component near the conducting plane for all x");
	disc.setXLabel("distance along x");
	disc.setYLabel("E field");

	PlotFrame myFrame = new PlotFrame("E vs x", disc);
	myFrame.setSize(10,20);

	int dataSet = 0;
	
	double q = 4;
	double d  = 10;
	double h = 1;
	double eps = 8.85e-12;
	
	
	
	int points = 500;

	double E[] = new double[points];
	double x[] = new double[points];
	
	for(int i = 0; i < (points-1); i++) {
	x[i] = (100*(int)i/points); 
	
	double Eqn = (q/(2*(Math.PI)*eps))*(d/Math.pow((Math.sqrt(x[i]*x[i]+d*d)),3) - h/((Math.pow((Math.sqrt(x[i]*x[i]+h*h)),3)));



	disc.addPoint(dataSet, x[i], Eqn, true);
	
	
	}
	System.out.println("Graph created");
	myFrame.setVisible(true);

	//System.exit(0); 

	

	} 

}
Could somebody check that I have entered the eqn in correctly? The program runs fine and I have a graph, but it doesn't agree at particular values of x. For example, for E=0, x approx. 30 in the graph, but it should be about 50. I realize that what I have is more complicated than it should be - I have tried many different ways to express the same thing.

After a while, I obtained the same expression over and over again no matter what change I made to it. So, for fun, I deleted the whole eqn and wrote double Eqn = 3x in its place. What I got was the exact same graph, which makes absolutely no sense.

Can anyone explain this bizarre occurrence?
 

Answers and Replies

  • #2
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
686
Your problem is here:
Code:
x[i] = (100*(int)i/points);
Surely you meant to use something else. Anything else.
 
  • #3
35,034
6,777
You need to change this line of code:
Code:
x[i] = (100*(int)i/points);
The expression i/points is doing integer division, which you DON'T want. For each value of i that is smaller than points, i/points evaluates to 0.

Also, your cast to int is unnecessarily casting i to an int, which it already is.

To get more reasonable values, cast either i or points to double, do the division, then round or truncate to an int.

Edit: Dang, D H beat me to it! I guess great minds think alike!:tongue:
 
  • #4
CAF123
Gold Member
2,936
88
You need to change this line of code:
Code:
x[i] = (100*(int)i/points);
The expression i/points is doing integer division, which you DON'T want. For each value of i that is smaller than points, i/points evaluates to 0.

Also, your cast to int is unnecessarily casting i to an int, which it already is.

To get more reasonable values, cast either i or points to double, do the division, then round or truncate to an int.

Edit: Dang, D H beat me to it! I guess great minds think alike!:tongue:

So something like x = ((double)i/points). I am not sure why I would want to truncate to an int now. To round, I believe the syntax is like %10.n, where n is the number of decimal points?
 
  • #5
35,034
6,777
So something like x = ((double)i/points).
I would do something like this.

Code:
double loopCount;
for(int i = 0; i < (points-1); i++) {
   loopCount = (double)i;
   x[i] = 100 * loopCount/points; 
   .
   .
   .
Now, since loopCount is a double, points will be automatically cast to a double before the division happens.
I am not sure why I would want to truncate to an int now. To round, I believe the syntax is like %10.n, where n is the number of decimal points?
No, that doesn't do anything to the variable. It affects only how the variable is displayed in printf-style output statements. If you want to actually round the variable, use round or ceil or floor, all of which are part of java.lang.Math.

BTW, there is only one decimal point - that's the period (in Europe and elsewhere, a comma) to the left of the fractional part of a number.
 

Related Threads on Very strange occurence in Java

Replies
3
Views
3K
Replies
6
Views
680
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
10
Views
1K
  • Last Post
Replies
8
Views
2K
Replies
34
Views
3K
Replies
4
Views
1K
Replies
2
Views
10K
  • Last Post
Replies
6
Views
15K
  • Last Post
Replies
13
Views
3K
M
Top