optimization problem using fminunc

5 views (last 30 days)
Graham
Graham on 14 Aug 2013
Commented: DURGA PRAJAPATI on 5 Sep 2021
Hi,
I am trying to do a parameter estimation on an experimental data set using matlab for the 1st time. I am confused how to do this exactly. I have a model built in one mfile which outputs a result for a random set of parameters [k, l, b, g] and my objective function to be minimized in another file.
I have been calling fminunc at the bottom of my model file and sending the output of the model and the experimental data to the objective function. A part of my code is below. I left out the body of it,"", below.
Model file:
global kg gp bp kb ;
k = 1; l=5; b=4; g=10; % Assign parameter values
x0 = [k,l,b,g];
"
"
"
"
nm=nl_mat';
[x,fval] = fminunc(@globalfun,nm)
_
My objective function is the following:
function f=globalfun(nm)
ydata=xlsread('graham.xlsx','Sheet12','a3:d102');
r1=(ydata-nm).^2;
r11=sum(sum(r1));
f=r11;
return
So when I execute my model file it seems the solver is trying to get to a solution. in the end it says that the initial point is a local minimum. Im not sure whether I have it laid out correctly or if the solver just wont work. I want it to back out new values for [k, l, b, g] that can enable my model data to fit my experimental data. any help would be appreciated.
Thanks
  2 Comments
Graham
Graham on 14 Aug 2013
any help with this please?
DURGA PRAJAPATI
DURGA PRAJAPATI on 5 Sep 2021
function obj = objFunction(q)
data=readtable('solu6_deleted few rows VALUE-Mat1');
A=table2array(data);
yex=A(:,33);
x1=A(:,1);
x2=A(:,2);
f1=A(:,4);
f2=A(:,5);
T=A(:,3);
size(T)
yp=@prediction;
obj=norm(yex(':') - yp(':')).^2 ;
-----------------------
function yp = prediction(q,f1,f2,x1,x2,T)
% Unknown parameter
Q1=q(1);
Q2=q(2);
Q3=q(3);
yp=f1.*log(x1)+f2.*log(x2)+(Q1+Q2.*(f1-f2)+Q3.*(f1-f2).^2).*f1.*f2.*T./298;
can anyone please help me ...i am not able to run this code ...basically i want the minimize the obj value by optimizing the Q1,Q2,Q3 .
if any additional data required i will share.
I am new to this software ..so please help me

Sign in to comment.

Accepted Answer

Matt J
Matt J on 14 Aug 2013
Edited: Matt J on 14 Aug 2013
A part of my code is below. I left out the body of it,"", below.
All of the important information appears to be in the part you left out!
It's hard to help you , because it's not clear which parameters you're trying to solve for. If you're trying to minimize globalfun with respect to [k,l,b,g], and nm is just an intermediate quantity depending on [k,l,b,g] then globalfun() should be written as a function of [k,l,b,g] instead of as a function of nm.
In other words, you should be passing the vector x=[k,l,b,g] to globalfun(x), and computing nm inside it:
function f=globalfun(x)
k=x(1);
l=x(2);
b=x(3);
g=x(4);
nm=... %something depending on k,l,b,g
ydata=xlsread('graham.xlsx','Sheet12','a3:d102');
f=norm(ydata(:) - nm(:)).^2 ;
  1 Comment
Graham
Graham on 15 Aug 2013
Hi guys,
thanks for the reply. I did as you said Matt and it works fine. I was confused about how to link up my model with the objective function but having it running inside the function itself works.
Thanks

Sign in to comment.

More Answers (1)

Alan Weiss
Alan Weiss on 14 Aug 2013
Perhaps examining a similar example would be useful.
Also, it is bad practice to read in data during an objective function evaluation. Read the data in once, and pass it to your objective function via an anonymous function or nested function. And generally speaking, don't use global variables.
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!