Using fzero with multiple parameters

193 visualizaciones (últimos 30 días)
Dave
Dave el 20 de En. de 2013
Comentada: Steven Lord el 16 de Jun. de 2023
Having trouble debugging error...
I have 2 scripts:
-function
-script that calls function for use
I'm pasting both scripts along with the error I received and where exactly I think the problem is (according to the error).
Function:
function F=EXPLICITFUNCTION(x)
F = x(1) + x(2) + x(3) - T2;
x(1)*exp(m*L) + x(2)*exp(-m*L) + x(3) - T1;
-k*(x(1)*m*exp(m*L)-x(2)*m*exp(m*L)) + k*(m*(x(1)-x(2))) - x(4);
P*h*((x(1)*(exp(L*m) - 1))/m - (x(2)*(1/exp(L*m) - 1))/m) - x(4);
end
Script:
%______________________________
clear all
clc
%______________________________
T2 = 375; %........................K
T1 = 450; %........................K
D = 0.003; %........................m
P = pi*D; %........................m
A_Cond = (pi/4)*(0.003-0.002); %.........m^2
h = 257.799; %......................W/(m^2*K)
k = 70; %......................W/(m*K)
L = 0.03; %......................m
m = sqrt((h*P)/(k*A_Cond)); %.......(1/m)
x0= [15;12;450;12];
fzero(@(x) MMAE322SUPERFUNCTION(x, T2, T1, m, L, k, P, h), x0)
The error I receive is:
-Error using fzero (line 413)
Second argument must be a scalar or vector of length 2.
-Error in MMAE322SCRIPT (line 19)
fzero(@(x) MMAE322SUPERFUNCTION(x, T2, T1, m, L, k, P, h), x0)
Somehow I think it's because of my guess, x0. I need 4 guesses, one for each unknown variable, but I don't know how to interpret the error. I tried for the sake of debugging by erasing 2 guess in my x0 and of course that didn't work..
I've done this before with a single parameter and single guess but anymore than 1 parameter I get a similar error.
Any help is greatly appreciated! :)

Respuesta aceptada

Walter Roberson
Walter Roberson el 20 de En. de 2013
fzero() is strictly for functions with one parameter. You need fminsearch()
  8 comentarios
Robert Buckles
Robert Buckles el 15 de Jun. de 2023
Editada: Robert Buckles el 15 de Jun. de 2023
Not wrong in the sense of applicability, just incomplete, and wrong to say that fzero cannot accept a function with multiple arguments. fzero can be used, so long as only one argument is being varied at a time. It is a lower-level function.
fminsearch is nice and easy until you encounter a situation where it doesn't converge. Even for a function as simple a poly2 fit, where you want to find the minimum, fminsearch can take you into outer space (and take forever to get there). fzero is more direct as a thresholding function, i.e. where does a function (or gradient of the function in this case) cross through zero? You can specify a [start stop] domain with fzero whereas fminsearch only has a start value, and this is the problem and why it goes crazy sometimes. If you need multiple varied parameters, you evaluate one parameter at a time with fzero and wrap it in a smart hunting routine. Of course we hope for built-in intelligence.
Steven Lord
Steven Lord el 16 de Jun. de 2023
The documentation for fzero states that its first input must be a "Function to solve, specified as a handle to a scalar-valued function or the name of such a function. fun accepts a scalar x and returns a scalar fun(x)." The original question passed in a function that did not satisfy that requirement, requiring passing in a non-scalar vector for x and returning a non-scalar vector as output.
That function can accept additional fixed parameters, but it can only solve a system of one equation in one unknown. To solve a system with multiple equations or multiple unknowns you may wish to switch to the fsolve function in Optimization Toolbox instead as others have mentioned.

Iniciar sesión para comentar.

Más respuestas (2)

Tobin Fricke
Tobin Fricke el 12 de En. de 2019
Use fsolve from the Optimization Toolbox.
  1 comentario
Robert Buckles
Robert Buckles el 15 de Jun. de 2023
I would if I had paid for the toolbox. Even with corporate dollars, I find the kilobuck pricetag for each toolbox a little daunting. Where to shave costs...

Iniciar sesión para comentar.


Robert Buckles
Robert Buckles el 6 de Mzo. de 2021
Extra arguments (other than the one being zeroed) can be passed to the function. The extra arguments come after the options argument, but you cannot leave out the option. Use [ ] for the options if none are desired.
The posted syntax is a bit wrong. Try this:
x0 = 15; % but is preferrable to use a range [0, 30]
fzero(@(x,T2,T1,m,L,k,P,h) MMAE322SUPERFUNCTION(x, T2, T1, m, L, k, P, h), ...
x0, [], T2, T1, m, L, k, P, h);
% would be better to place the other arguments as a vector and pass the vector, parsing it within the function
  2 comentarios
Walter Roberson
Walter Roberson el 6 de Mzo. de 2021
Yup, but
x0= [15;12;450;12];
from the original question is not a scalar.
John D'Errico
John D'Errico el 6 de Mzo. de 2021
Editada: John D'Errico el 6 de Mzo. de 2021
Sadly, this misses the point completely. While it shows how to pass in extra ARGUMENTS, the problem as posted had a vector of unknowns. And while I could probably have flagged your answer as incorrect, I did not.

Iniciar sesión para comentar.

Categorías

Más información sobre Optimization en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by