For x varying from a to b with a step (ba) / k, where (k = 10), calculate the function f (x) using its expansion in a power series in three cases:
- a) for a given n;
- b) for a given accuracy e (e = 0.0001);
- c) for the “exact” value (according to the analytical formula).
For comparison, find the relative error in calculating the function function value o_pogr = ABS ((exact_value - approximate_value) / exact_value)
- function: y = -ln | 2sinx / 2 |
- range of argument variation: pi / 5 <= x <= 9pi / 5
- n = 40
sum: S = cosx + cos2x / 2 + ... + cosnx / n
package laboratornaya3; import static java.lang.Math.*; public class Laboratornaya3 { public static void main(String[] args) { double a = PI / 5, b = 9 * PI / 5, E = 0.0001, pSN, pSE; int n = 40; double dx=(ba)/10; //Шапка System.out.printf("%-15s %-30s %-30s %-30s %-30s %-30s", "x", "y", "SN", "SE", "pSN", "pSE"); System.out.println(); for (double x = a; x <= b; x += dx) { // Точное значение Y double y = -log(abs(2*sin(x/2))); // SN double SN = 0; for (int i = 1; i <= n; i++) { SN += cos(i*x)/i; } // SE double SE = 0; int i = 1; double step; do { step = cos(i*x)/i; SE+=step; i++; } while (abs(step)>E); // погрешности pSN = abs((y - SN) / y); pSE = abs((y - SE) / y); //вывод System.out.printf("%-15s %-30s %-30s %-30s %-30s %-30s", String.format("%.5f", x), y, SN, SE, pSN, pSE); System.out.println(); } } }