-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAreaUnderCurve.java
More file actions
47 lines (35 loc) · 1.38 KB
/
AreaUnderCurve.java
File metadata and controls
47 lines (35 loc) · 1.38 KB
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
import java.util.Scanner;
// Задача 2: Площадь под графиком функции
public class AreaUnderCurve {
public static double f(double x) {
return Math.pow(x, 3) + x + 1;
}
public static double calculateArea(double a, double b, int n) {
if (a > b) {
double temp = a;
a = b;
b = temp;
}
double width = (b - a) / n;
double area = 0;
for (int i = 0; i < n; i++) {
double xLeft = a + i * width;
double xRight = a + (i + 1) * width;
double height = (f(xLeft) + f(xRight)) / 2;
area += height * width;
}
return area;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите a: ");
double a = scanner.nextDouble();
System.out.print("Введите b: ");
double b = scanner.nextDouble();
System.out.print("Введите количество разбиений (n, например 1000): ");
int n = scanner.nextInt();
double area = calculateArea(a, b, n);
System.out.printf("Площадь под графиком на отрезке [%.2f, %.2f]: %.6f\n", a, b, area);
scanner.close();
}
}