fminsearch for multiple variables. HELP!!!

16 ビュー (過去 30 日間)
Jo 5
Jo 5 2017 年 8 月 29 日
編集済み: Jo 5 2017 年 9 月 28 日
Hi all, I wanted to get the values for 2 parameters(n & m) by maximizing the function 'fun' with fminsearch to get the values for n and m but I keep on getting the error Undefined function or variable 'n' and 'm'. Can anyone suggest a solution?
and I am not sure how to group n & m into a single vector so that fminsearch can be applied. Is 'fun2' the correct way of doing it? Thank you in adv!!!!
  2 件のコメント
Stephen23
Stephen23 2017 年 8 月 29 日
"Is fun2 the correct way of doing it?"
Yes
Jo 5
Jo 5 2017 年 8 月 29 日
Thanks Stephen. do u know how to solve the Undefined function or variable 'n' and 'm' problem?

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2017 年 8 月 29 日
You have not defined ‘n’ and ‘m’ prior to this assignment:
y = ln(dAdT./((1-A).^n.*A.^m) );
that also is coded incorrectly. Use ‘log’, not ‘ln’ to calculate the natural logarithm:
y = log(dAdT./((1-A).^n.*A.^m));
I do not know what you are doing, so I cannot offer specific code to correct the error.
  5 件のコメント
Jo 5
Jo 5 2017 年 8 月 29 日
編集済み: Jo 5 2017 年 8 月 29 日
Hi Walter, I am sorry I have very limited knowledge of MATLAB. I have tried to use the codes but it showed error:
Not enough input arguments. Error in obj (line 8) n = nm(1);
Could you please tell me what went wrong? I have multiply the objective function with -1 because I wanted to maximize the objective function. Is that the right way? Thank you so much for your help.
function f = obj(nm, A, dA, T)
data = evalin('base','data');
T = data{1,1}(:,1)+273.15;
A = data{1,1}(:,5);
dA = data{1,1}(:,6);
N =length(T);
n = nm(1);
m = nm(2);
x = 1./T;
y = ln(dA ./ ((1-A).^n .* A.^m) );
xy = x .* y;
sum_x = sum(x);
sum_y = sum(y);
sum_xy = sum(xy);
sum_x2 = sum(x.^2);
sum_y2 = sum(y.^2);
f = -1*(N*sum_xy-sum_x*sum_y)/(((N*sum_x2-(sum_x)^2)*(N*sum_y2-(sum_y)^2))^0.5);
nm0 = randn(1, 2);
best_nm = fminsearch( @(nm) obj(nm, A, dA, T), nm0 );
Walter Roberson
Walter Roberson 2017 年 8 月 29 日
You need to break the code into two parts. One of the parts just evaluates the function given a particular nm pair, and given A, dA, and T. The other part, in a different function or a different file, has to read in or construct the original A, dA, and T, and then call
nm0 = randn(1, 2);
best_nm = fminsearch( @(nm) obj(nm, A, dA, T), nm0 );
For example,
function best_nm = run_the_optimization
data = evalin('base', 'data');
T = data{1,1}(:,1)+273.15;
A = data{1,1}(:,5);
dA = data{1,1}(:,6);
nm0 = randn(1, 2);
best_nm = fminsearch( @(nm) obj(nm, A, dA, T), nm0 );
function f = obj(nm, A, dA, T)
n = nm(1);
m = nm(2);
x = 1./T;
y = ln(dA ./ ((1-A).^n .* A.^m) );
xy = x .* y;
sum_x = sum(x);
sum_y = sum(y);
sum_xy = sum(xy);
sum_x2 = sum(x.^2);
sum_y2 = sum(y.^2);
f = -1*(N*sum_xy-sum_x*sum_y)/(((N*sum_x2-(sum_x)^2)*(N*sum_y2-(sum_y)^2))^0.5);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by