Why Does My Java Graph Show Incorrect E Field Values?

  • Context: Java 
  • Thread starter Thread starter CAF123
  • Start date Start date
  • Tags Tags
    Java Strange
Click For Summary

Discussion Overview

The discussion revolves around a Java program intended to graph the electric field (E field) near a conducting plane, specifically addressing an issue where the graph does not match expected values at certain points. Participants are examining the code implementation and the mathematical expression used for the E field.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a mathematical expression for the E field and describes issues with the resulting graph not aligning with expected values.
  • Another participant identifies a potential problem in the code related to how the variable x[i] is calculated, suggesting that integer division is occurring.
  • Multiple participants agree that the expression i/points is causing integer division, which results in incorrect values for x[i].
  • Suggestions are made to cast either i or points to double to avoid integer division and obtain more accurate results.
  • There is a discussion about rounding and truncating values, with some participants clarifying that formatting affects display rather than the variable itself.

Areas of Agreement / Disagreement

Participants generally agree on the issue of integer division affecting the calculation of x[i], but there is no consensus on the best method for rounding or truncating values in the context of the program.

Contextual Notes

The discussion highlights limitations in the current implementation, particularly regarding the handling of data types and mathematical operations in Java, which may affect the accuracy of the graph.

CAF123
Gold Member
Messages
2,918
Reaction score
87
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?
 
Technology news on Phys.org
Your problem is here:
Code:
x[i] = (100*(int)i/points);
Surely you meant to use something else. Anything else.
 
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!:-p
 
Mark44 said:
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!:-p

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?
 
CAF123 said:
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.
CAF123 said:
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 20 ·
Replies
20
Views
5K