fminsearch for multiple variables. HELP!!!
13 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 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 Comments
Walter Roberson
on 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);
More Answers (0)
See Also
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!