TU Wien:Computernumerik VU (Schranz-Kirlinger)/Übungen SS16/Beispiel 23
Zur Navigation springen
Zur Suche springen
Lösung[Bearbeiten | Quelltext bearbeiten]
Matlab Code
function Bsp3 bisektion(0.1, 1.9, 0.000001); sekanten(0.1, 1.9, 0.000001); end function [error] = bisektion(la, rb, eps) error = []; while(abs(f(rb) - f(la)) >= eps) %error = cat(1, error, f(rb) - f(la)); fprintf('%.15f\n', f(rb) - f(la)); m = abs((rb - la)/2); if(sign(f(la)) ~= sign(f(rb - m))) rb = rb - m; error = cat(1, error, rb); elseif (sign(f(la + m)) ~= sign(f(rb))) la = la + m; error = cat(1, error, la); else break; end end fprintf('%.5f %.5f %.5f\n', la, rb, f(rb) - f(la)); end function [error] = sekanten(x0, x1, eps) xn = 0; error = []; while(1) xn = x1 - ((x1 - x0)/(f(x1) - f(x0))) * f(x1); fprintf('%.10f', xn); if(abs(f(xn)) <= eps) break; end error = cat(1, error, xn); x0 = x1; x1 = xn; end disp(xn); end function [fn] = f(x) fn = x^3 - 3 * x^2 + 2 * x; end