Why Does My Java Graph Show Incorrect E Field Values?

In summary: You can specify the number of decimal places that are displayed in printf-style statements, but the statement continues to use the unrounded value. So, for all practical purposes, the number of decimal places is irrelevant.
  • #1
CAF123
Gold Member
2,948
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?
 
Technology news on Phys.org
  • #2
Your problem is here:
Code:
x[i] = (100*(int)i/points);
Surely you meant to use something else. Anything else.
 
  • #3
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
 
  • #4
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?
 
  • #5
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.
 

FAQ: Why Does My Java Graph Show Incorrect E Field Values?

What is a "Very strange occurence in Java"?

A "Very strange occurence in Java" refers to an unexpected or unusual behavior that occurs in a Java program. This could be a bug, an error, or an unexpected output.

What causes "Very strange occurences" in Java?

There can be many reasons for "Very strange occurences" in Java, including coding errors, compatibility issues, or conflicts with other programs or systems.

How can I troubleshoot a "Very strange occurence" in Java?

To troubleshoot a "Very strange occurence" in Java, you can start by reviewing your code for any errors or inconsistencies. You can also check for updates or patches for your Java software, and try running your program on a different platform or device.

Can "Very strange occurences" in Java be prevented?

While it is impossible to completely prevent "Very strange occurences" in Java, there are steps you can take to reduce the likelihood of encountering them. This includes writing clean, well-structured code, keeping your Java software up-to-date, and testing your program thoroughly before deployment.

Is there a way to report "Very strange occurences" in Java?

Yes, you can report "Very strange occurences" in Java to the Java development team or to the community of Java developers. You can also submit bug reports to the Java bug database, which can help identify and resolve the issue. Additionally, you can seek help from online forums or communities of Java developers for troubleshooting assistance.

Similar threads

Replies
2
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
2
Views
1K
Replies
2
Views
1K
Replies
20
Views
5K
Replies
1
Views
2K
Back
Top