필터 지우기
필터 지우기

Using fzero with multiple parameters

조회 수: 90 (최근 30일)
Dave
Dave 2013년 1월 20일
댓글: Steven Lord 2023년 6월 16일
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! :)

채택된 답변

Walter Roberson
Walter Roberson 2013년 1월 20일
fzero() is strictly for functions with one parameter. You need fminsearch()
  댓글 수: 8
Robert Buckles
Robert Buckles 2023년 6월 15일
편집: Robert Buckles 2023년 6월 15일
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 2023년 6월 16일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Tobin Fricke
Tobin Fricke 2019년 1월 12일
Use fsolve from the Optimization Toolbox.
  댓글 수: 1
Robert Buckles
Robert Buckles 2023년 6월 15일
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...

댓글을 달려면 로그인하십시오.


Robert Buckles
Robert Buckles 2021년 3월 6일
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
Walter Roberson
Walter Roberson 2021년 3월 6일
Yup, but
x0= [15;12;450;12];
from the original question is not a scalar.
John D'Errico
John D'Errico 2021년 3월 6일
편집: John D'Errico 2021년 3월 6일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Optimization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by