# Very strange occurence in Java

1. Oct 1, 2013

### CAF123

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 (Text):

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

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

2. Oct 1, 2013

### D H

Staff Emeritus
Code (Text):
x[i] = (100*(int)i/points);
Surely you meant to use something else. Anything else.

3. Oct 1, 2013

### Staff: Mentor

You need to change this line of code:
Code (Text):
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. Oct 1, 2013

### CAF123

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. Oct 1, 2013

### Staff: Mentor

I would do something like this.

Code (Text):

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