フィルターのクリア

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 Center および File ExchangeOptimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by