Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

ЛБ6 / Звіт

.pdf
Скачиваний:
1
Добавлен:
20.12.2024
Размер:
784.44 Кб
Скачать

MINISTRY OF EDUCATION AND SCIENCE OF UKRAINE KHARKIV NATIONAL UNIVERSITY OF RADIO ELECTRONICS

Department of ST

Report

on the implementation of laboratory work No. 6

" Application and analysis of numerical differentiation methods" in the discipline "Numerical integration of functions"

Complied by student KNT-23-1:

Accepted:

Roman, Kravchenko

P.E. Sytnikova,

 

Kharkiv 2024

Purpose of the work: application of numerical methods of calculating derivatives in various practical problems. Analysis and comparison of results with analytical ones.

Work progress

1.Choose an option of an individual task from the table. 4.1 according to the number n of the student in the group list.

Variant 11: ( ) = − − + − −

2.Using the rules of analytical calculation of derivatives, calculate the derivative f`(x) and get an exact solution to the problem.

`( ) = (−4 4 3 + 2 − 4 − 4)` = − − + −

3. Select point , step value h =

 

1 and

define

nodes

= + , =

 

 

 

 

 

 

̅̅̅̅

 

 

, ± , ± , … . Calculate the value

 

= (

 

 

 

), ` = `( )

 

 

 

 

 

 

 

 

 

a. Select initial point

 

= , and calculate the .

 

 

 

 

 

 

 

 

 

1 = 0 + 1

∙ 1 = 1

 

 

 

 

−1 = 0 + (−1) ∙ 1 = −1

2 = 0 + 2

∙ 1 = 2

 

 

 

 

−2 = 0 + (−2) ∙ 1 = −2

3 = 0 + 3

∙ 1 = 3

 

 

 

 

−3 = 0 + (−3) ∙ 1 = −3

b. Calculate function and derivative values at nodes .

 

= −4 ∙ (−2)4

− (−2)3 + (−2)2 − 4 ∙ (−2) − 4 = −

−2

 

 

 

 

 

 

 

 

 

 

= −4 ∙ (−1)4

− (−1)3 + (−1)2 − 4 ∙ (−1) − 4 = −

−1

 

 

 

 

 

 

 

 

 

0 = −4 ∙ 0 − 0 + 0 − 4 ∙ 0 − 4 = −

1 = −4 ∙ 1 − 1 + 1 − 4 ∙ 1 − 4 = −

= −4 ∙ 24 − 23

+ 22 − 4 ∙ 2 − 4 = −

2

 

 

 

 

 

 

 

 

 

̅̅̅̅̅̅

 

 

 

 

 

3

 

2

`−2

= `( −2) = −16 ∙ (−2)

− 3 ∙ (−2) + 2 ∙ (−2) − 4 =

̅̅̅̅̅̅

 

 

 

3

 

 

2

+ 2 ∙ (−1) − 4 =

`−1

= −16 ∙ (−1) − 3 ∙ (−1)

 

̅̅̅̅

= −16 ∙ 0 − 3 ∙ 0 + 2 ∙ 0 − 4 = −

`0

̅̅̅̅

= −16 ∙ 1 − 3 ∙ 1 + 2 ∙ 1 − 4 = −

`1

̅̅̅̅

= −16 ∙ 2

3

− 3 ∙ 2

2

+ 2 ∙ 2 − 4 = −

`2

 

 

x

-2

-1

0

1

 

2

 

 

 

 

 

 

 

f(x)

-48

-2

-4

-12

 

-80

 

 

 

 

 

 

 

f`(x)

108

7

-4

-21

 

-140

 

 

 

 

 

 

 

f``(x)

-178

-40

2

-52

 

-202

 

 

 

 

 

 

 

 

Table 1. values of function and its derivative at nodes

 

4.Develop an algorithm for the numerical differentiation method using the simplest numerical differentiation formulas.

Impement the function to find first derivative, using backward difference, central difference and second backward difference formulas.

Backward difference: `( ) = ( + )− ( )

static double backward_difference(double x)

{

return (f(x + h) - f(x)) / h;

}

Where ‘h’ is the constant value set by developer manually.

Central difference: `( ) = ( + )− ( − )

2

static double central_difference(double x)

{

return (f(x + h) - f(x - h)) / 2. * h;

}

Second backward difference: ``( ) = ( + )−2 ( )+( − )

2

static double second_central_difference(double x)

{

return (f(x + h) – 2. * f(x) + f(x - h)) / pow(h, 2);

}

5.For the constructed nodes, using the developed program, find the value of the first and second derivatives according to formulas (4.1) - (4.3). In each case, estimate the differentiation error in two ways: 1) as the modulus of the

difference between the exact and approximate values of the derivative; 2) using the appropriate formulas for derivative errors:

a.First we must implement the functions to find the absolute and marginal errors of each method.

Absolute error calculation function.

//The coefficients of my polynomial constexpr double a = -4;

constexpr double b = -1; constexpr double c = 1; constexpr double d = -4; constexpr double e = -4;

//General formula to find the derivative of fourth //degree polynomial static double first_derivative(double x)

{

return 4 * a * pow(x, 3) + 3 * b * pow(x, 2) + 2 * c * x + d;

}

template<typename Method, typename Derivative>

static double absolute_error_calculation(Method formula, Derivative der, double x)

{

return std::abs(formula(x) - der(x));

}

Now define functions to find marginal errors of each method.

Formula to find marginal error for difference derivative:

=

 

max | ``( )| =

1

.

2

2

 

[

, ]

 

 

 

0

1

 

 

The coding implementation worth a remark. To find maximum value of fourth degree polynomials second derivative, it requires finding only one critical point of the one that is actually calculated by solving an equation 24 + 6 = 0 and the critical

point can be found as 246 = − 4 , check if critical point is at the interval and then

determine the maximum second derivative’s absolute value at left, right limits and the critical point.

static double calculate_M1(double x0, double x1)

{

double crit_point = -b / (4. * a);

if (crit_point < x0 || crit_point > x1) crit_point = x0;

return std::max({ std::abs(second_derivative(x0)), std::abs(second_derivative(x1)), std::abs(second_derivative(crit_point)) });

}

static double marginal_error_for_difference_derivative(double x)

{

double x0 = x; double x1 = x + h;

return calculate_M1(x0, x1) * h / 2.;

}

Formula to find central difference error

 

2

 

 

 

2

=

 

max

| ```( )| =

2

.

6

6

 

[

,

]

 

 

 

−1

1

 

 

 

Because third derivative is just a line of type maximum by comparing the absolute value of third e.g. this function has no critical points.

+ , we can simply find the derivative at left, right limit points,

static double marginal_error_for_central_difference(double x)

{

double x0 = x - h; double x1 = x + h;

double M2 = std::max(std::abs(third_derivative(x0)), std::abs(third_derivative(x1)));

return M2 * pow(h, 2) / 6.;

}

And now second difference derivative error formula

= − 2 max | ( )|.

12 [−1,1]

The fourth derivative of f(x) is just a straight line of type = 24 , then its max value is simply an absolute value of it. Then

static double marginal_error_for_second_difference(double x)

{

return -pow(h, 2) / 12. * std::abs(fourth_derivative(24 * a));

}

b. After all these steps we now can find the values of first and second derivative and estimate their absolute and marginal errors at the constructed nodes.

c.Tables for each obtained value. i = -2, x= -2:

 

Difference

Central difference

Second difference

 

derivative

derivative

derivative

 

 

 

 

Exact value

108

108

-178

 

 

 

 

Formula value

46

139

-186

Absolute error

62

31

8

Marginal error

89

47

8

 

 

 

 

i = -1, x= -1:

 

 

 

 

 

 

 

 

Difference

Central difference

Second difference

 

derivative

derivative

derivative

 

 

 

 

Exact value

7

7

-40

 

 

 

 

Formula value

-2

22

-48

 

 

 

 

Absolute error

9

15

8

 

 

 

 

Marginal error

20

31

8

 

 

 

 

i = 0, x= 0:

 

 

 

 

 

 

 

 

Difference

Central difference

Second difference

 

derivative

derivative

derivative

 

 

 

 

Exact value

-4

-4

2

 

 

 

 

Formula value

-8

-5

-6

 

 

 

 

Absolute error

4

1

8

 

 

 

 

Marginal error

26

17

8

 

 

 

 

i = 1, x= 1:

 

 

 

 

 

 

 

 

Difference

Central difference

Second difference

 

derivative

derivative

derivative

 

 

 

 

Exact value

-21

-21

-52

 

 

 

 

Formula value

-68

-38

-60

 

 

 

 

Absolute error

47

17

8

 

 

 

 

Marginal error

101

33

8

 

 

 

 

i = 2, x= 2:

 

Difference

Central difference

Second difference

 

derivative

derivative

derivative

 

 

 

 

Exact value

-140

-140

-202

 

 

 

 

Formula value

-278

-173

-210

 

 

 

 

Absolute error

138

33

8

 

 

 

 

Marginal error

224

49

8

 

 

 

 

d. Program listing.

#include <iostream>

constexpr double a = -4; constexpr double b = -1; constexpr double c = 1; constexpr double d = -4; constexpr double e = -4;

constexpr double h = 1;

static double f(double x)

{

return a * pow(x, 4) + b * pow(x, 3) + c * pow(x, 2) + d * x + e;

}

static double first_derivative(double x)

{

return 4 * a * pow(x, 3) + 3 * b * pow(x, 2) + 2 * c * x + d;

}

static double second_derivative(double x)

{

return 12 * a * pow(x, 2) + 6 * b * x + 2 * c;

}

static double third_derivative(double x)

{

return 24 * a * x + 6 * b;

}

static double fourth_derivative(double x)

{

return 24 * a;

}

static double difference_derivative(double x)

{

return (f(x + h) - f(x)) / h;

}

static double central_difference_derivative(double x)

{

return (f(x + h) - f(x - h)) / (2. * h);

}

static double second_difference_derivative(double x)

{

return (f(x - h) - 2 * f(x) + f(x + h)) / pow(h, 2);

}

template<typename Method, typename Derivative>

static double absolute_error_calculation(Method formula, Derivative der, double x)

{

return std::abs(formula(x) - der(x));

}

static double calculate_M1(double x0, double x1)

{

double crit_point = -b / (4. * a);

if (crit_point < x0 || crit_point > x1) crit_point = x0;

return std::max({ std::abs(second_derivative(x0)), std::abs(second_derivative(x1)), std::abs(second_derivative(crit_point)) });

}

static double marginal_error_for_difference_derivative(double x)

{

double x0 = x; double x1 = x + h;

return calculate_M1(x0, x1) * h / 2.;

}

static double marginal_error_for_central_difference(double x)

{

double x0 = x - h; double x1 = x + h;

double M2 = std::max(std::abs(third_derivative(x0)), std::abs(third_derivative(x1)));

return M2 * pow(h, 2) / 6.;

}

static double marginal_error_for_second_difference(double x)

{

return pow(h, 2) / 12. * std::abs(fourth_derivative(24 * a)) + 0.000000001;

}

static void calculate_values()

{

double exact_value, value, absolute_error, marginal_error, x, x0 = 0; int i;

static auto print = [&](std::string str) { std::cout << "x_" << i << " = " << x <<

"\n" << str <<

"\n\tExact value: " << exact_value << "\n\tValue: " << value <<

"\n\tAbsolute error: " << absolute_error << "\n\tMarginal error: " << marginal_error <<

"\nDoes absolute error exceed marginal error? - " << std::boolalpha << (absolute_error > marginal_error) << "\n\n"; };

for (i = -2; i <= 2; ++i)

{

x = x0 + i * h;

exact_value = first_derivative(x); value = difference_derivative(x);

absolute_error = absolute_error_calculation(difference_derivative, first_derivative, x);

marginal_error = marginal_error_for_difference_derivative(x); print("difference derivative");

exact_value = first_derivative(x);

value = central_difference_derivative(x);

absolute_error = absolute_error_calculation(central_difference_derivative, first_derivative, x);

marginal_error = marginal_error_for_central_difference(x); print("central difference derivative");

exact_value = second_derivative(x); value = second_difference_derivative(x);

absolute_error = absolute_error_calculation(second_difference_derivative, second_derivative, x);

marginal_error = marginal_error_for_second_difference(x); print("second difference derivative");

}

}

int main()

{

calculate_values();

return 0;

}

6. Perform steps 3–5 for the step value h = 0.1,

 

=

 

 

 

 

 

 

 

 

 

a. Set nodes

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

x

 

-0.2

-0.1

0

 

 

0.1

0.2

 

 

 

 

 

 

 

 

 

f(x)

 

-3.158

-3.589

-4

 

 

-4.391

-4.774

 

 

 

 

 

 

 

 

 

f`(x)

 

-4.392

-4.214

-4

 

 

-3.846

-3.848

 

 

 

 

 

 

 

 

 

f``(x)

 

1.28

2.12

2

 

 

0.92

-1.12

 

 

 

 

 

 

 

 

 

b.Change the h value in our program just altering the const value ‘h’ to 0.1

c.Find values and errors.

Соседние файлы в папке ЛБ6