newton-raphson please help
2 次查看(过去 30 天)
显示 更早的评论
i want to find roots of f(x)=x(x-1)e^x by using newton raphson but instead of finding df/dx by using code , i want to use central numerical differentiation which is that f_diff(x)=(f(x+h)-f(x-h))/2*h; and h is any variable but smaller is better such as 10^-6 . and i want write each of them as a function file then call just newton-raphson formule and find root.
please help me
0 个评论
回答(3 个)
Mischa Kim
2014-1-18
Try:
function [x, fx] = my_NR(my_tol, h, x0)
x = x0;
while (abs(f(x)) > my_tol)
dx = f(x)/df(x, h);
x = x - dx;
end
fx = f(x);
end
function f_val = f(x)
f_val = x*(x - 1)*exp(x);
end
function df_val = df(x, h)
df_val = (f(x + h) - f(x - h))/(2*h);
end
1 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!