0
votes

Basically I would like to use the fsolve command in order to find the roots of an equation. I think I should create a function handle that evaluates this equation in the form "right hand side - left hand side =0", but I've been struggling to make this work. Does anyone know how to do this?

The equation itself is 1/sqrt(f) = -1.74log((1.254/((1.27310^8)sqrt(f)))+((110^-3)/3.708)). So I would like to find the point of intersection of the left and right side by solving for 1/sqrt(f)+(1.74log((1.254/((1.27310^8)sqrt(f)))+((110^-3)/3.708))) = 0 using fsolve.

Thanks a lot!

The code so far (not working at all)

f = @(x) friction(x,rho,mu,e,D,Q, tol, maxIter) ;

xguess = [0, 1];

sol = fsolve(x, xguess ) ;


function y = friction(x,rho,mu,e,D,Q, tol, maxIter)

D = 0.1; 
L = 100 
rho = 1000; 
mu = 0.001; 
e = 0.0001; 
Q = 0.01; 
U = (4*Q)/(pi*D^2); 
Re = (rho*U*D)/mu ; 


y = (1/sqrt(x))-(-1.74*log((1.254/(Re*sqrt(x)))+((e/D)/3.708)))

end

Error message:

Error using lsqfcnchk (line 80) FUN must be a function, a valid character vector expression, or an inline function object.

Error in fsolve (line 238) funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);

Error in Untitled (line 6) sol = fsolve(x, xguess ) ;

2
As a concept that sounds like it should work. The final equation looks to have a sign error for the third term (should be minus). Post your code and the error you're getting so we can reproduce it. - aosborne

2 Answers

0
votes
opt = optimset('Display', 'Iter');
sol = fsolve(@(x) friction(x), 1, opt);

function y = friction(x)

  D = 0.1; 
  L = 100; % note -- unused 
  rho = 1000; 
  mu = 0.001; 
  e = 0.0001; 
  Q = 0.01; 
  U = (4*Q)/(pi*D^2); 
  Re = (rho*U*D)/mu ; 
  
  y = (1/sqrt(x))-(-1.74*log((1.254/(Re*sqrt(x)))+((e/D)/3.708)));

end

sol = 0.0054

0
votes

the first argument of fsolve should be the function not variable. Replace:

sol = fsolve(x, xguess );

with

 sol = fsolve(f, xguess );

And define Rho, mu, e etc before you define f (not inside the friction function).