% Iterative function solver % Pre-filled out for calculating X^2-2 =0 clear clc int = 5; % Size of interval lower = 0; % Intitial lower limit upper = 4; % Intitial lower limit ac = 1*10^-9; % Accuracy noit = 0; % Number of iterations while true x = linspace(lower,upper,int); f = x.^2-2; upper = x(min(find(f>0))); lower = x(min(find(f>0))-1); if abs((f(min(find(f>0)))+f(min(find(f>0))-1)))/2 < ac break end noit = noit +1; end Solution = (upper+lower)/2 noit
Solution = 1.414213562384248 noit = 14