-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integrator2.java
73 lines (51 loc) · 1.72 KB
/
Integrator2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**************************************
Lab 1, Part A, task 2
28 January 2019
Fei Alm, Shamoun Mourad, MT1a
**************************************/
package integrator2;
public class Integrator2 {
public static void main(String[] args) {
double integral;
int N = 1;
double base = 2.0/N;
double error = 0.0;
double F2= 8.0/3.0; //exact value of area
int intError = 0;
int limit = 1;
do {
integral = 0.0; // integral nollställs till noll varje gång ny do-while loop körs
System.out.println("Step: " + N); //Räknar varv som loopen körs
for (int i=1; i < N+1; i++)
{
double height = Math.pow(i*base,2);
//double height =(i*base)*base;
//height = base*base, height ökar, därför lägger vi till i, för att i++
//System.out.println(height);
//import function from Math-library
double area = height * base;
integral += area;
}
//System.out.println(integral);
//System.out.println(intError);
error = (integral - F2)/F2;
error = Math.round(100.0*error);
intError = (int)error;
// gör om error till int, lagrar i ny variabel
//if else sats här
if(integral > 10.00)
{
System.out.println(String.format("The result is: %4.2f units of length and the error is %-2d%% " , integral , intError));
}
else
{
System.out.println(String.format("The result is: 0%4.2f units of length and the error is %-2d%% " , integral , intError));
}
// When increasing value of N, error value decrease
// 4 slots, two of them are for deciamls
// to write out %-symbol, code: %%
N++; // lägger till ett N
base = 2.0/N; // new base when N is changed
}while ( intError > limit);
}
}