This is an iterative program, but the result is wrong. Why?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
这是一个迭代法的程序,但结果是错误的,这是为什么呢?
2 Kommentare
VBBV
am 19 Mär. 2024
Bearbeitet: VBBV
am 19 Mär. 2024
Hi @希媛 the comparison of output values from the function based on some inputs is not a correct way to test the program correctness since sin & cos functions exhibit sign changes according to different values of inputs
g = @(x) cos(x);
p = 20; % initial value
xc = fpi(g,p,1e-6)
cos(g(p))
function xc = fpi(g,x0,tol)
x(1) = g(x0); % <<<
x(2) = g(x(1));
i = 1;
while abs(sign(i+1)-sign(x(i))) > tol
x(i+2) = g(x(i+1));
i=i+1;
end
xc = x(end);
end
Siehe auch
Kategorien
Mehr zu Outputs finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!