Неверный вывод при запуске программы

Код:
f = @(x) x - 5 - 1/3*cos(2*x+1) + (2*x)/(2+x^2);
g = @(x) x - f(x);
a = 0; b = 1; % интервал, на котором ищем решение
M1 = @(x) max(abs(4*del2(f(x)))); % оценка модуля производной второго порядка
eps = 1e-5; % заданная точность
N_apr = @(x) ceil(log(M1(x)*(b-a)/eps)/log(2)); % априорная оценка числа итераций
x0 = (a+b)/2; % начальное приближение
N_post = 0; % счетчик числа итераций
while true
x1 = g(x0);
N_post = N_post + 1;
if abs(x1-x0) < eps*(1-M1(x1))/M1(x1) % проверка условия окончания итераций
break;
end
x0 = x1;
end
disp(['Приближенное решение: x = ', num2str(x1)]);
disp(['Число итераций: N = ', num2str(N_post)]);
disp(['Априорная оценка числа итераций: N_apr = ', num2str(N_apr(x1))]);

Вывод:
Приближенное решение: x = 4.4168
Число итераций: N = 1
Априорная оценка числа итераций: N_apr = -Inf

Как исправить то, что в N_apr = -inf?

4 Comments

Google translation:
Wrong output when starting the program
Code:
f = @(x) x - 5 - 1/3*cos(2*x+1) + (2*x)/(2+x^2);
g = @(x) x - f(x);
a = 0; b = 1; % interval on which we are looking for a solution
M1 = @(x) max(abs(4*del2(f(x)))); % estimate of the modulus of the second order derivative
eps = 1e-5; % specified accuracy
N_apr = @(x) ceil(log(M1(x)*(b-a)/eps)/log(2)); % a priori estimate of the number of iterations
x0 = (a+b)/2; % initial approximation
N_post = 0; % iteration counter
while true
x1 = g(x0);
N_post = N_post + 1;
if abs(x1-x0) < eps*(1-M1(x1))/M1(x1) % check the iteration termination condition
break;
end
x0 = x1;
end
disp(['Approximate solution: x = ', num2str(x1)]);
disp(['Number of iterations: N = ', num2str(N_post)]);
disp(['A priori estimate of the number of iterations: N_apr = ', num2str(N_apr(x1))]);
Conclusion:
Approximate solution: x = 4.4168
Number of iterations: N = 1
A priori estimate of the number of iterations: N_apr = -Inf
How to fix what's in N_apr = -inf?
Torsten
Torsten on 10 Jun 2023
M1 is always evaluated for a single value of x. Why are you using del2(f(x)) here ? It will return 0 .
Anastasya
Anastasya on 10 Jun 2023
how then to calculate M1?
M1 =@(x) max(abs(diff(f(x), x, 2)));?
Torsten
Torsten on 10 Jun 2023
Edited: Torsten on 10 Jun 2023
syms x
f = x - 5 - 1/3*cos(2*x+1) + (2*x)/(2+x^2);
d2f = diff(f,x,2)
d2f = 
absd2f = abs(d2f)
absd2f = 
Now you take "max" of this expression. But what does it mean ? Determine the x that makes this expression maximum over an interval ?

Sign in to comment.

Answers (0)

Categories

Asked:

on 10 Jun 2023

Edited:

on 10 Jun 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!