Results 1 to 20 of 25
- 07-26-2008, 06:43 PM #1
Equation problems in Java
y0 = x0
y1 = 2*y0 + x1
y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10
and continues until the maximum number in a text file.
"x0" is the first number in the text file and so on.
Here my text file name is "Xdata.txt" that contains:
1.2
-1.7
0.8
4.6
0.9
1.0
0.0
5.4
3.9
9.2
1.5
4.92
-0.5
3.7
2.5
8.0
9.8
-0.8
1.0
2.0
The program read from the "Xdata.txt" and get all the outputs into "Ydata.txt"Last edited by matt_well; 07-27-2008 at 08:42 AM. Reason: Attached textfile
- 07-26-2008, 07:53 PM #2
Question about the application/program.
It reads a number (x0) from a file and does some computations on it until when? What stops the computation sequence? Your example shows it stopping at y10.The maximum number(by that I assume you mean the largest) that is in the file is 9.8. It's hard to do something 9.8 times. Do you mean read to end of file?continues until the maximum number in a text file
By doing algebraic substitution on the 10 equations you show, you can reduce it to a single equation. So why do you show 10 equations? Can there be more? How many?
It seems like the program only needs this simple form:
loop until eof
read number
compute y10 = <the reduced equation>
write y10
end of loop
What is the program supposed to do?
It is not possible to write a program without there being much better specifications.Last edited by Norm; 07-26-2008 at 07:55 PM.
- 07-26-2008, 08:45 PM #3
No no, the idea is like this.
Maximum number I mean here is the numbers that the textfile have from beginning until the last one.
What I mean is like this, x0 is the first number in the text file until the last value in the text file.
My text file name is "Xdata.txt" contains X0 until the last X19, so there is maximum of 20 numbers.
x0 = 1.2
x1 = -1.7
x2 = 0.8
x3 = 4.6
x4 = 0.9
x5 = 1.0
until
X19 = 2.0
Here's my details explaination,
y0 = x0
y1 = 2*y0 + x1
y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10
and continues until the last number in a text file.
- 07-26-2008, 09:05 PM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Yes, of course it's possible, but, since I believe this is probably a homework assignment (and even if it's not) I won't do it for you. Try it yourself, and if you get stuck, post your code and all error/compiler messages and we will help you correct it, but we will not do it for you.
- 07-26-2008, 09:16 PM #5
- 07-26-2008, 09:39 PM #6
This question is from my stupid idea mind to play with this textfile previously.
Ok, reading input from txt file is not the problem, but now the problem is that how should I start a for loop for these equation in this case using the continuous input x0 until x19 read from the textfile?
From the text file,
y0 = 1.2
y1 = 2*1.2 + (-1.7)
blah blah...
where...
y0 = x0
y1 = 2*y0 + x1
y2 = 2*y1 - y0 + x2
y3 = 2*y2 - y1 + x3
y4 = 2*y3 - y2 + x4
y5 = 2*y4 - y3 + x5
y6 = 2*y5 - y4 + x6
y7 = 2*y6 - y5 + x7
y8 = 2*y7 - y6 + x8
y9 = 2*y8 - y7 + x9
y10 = 2*y9 - y8 + x10
As you can see, the previously output are use for the next input, so I am thinking how's the for loop can be create.
- 07-26-2008, 10:43 PM #7
Ok, I missed the x variable at the end of each equation.
So your problem is to read a line with a number, convert it to a number and if its the first line save the value in y and go read another line.
If its not the first line, compute a new value for y using the old value of y and the new value read from the line x.
Continue until end of file.
You know this has NOTHING to do with java programming and you are wasting time trying to write this kind of program. Its purely an exercise in creating an algorithm for an obscure problem.Java Code:loop until eof: read line & convert to x if first line y = value; else y = func(y, x, oldy) save oldy end loop
If you want to learn java, I'd suggest finding some more productive problems to solve. There are several sites you can Google that will have sample student exercises you can attempt.
Good luck.Last edited by Norm; 07-26-2008 at 10:49 PM.
- 07-27-2008, 12:14 AM #8
Thanks Norm for some guides. Here's the current doing, but cannot run yet.
Java Code:import java.io.*; import java.util.*; public class FindOutputs { public static void main(String[] args) throws java.io.IOException { String inputFileName = "Xdata.txt"; List<String> fileData = getData(inputFileName); System.out.println('\n'+ "fileData size = " + fileData.size()); String path = "C:\\Ydata.txt"; PrintWriter pw = new PrintWriter( new FileOutputStream(path)); int maxSize = fileData.size(); for(int i = 0; i < maxSize; i++) { String data = getNextValue(fileData, i); double x = parseValue(data); if(firstline = ){ y = value; } else{y = func(y, x, oldy); pw.printf("%1.2f%n",oldy); } } pw.close(); System.out.print( "Output file at C drive now" + '\n'); } private static List<String> getData(String filePath) throws IOException { List<String> list = new ArrayList<String>(); FileReader fr = new FileReader(filePath); BufferedReader br = new BufferedReader(fr); String buffer; while( (buffer = br.readLine()) != null) { list.add(buffer); } br.close(); return list; } private static String getNextValue(List<String> list, int index) { if(index > list.size()-1) // gone past end of list return ""; return list.get(index); } private static double parseValue(String s) { if(s.equals("")) // missing data values appear as empty string return 0; return Double.parseDouble(s); } }
I am not so sure about the meaning..
How to get the first line and how's the working inside the function?Java Code:if first line y = value; else y = func(y, x, oldy)
I am on stuck in the middle part. Correct me.
- 07-27-2008, 03:03 AM #9
Please reread my last about a waste of time.
- 07-27-2008, 07:25 AM #10
Still interest in doing, can you give me some guide please how this work?
Java Code:if first line y = value; else y = func(y, x, oldy)
- 07-27-2008, 09:13 AM #11
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Well, set a boolean true before you start reading, and after reading the first line set it false. Use that boolean to determine whether or not you've already read the first line.
That's one easy way to do it, though not necessarily the most sophisticated.
- 07-27-2008, 04:20 PM #12
The 3rd and following equations you show can be replaced by a single one:
y[i] = 2*y[i-1] - y[i-2] + x[i]
You need to handle the cases where i is less than 2 manually, then this equation will work forever after that.
- 07-27-2008, 07:39 PM #13
Hi, here's some doing ideas.
Guide and Correct me, see I may do some mistakes. Thanks.Java Code:for(int i = 2; i < maxSize; i++) { String data = getNextValue(fileData, i); double value = parseValue(data); // What's the way to assign the 'double value' as x[i]? if(x[0]){ y[0] = x[0] ; //how to read the first value 'x[0]' only? } else if(x[1]){ y[1] = 2*y[0] + x[1]; //how to read the value 'x[1]' only? } else{ y[i] = 2*y[i-1] - y[i-2] + x[i]; } pw.close(); System.out.print( "Output file at C drive now" + '\n'); }Last edited by matt_well; 07-27-2008 at 07:46 PM.
- 07-27-2008, 08:01 PM #14
Do you mean: x[i] = value; // save value in current xto assign the 'double value' as x[i]?
- 07-27-2008, 08:14 PM #15
As I know the 'value' is already converted to a 'double value'.
Is it like this? Then, how to declare the x[i] and y[i]?
Java Code:for(int i = 2; i < maxSize; i++) { String data = getNextValue(fileData, i); double value = parseValue(data); x[i] = value; ......
- 07-28-2008, 02:42 AM #16
Are you asking how to create an array?
double[] x = new double[100]; // assume < 100 nbrs in file
double[] y = new double[100];
- 07-28-2008, 04:45 AM #17
- 07-28-2008, 09:25 AM #18
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Use an ArrayList. See the API docs.
- 07-28-2008, 03:32 PM #19
Is there a reason you need to save all the values of x and y?
What is supposed to be the output from the program?
You only need to save the last two values of y to compute the new y[i] value.
- 07-29-2008, 04:05 AM #20
Similar Threads
-
Help with resource or object of java to make a standard button
By fmetelico in forum New To JavaReplies: 0Last Post: 07-01-2008, 09:06 PM -
Make Java codes more simplier (Multidimensional Arrays)
By javanewbie in forum JCreatorReplies: 9Last Post: 06-25-2008, 04:48 AM -
Is it possible to make a Phone call program using java?
By fireball2008 in forum New To JavaReplies: 2Last Post: 05-08-2008, 06:20 PM -
Java Question
By Jay-1.1 in forum New To JavaReplies: 11Last Post: 05-01-2008, 04:04 PM -
Make sound play in a java application
By lenny in forum AWT / SwingReplies: 2Last Post: 08-13-2007, 11:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks