As Redes Industriais
Por: helalbr • 2/6/2019 • Trabalho acadêmico • 349 Palavras (2 Páginas) • 158 Visualizações
f=@(x) 6-2*(x-cos(x)+0.1);
%f=@(x) x^2+10*x+16
fplot(f, [1 2])
grid
% The plot shows, that the solution is between x = 2 and x = 3.
a = 2;
b = 3;
n = 200000; % max number of iterations
tol = 0.0; % tolerance
fa = f(a);
fb = f(b);
if fa*fb > 0
disp('Error: The function has the same sign at points a and b.')
else
disp('Iteration a b p f(p)')
for i = 1:n
p = (a + b)/2;
toli = (b - a)/2;
fp = f(p);
fprintf('%5i %13.6f %9.6f %9.6f %10.6f\n',i,a,b,p,fp)
if fp == 0
fprintf('An exact solution x=%11.6f was found',p)
break
end
if toli < tol
break
end
if i == n
fprintf('Solution was not obtained in %i iteraticns',n)
break
end
if f(a)*fp < 0
b = p;
else
a = p;
end
end
end
% The output shows that the solution with the desired tolerance
% is obtained in the 10th iteration.
...