optimization problem using fminunc

조회 수: 5 (최근 30일)
Graham
Graham 2013년 8월 14일
댓글: DURGA PRAJAPATI 2021년 9월 5일
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
Graham
Graham 2013년 8월 14일
any help with this please?
DURGA PRAJAPATI
DURGA PRAJAPATI 2021년 9월 5일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2013년 8월 14일
편집: Matt J 2013년 8월 14일
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
Graham
Graham 2013년 8월 15일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Weiss
Alan Weiss 2013년 8월 14일
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!

Translated by