MATLAB: This functions doesn’t work on matlab coder

matlab coder

function [Rs] = Racinew(s)
imax=25;
emax=0.00001;
x=s;
i=1;
if s<0
Rs=nan;
disp('le paramètre entrée s doit être positif')
end
while i~=imax
Rs=x-(x^2-s)/(2*x);
E=abs((Rs-x)/x);
x=Rs;
i=i+1;
if E<emax
break
end
end
if E>emax
Rs=nan;
disp('Le nombre de 25 itération a ete atteint sans avoir trouvé la solution')
end
It is saying that variable 'E' is not fully defined on some execution paths (line 19 (E>emax))
How can I resolve this problem? Thank you

Best Answer

Before the "for i" loop use
E = emax;
to initialize to some value. MATLAB is not able to quite notice that your "while" loop will always execute at least once.
Also, take another look at your lines
Rs=x-(x^2-s)/(2*x);
E=abs((Rs-x)/x);
Those lines would have difficulty if x is not a scalar. Your code itself does not guarantee it, as you assign
x = s;
but what if the parameter is is not a scalar? It would be more robust if you assigned
s = s(1,1);
before the assignment x = s;