Solving an implicit equation with fzero in a function.

I am trying to solve an implicit equation using the fzero command. The implicit function includes a few variables. When I set up the function and fzero command with those variables hard-coded everything works. I can't figure out how to create a function that includes the fzero command, so I can solve my original equation implicitly for different values of the variables. Here are my two functions...
function y = weir_height2(h,weir_height,total_weir_length,total_weir_discharge)
y = (2/3) * sqrt(2 * 32.174) * (0.611 + (0.08 * (h./weir_height))) * total_weir_length * (h.^1.5) - total_weir_discharge;
and
function y = weir_height_all(weir_height,h0,total_weir_length,total_weir_discharge)
fun = @weir_height2;
x0 = h0;
z = fzero(fun,x0);
end
I keep getting different errors. Can someone please help me fix my two functions?

 Respuesta aceptada

Matt J
Matt J el 14 de Mayo de 2025
fun = @(h) weir_height2(h,weir_height,total_weir_length,total_weir_discharge)

4 comentarios

Thank you! I tried your suggestion (I probably messed it up), but I'm getting an error. Here are my updated functions:
function y = weir_height_all(weir_height2,h0,total_weir_length,total_weir_discharge)
fun = @(h) weir_height2(h,weir_height,total_weir_length,total_weir_discharge);
x0 = h0;
z = fzero(fun,x0);
end
and
function y = weir_height2(h,weir_height,total_weir_length,total_weir_discharge)
y = (2/3) * sqrt(2 * 32.174) * (0.611 + (0.08 * (h./weir_height))) * total_weir_length * (h.^1.5) - total_weir_discharge;
Here's the error message I'm getting now:
>> weir_height_all(0.5,0.1,292,0.524)
Error using fzero>localFirstFcnEval
FZERO cannot continue because user-supplied function_handle ==> @(h)weir_height2(h,weir_height,total_weir_length,total_weir_discharge) failed with the error below.
Not enough input arguments.
Error in fzero (line 305)
fx = localFirstFcnEval(FunFcn,FunFcnIn,x,varargin{:});
Error in weir_height_all (line 7)
z = fzero(fun,x0);
Can you tell what I'm doing wrong?
Your function signature for weir_height_all has a typo in it.
Here's what I get:
weir_height_all(0.5,0.1,292,0.524)
Exiting fzero: aborting search for an interval containing a sign change because complex function value encountered during search. (Function value at -0.028 is -0.524-4.4375i.) Check function or try again with a different starting value.
function y = weir_height_all(weir_height,h0,total_weir_length,total_weir_discharge)
fun = @(h) weir_height2(h,weir_height,total_weir_length,total_weir_discharge);
x0 = h0;
z = fzero(fun,x0);
end
function y = weir_height2(h,weir_height,total_weir_length,total_weir_discharge)
y = (2/3) * sqrt(2 * 32.174) * (0.611 + (0.08 * (h./weir_height))) * total_weir_length * (h.^1.5) - total_weir_discharge;
end
function y = weir_height_all(weir_height2,h0,total_weir_length,total_weir_discharge)
fun = @(h) weir_height2(h,weir_height,total_weir_length,total_weir_discharge);
%snip
I believe you intend for the fun anonymous function you've defined to call the weir_height2 function. But the first input argument to weir_height_all is named weir_height2, so that anonymous function actually tries to index into that variable. Rename the first input argument of weir_height_all to weir_height instead of weir_height2.
Thank you! I fixed the typo you noticed, and everything is working now!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2022b

Preguntada:

el 14 de Mayo de 2025

Comentada:

el 14 de Mayo de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by