Newton Method - no of iterations
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello,
I am new to Matlab and have a school project.
I have the function and derivative
f = @(x) -0.06667*x- 1.1167+ (1/V^2)*(0.1256*x^2-0.1589*x+0.05139- V)^2
df = @(x) -0.06667+ 2*(1/v^2)*(0.1256*x^2- 0.1589*x+ 0.05139- V)*(0.1256*x- 0.1589)
For initial point x0 = 1, I would like to find the number of iteration after I obtain the value 0.00168 (0.00169) for V, through Newton Method.
Could you please help me with the code, or at least advice on the steps to follow?
Thank you,
Alina!
1 comentario
Torsten
el 9 de En. de 2024
Editada: Torsten
el 10 de En. de 2024
If you are not prepared to code Newton's method on your own: there are many implementations on the File Exchange.
Google
File exchange & MATLAB & Newton's method
and you will find many, many codes - some of them good, some of them bad. Give it a try.
If you have problems with MATLAB itself, invest two hours of your time and visit an introductory course online free of costs:
Respuestas (1)
Hassaan
el 10 de En. de 2024
Editada: Torsten
el 10 de En. de 2024
- Define the function f(x) and its derivative f′(x).
- Choose an initial guess 0x0.
- Apply Newton's Method iteratively to find the root of f(x) until the value reaches the desired threshold.
% Define the function and its derivative as function handles
f = @(x) -0.66667*x - 1.1167 - (1/sqrt(x))*(0.1256*x^2 - 0.1589*x + 0.5139 - x^2);
df = @(x) -0.66667 + 2*(1/sqrt(x))*(0.1256*x^2 - 0.1589*x + 0.5139) - (1/x)*(0.1256*x - 0.1589);
% Initial guess
x0 = 1;
% Desired threshold for the function value
threshold = 0.00168;
% Initialize iteration counter
iter = 0;
% Newton's Method iteration
while true
% Calculate the function value and its derivative at the current guess
fx = f(x0);
dfx = df(x0);
% Check if the current function value is close enough to the desired threshold
if abs(fx - threshold) < 1e-5 % tolerance set to 0.00001 for convergence
break;
end
% Update the guess using Newton's Method formula
x0 = x0 - fx / dfx;
% Increment iteration counter
iter = iter + 1;
% Optional: Break if too many iterations to avoid infinite loop
if iter > 10000
error('Newton''s Method did not converge within 10000 iterations');
end
end
% Display the result
fprintf('The number of iterations to reach the threshold is: %d\n', iter);
fprintf('The root found by Newton''s Method is: %.5f\n', x0);
This code uses an anonymous function for f(x) and df(x), and a while loop for the iterative process. It stops iterating when the function value is sufficiently close to the threshold. It also includes a safety check to avoid an infinite loop by limiting the maximum number of iterations.
Remember to adjust the convergence tolerance (1e-5 in this case) if needed. This tolerance determines how close to the threshold the function value must be to stop iterating. Also, ensure that the derivative function df is correctly implemented as per your derivative formula.
Note
- you can find the code for Newton's method on File-Exchange and/or google.
- Take the provided code snippet as reference only and its better you understand and implement it your self.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 comentarios
Ver también
Categorías
Más información sobre Parallel Computing Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!