matlab - while loop until values converge -


i need solve following equation:

old_val = 0.4*u_z/(log(5/new_val)); 

where

new_val = 0.11*1.5e-5./old_val; 

in order calculate new_val , old_val need write loop calculates new_val , old_val , takes true value of new_val when new_val 0.001% of previous new_val.

i have though using while loop this, think might work. bit confused, though, on how start while loop, should have:

while abs((new_val(i) - val_prev(i))) > 0.000001          old_val = 0.4*w(i)/(log(5/new_val));     dummy = new_val(i);     new_val = 0.11*1.5e-5./old_val;     val_prev(i) = dummy; end 

or

    while abs((new_val(i) - val_prev(i))) / abs(val_prev(i)) > 0.000001          old_val = 0.4*w(i)/(log(5/new_val));     dummy = new_val(i);     new_val = 0.11*1.5e-5./old_val;     val_prev(i) = dummy; end 

where

val_prev = new_val*1.1;  

which used initiate iteration. while loop used continue running loop until new_val , val_prev within 0.001% of each other.

if understand correctly, want solve equation x:

x =  0.4*u_z/log( 5/(0.11*1.5e-5./x) ) 

with u_z constant. if indeed want do, i'd re-write as

x * log( 5*x/1.65e-6 ) - 0.4*u_z == 0 

and solve this:

x_sol = fzero(@(x) x .* log( 5*x/1.65e-6 ) - 0.4*u_z, 1) 

or, if insist, use own newton-raphson scheme:

fx   = @(x) x .* log( 5*x/1.65e-6 ) - 0.4*u_z dfdx = @(x) log(x) + 15.9242;  x = 1; f = fx(x); while abs(f) > 1e-6        x = x-f/dfdx(x);     f = fx(x); end 

but i'm not sure if want...


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -