# 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)
Brendan on 6 Sep 2023
%% False-position root finder
function FP_iteration_root_finder
clear
clc
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
B=linspace(0,10);
y=the_function(B);
plot(B,y)
xlabel('B')
ylabel('y')
legend('Function','FontSize',12)
end
root = Bi %report root
end
%% function for which root is to be found
function [y] = the_function(B)
y = (cos(B).*cosh(B))+ 1;
end

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;
end