How to find x values where y= 0 using newtons method and secant method?

5 次查看(过去 30 天)
I´ve drawn up newtons method in an easy function as:
function [x, iter] = newton(f, df, x)
maxiter = 1000;
iter = 0;
d = 100;
while abs(d) > sqrt(eps)*abs(x) && iter < maxiter
iter = iter + 1;
d = -f(x)/df(x);
x = x + d;
end
if iter == maxiter
disp('Maximum number of iterations performed. Answer is probably wrong.')
end
end
The secant method i've drawn up like this:
function [xout,iter] = sekant(f,x,x1)
iter = 0;
maxiter = 1000;
while abs(x1-x) > 1e-6
iter = iter + 1;
d = (f(x1) - f(x))./(x1-x);
xout = (x1-f(x1)./d);
x = x1;
x1 = xout;
if iter == maxiter
disp('No points of zeros found')
break
end
end
end
Here is my problem, I wish to put both of them together to find points where y = 0 for several functions as polynomials and differential eqations. My thought process has been that i need to find an interval of x values where these methods are used and with them make the inteval smaller untill im close enough to find the "approximation" or actual value of 0.

采纳的回答

Nipun
Nipun 2024-5-17
Hi AGCM,
I understand that you are trying to utilize both Newton's method and the secant method in MATLAB to find the zeros of several functions, such as polynomials and differential equations, by narrowing down intervals until you are close enough to an approximation or the actual value of zero. You aim to start with a broader interval and make it smaller using these methods to find the roots more accurately.
Here is a concise MATLAB approach to achieve your goal:
function root = findRootCombined(f, df, x0, x1)
% Use the secant method to get an initial approximation of the root
[x_approx, iter_secant] = sekant(f, x0, x1);
disp(['Secant method used ', num2str(iter_secant), ' iterations. Approximation: ', num2str(x_approx)]);
% Refine the approximation using Newton's method
[root, iter_newton] = newton(f, df, x_approx);
if iter_newton == 1000
disp('Newton method reached maximum iterations. Result may be inaccurate.');
else
disp(['Newton method refined the root in ', num2str(iter_newton), ' iterations. Final Root: ', num2str(root)]);
end
end
% Secant method function
function [xout, iter] = sekant(f, x, x1)
iter = 0;
maxiter = 1000;
while abs(x1-x) > 1e-6 && iter < maxiter
iter = iter + 1;
d = (f(x1) - f(x)) / (x1-x);
xout = x1 - f(x1) / d;
x = x1;
x1 = xout;
end
end
% Newton's method function
function [x, iter] = newton(f, df, x)
maxiter = 1000;
iter = 0;
d = 100;
while abs(d) > sqrt(eps) * abs(x) && iter < maxiter
iter = iter + 1;
d = -f(x) / df(x);
x = x + d;
end
end
This combined approach starts with the secant method to find an initial approximation of the root within a given interval (x0 to x1). It then uses Newton's method, starting from this approximation, to refine the search and find the root more accurately. This strategy leverages the strengths of both methods: the secant method's ability to work without the derivative and its broader convergence characteristics, followed by Newton's method's rapid convergence near the root.
Make sure to replace f and df with your specific function and its derivative when calling findRootCombined. The initial guesses x0 and x1 should be chosen based on your knowledge of the function's behavior or through preliminary analysis.
Hope this helps.
Regards,
Nipun

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by