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! :)
採用された回答
その他の回答 (2 件)
Tobin Fricke
2019 年 1 月 12 日
1 投票
1 件のコメント
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
2021 年 3 月 6 日
0 投票
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
2021 年 3 月 6 日
Yup, but
x0= [15;12;450;12];
from the original question is not a scalar.
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.
カテゴリ
ヘルプ センター および File Exchange で Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!