I'm unsure as to why MATLAB is returning an infinite root no matter the initial guess. Can someone help with this?

2 views (last 30 days)
%% False-position root finder
function FP_iteration_root_finder
close all
Bi = 4.8; %initial guess for root
ea = 0.0001; %maximum percent (%) difference btw iterations
error = 100; %initialize error as something large
while error > ea
Bi_plus_1 = the_function(Bi)+Bi; %eval y2 and set equal to y1
error = abs((Bi_plus_1-Bi)/Bi_plus_1)*100; %compare difference
Bi = Bi_plus_1; %update xi
root = Bi %report root
%% function for which root is to be found
function [y] = the_function(B)
y = (cos(B).*cosh(B))+ 1;

Accepted Answer

Nathan Hardenberg
Nathan Hardenberg on 6 Sep 2023
I'm very unsure what algorithm this should be. Can you provide more information. You could do it with something like newton's method, but that's not even close on what you wrote.
To check why it is going to inf please debug your code. You just always take the funciton of x wich is higher than before and then you add Bi. How should this converge? The only reason this leaves the loop is because error gets NaN. Your function returns inf since you provide a really large number (ca. 4.0e+03) into th cosh()-function
The easiest and best way would be to use the fzero()-function, which is implemented by MATLAB. But as practice you can also try to implement newton's method.
fzero(@the_function, 4.8) % MATLABs implemented method
ans = 4.6941
function [y] = the_function(B)
y = (cos(B).*cosh(B))+ 1;

More Answers (0)


Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!