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

  • Thread starter Thread starter CAF123
  • Start date Start date
  • Tags Tags
    Java Strange
AI Thread Summary
The Java code for generating a graph of the E field near a conducting plane is producing incorrect values due to an error in how the x-values are calculated. The line `x[i] = (100*(int)i/points);` is causing integer division, resulting in many x-values being set to zero. To fix this, it is recommended to cast either `i` or `points` to a double to ensure proper floating-point division. Additionally, there is confusion regarding rounding and formatting, with suggestions to use Java's Math functions for actual rounding rather than just formatting output. Correcting these issues should lead to accurate graph results.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
0
Views
309
Replies
0
Views
788
Replies
2
Views
2K
Replies
7
Views
2K
Replies
1
Views
3K
Replies
2
Views
2K
Replies
2
Views
2K
Replies
20
Views
5K
Back
Top