You are on page 1of 1

function x = newton (x0, p, f, df)

erro = 1
x = x0
cont=0
while (erro > p)
cont=cont+1
x_antigo = x;
x = x_antigo - f(x_antigo)/df(x_antigo)
erro = abs((x - x_antigo)/x)
end
printf("%d",cont)
endfunction
function y=f(x)
y=x^3-2*exp(-x)
endfunction
function y = df(x)
y = 3*x^2-2*exp(-x)
endfunction
x=0.5:0.1:1
plot (x,f(x))
xgrid
raiz=newton(0.9,10^-4,f,df)

You might also like