While Loop won't work on iteration problem

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

2 commentaires

How did you call your function with values of Re and x0
A K
A K le 2 Nov 2013
I called it from the command window such as, reynolds(5000,3) 5000 is Re and x0=3

Connectez-vous pour commenter.

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 2 Nov 2013
Modifié(e) : Azzi Abdelmalek le 2 Nov 2013
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)

9 commentaires

A K
A K le 2 Nov 2013
Modifié(e) : A K le 2 Nov 2013
Is there a way to get the code to work without the matrix?
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
x(n+1)=x(n);
n=n+1;
x(n+1)=(2.5*log(Re*x(n)^0.5)+0.3)^-2;
end
like this? It seems to go in an infinite loop
which matrix?
A K
A K le 2 Nov 2013
x=zeros(1000,1)
I don't know what your code is doing, can you explain?
A K
A K le 2 Nov 2013
It's doing an iteration, so I guess the first value of x and it runs it through a while loop until the difference between x(n) and x(n-1) is less than 1e-6
It seems that your code is doing the same thing during all the time.
Azzi Abdelmalek
Azzi Abdelmalek le 2 Nov 2013
Modifié(e) : Azzi Abdelmalek le 2 Nov 2013
Try this
function x=reynolds(Re,x0)
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
I kind of fixed it but can't seem to stop the vector in the command window
function [x]=reynolds(Re,x0)
n=1;
x=zeros(100,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-8
n=n+1;
x(n+1)=(2.5*log(Re*x(n)^0.5)+0.3)^-2;
end
if you scroll up the command window you can see the final result but it shows all the 0's after.
Azzi Abdelmalek
Azzi Abdelmalek le 2 Nov 2013
Modifié(e) : Azzi Abdelmalek le 2 Nov 2013
Look at my previous comment. Remove x=zeros(100,1) or add after the while loop
x=x(1:n+1)

Connectez-vous pour commenter.

Plus de réponses (1)

Assuming that by 'log' you mean the natural logarithm and not the logarithm-base-ten, then if you define the variable w as:
w = 1/2.5/sqrt(x)
your equation to be solved can be expressed as:
w*exp(w) = Re/2.5*exp(.3/2.5)
If you have the 'lambertw' function in your system, you can solve for this directly without doing iteration:
Re = 5000;
w = lambertw(Re*exp(.3/2.5)/2.5);
x = (2.5*w)^(-2);
Your initial estimate of 3 is very far from the actual solution which is in fact:
x = 0.00453573902634
and this may account for the trouble you experienced.
There are also two other methods you could use: 1) the Newton-Raphson method which requires a derivative, and the matlab function 'fzero'. Either one would surely be superior to the method you are using here. In some circumstances your method might not even converge to a solution at all.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by