fminsearch for multiple variables. HELP!!!

13 Ansichten (letzte 30 Tage)
Jo 5
Jo 5 am 29 Aug. 2017
Bearbeitet: Jo 5 am 28 Sep. 2017
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 Kommentare
Stephen23
Stephen23 am 29 Aug. 2017
"Is fun2 the correct way of doing it?"
Yes
Jo 5
Jo 5 am 29 Aug. 2017
Thanks Stephen. do u know how to solve the Undefined function or variable 'n' and 'm' problem?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 29 Aug. 2017
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 Kommentare
Jo 5
Jo 5 am 29 Aug. 2017
Bearbeitet: Jo 5 am 29 Aug. 2017
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 am 29 Aug. 2017
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);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by