Я использую функцию f = @(x) 2*sin(x) - exp(x)/4-1
в моем алгоритме ложного положения.Дано, что корень -5.7591
является правильным корнем.Тем не менее, разные методы для вычисления ошибки неверны в моем алгоритме.Если бы не было счетчика для остановки цикла while, он работал бы всегда, когда бы я использовал абсолютную приблизительную (flag = 1) или относительную ошибку (flag = 2).
flag = 1 means calculate using absolute approx error
flag = 2 means calculate using absolute relative error
flag = 3 means calculate using true error (this one works fine)
используемый диапазон равен [-7, -5] и цикл должен остановиться, когда ошибка упадет ниже 10 ^ 6
ниже приведен код вместе с входом и выходом под ним:
function [root,counter] = FalsePosition(f,x1,x2,d,flag)
sx1 = x1;
sx2 = x2;
if(f(x1)*f(x2) >= 0)
disp("x1 and x2 are not correct")
return
end
while(flag > 3 || flag < 1)
flag = input("Flag used incorrectly! please enter a value 1 - 3: ");
end
i = 0;
E = d;
while(i < 100 && E >= d)
x3 = x2 - f(x2)*(x2-x1)/(f(x2)-f(x1));
i = i + 1;
if(f(x1)*f(x3) < 0)
x2 = x3;
x = x1;
else
x1 = x3;
x = x2;
end
if(flag == 1)
E = abs(x - x3); % abs approx error
elseif(flag == 2)
e = abs(x - x3);
E = e/abs(x3); % abs relative error
else
E = abs(f(x3)); % true error
end
end
counter = i;
root = x3;
if(flag == 1)
method = "Absolute approximate error";
elseif(flag == 2)
method = "Absolute relative approximate error";
else
method = "True absolute error";
end
disp("Method used: " + method);
disp("Brackets: " + sx1 + " and " + sx2);
disp("The root is " + root);
disp("Iterations: " + counter);
disp(" ");
Ввод / вывод:
>> f = @(x) 2*sin(x) - exp(x)/4-1;
>> [v,c] = FalsePosition(f,-7,-5,10^-6,2)
Method used: Absolute relative approximate error
Brackets: -7 and -5
The root is -5.7591
Iterations: 100
v =
-5.7591
c =
100
где v - корень, а c - итерации цикла while