MATLAB: While Loop won’t work on iteration problem

basiciterationwhile loop

Hi i don't seem to understand why my while loop doesn't work for my function i've done everything in logical steps:
function [x]=reynolds(Re,x0)
%x=zeros(1000,1);
x=(2.5*log(Re*(x0)^0.5)+0.3)^-2;
n=1;
x(1)=x0;
while abs(x(n+1)-x(n))>1e-6
x(n+1)=x(n);
x(n+1)=(2.5*log(Re*x(n)^0.5)+0.3)^-2;
n=n+1;
end
The function requires two inputs with one being an initial guess, but an error pops up saying that it can't find x(3) mea

Best Answer

Edit
function x=reynolds(Re,x0)
x=zeros(1,100)
n=1
x(n)=x0;
x(n+1)=(2.5*log(Re*(x0)^0.5)+0.3)^-2;
while abs(x(n+1)-x(n))>1e-6
n=n+1;
x(n+1)=(2.5*log(Re*x(n)^0.5)+0.3)^-2
end
x=x(1:n+1)
Related Question