| FunktionPlot.java |
1 /**
2 * 183.592 Programmierpraxis TU Wien WS2014/15 H.Moritsch
3 * Plot einer Funktion y = f(x) mittels '*'-Zeichen
4 * im Intervall von a bis b mit Schrittweite dx
5 */
6 public class FunktionPlot {
7 public static void main(String[] args) {
8
9 double a = -2.0;
10 double b = 3.0;
11 double dx = 0.25;
12
13 double x;
14 double y;
15
16 double pos; // (horizontale) Position
17
18 x = a;
19 // *solange* x innerhalb des Intervalls liegt:
20 while (x <= b) {
21
22 y = f(x);
23
24 pos = 0.0;
25 // *solange* pos "links" des Funktionswertes liegt:
26 while ( pos < y ) {
27
28 System.out.print(' ');
29
30 pos = pos + dx/2.0; // nächster Positionsschritt
31 }
32
33 System.out.println('*'); // entspricht dem Funktionswert
34
35 x = x + dx; // nächster x-Wert
36 }
37
38 }
39
40 /**
41 * Berechnung der Funktion -0.01*x^5 + 0.08*x^4 - 0.26*x^3 - 0.31*x^2 + 1.4*x + 2.36
42 */
43 static double f(double x) {
44 return -0.01*x*x*x*x*x + 0.08*x*x*x*x - 0.26*x*x*x - 0.31*x*x + 1.4*x + 2.36;
45 }
46
47 }
48