Using fzero with multiple parameters

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

3 votos

fzero() is strictly for functions with one parameter. You need fminsearch()

8 comentarios

Dave
Dave el 24 de En. de 2013
Thanks so much, I'm sorry for the few days reply. I didn't get the answer I wanted, but it didn't give me errors. I'll mess with the optimset options to try and get my code to spit out proper numbers. Thank you!!
Hossein
Hossein el 13 de Jun. de 2016
Hi, could you solve the problem? I have the same problem as you, can you help?
Ming Xia
Ming Xia el 21 de Ag. de 2018
Thx for your helpful answer! I have solve the problem encountered according to your answer.
Actually, I think Frick is just trying to pass extra arguments along to the function, which is indeed allowed by fzero. I've noticed others with a similar problem, and they just don't know how to ask, and get misunderstood as multiparameter optimization. The extra arguments come after the options argument, but you cannot leave out the option. Use [ ] for the options if none are desired.
Here's an example for finding the analytical peak of the Poisson distribution. There is one main argument for spanning the zero finding range, and one extra argument, mu, for defining the distribution.
function p = PoissonPeak(mu)
fun = @(x,mu) log(mu)-psi(x+1);
xo = [0,10*mu];
x = fzero(fun,xo,[],mu);
y = mu^x*exp(-mu)/gamma(x+1);
p = [x, y];
end
PoissonPeak(3)
ans =
2.4863 0.2335
@Robert Buckles Who? The person who posted this Question is Dave. Tobin Fricke posted an Answer to this question suggesting using fsolve().
If you are referring to Dave, then I have to disagree. Dave posted
x0= [15;12;450;12];
and
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);
which is obviously a function of four inputs togther with some parameters. Passing in the m, L, T1, k, h, P as extra parameters would be a very reasonable thing to do, but it is still a system with four inputs and fzero() can only be used for systems with one input.
Robert buckles flagged this answer, thinking it wrong. However, Walter is correct. fzero is not the correct tool. The question clearly askes about how to solve a problem with vector input x, where x is used like this:
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);
So we see a system of nonlinear equations in FOUR parameters.
fsolve is probably the correct tool, but fminsearch can also be used.
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

1 voto

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

0 votos

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

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 Programming en Centro de ayuda y File Exchange.

Preguntada:

el 20 de En. de 2013

Comentada:

el 16 de Jun. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by