fminsearch for multiple variables. HELP!!!

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 commentaires

Stephen23
Stephen23 le 29 Août 2017
"Is fun2 the correct way of doing it?"
Yes
Jo 5
Jo 5 le 29 Août 2017
Thanks Stephen. do u know how to solve the Undefined function or variable 'n' and 'm' problem?

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 29 Août 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 commentaires

Jo 5
Jo 5 le 29 Août 2017
Modifié(e) : Jo 5 le 29 Août 2017
Hi thanks for your reply. I need to find the unknowns, n and m by maximizing r using the equation at the bottom. I have a set of data containing the values of α, dα/dT, and T but I am not sure how to define n and m. Can you kindly suggest a solution? Thank you!!
Walter Roberson
Walter Roberson le 29 Août 2017
You do not define them. You define the objective function I already showed, and you give the objective function to an optimizer. At each stage, the optimizer will supply specific n and m values, and your objective function will evaluate how good those particular values are, and the optimizer will figure out the best combination.
Star Strider
Star Strider le 29 Août 2017
You need to determine ‘m’ and ‘n’ by fitting the first equation in the image (that does not seem to be in your code) to your data. You need to solve it for the appropriate dependent variable in terms of the independent variable. Then code it as an anonymous function.
Maximizing the correlation coefficient is not an optimization strategy I’ve ever encountered before, or that I would recommend.
You can use fminsearch to fit your data, minimizing the norm of the residuals. Do you know the values of ‘alpha’ and other constants in that expression? If you explain what you are doing, I might be able to help you with that.
Jo 5
Jo 5 le 29 Août 2017
Modifié(e) : Jo 5 le 29 Août 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 );
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);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Centre d'aide et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by