lsqcurvefit fitting code debug

2 views (last 30 days)
Carol
Carol on 14 Oct 2013
Edited: Matt J on 14 Oct 2013
I have been tried a long time but I still fail to debug the lsqcurvefit function. Here is my m file. When I run these codes, the matlab system give me the messege as follows:
Error using feval
Cannot find an exact (case-sensitive) match for 'Model'
Error in lsqcurvefit (line 195)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Error in_fitting (line 15)
[x,resnorm]=lsqcurvefit(@Model,x,t,T,[0 0 0 0],[1 1 1 100]);
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.
Hope someone could give me some instructions. Thanks!
-------------------------------------------------------------------------------------
m file 1 (function)
function p=model(P)
clc;clear all;
dt=2.5;
H=0.3;
load A.txt;
load T.txt;
F=P(1);
e=P(2);
K=P(3);
b=P(4);
%set Impulse Response Fuction
alpha=(F+K)./b;
beta=((1-p)*(1-H)*K)./b.*e;
gamma=K./(1-p);
theta=(K.*(1-H))./e;
a=1/2*(theta+alpha+sqrt((theta)^2+(alpha)^2-2*theta*alpha+4*gamma*beta));
b=1/2*(theta+alpha-sqrt((theta)^2+(alpha)^2-2*theta*alpha+4*gamma*beta));
IRF=((a-theta-K/b)*exp(-at)+(-b+theta+K/b)*exp(-bt))/(a-b);
---------------------------------------------------------------
m file 2 (lscurvefit fitting)
function bfa=twoCXM_fitting(x)
H=0.3;
dt=2.5;
t=0:dt:59*dt; %second
x1=input('please input the initial guess K value:');
x2=input('please input the initial guess e value:');
x3=input('please input the initial guess p value:');
x4=input('please input the initial guess F value:');
x=[x1/60 x2 x3 x4/60]; %initial guess value
[x,resnorm]=lsqcurvefit(@Model,x,t,T,[0 0 0 0],[1 1 1 100]);
%bfa=find(resnorm(:) == min(resnorm(:)));
  2 Comments
Matt J
Matt J on 14 Oct 2013
Please use the toolbar button
to format your code in a font distinguishable from your text.
Jonathan LeSage
Jonathan LeSage on 14 Oct 2013
I am taking a look at your code, but I need a few clarifications.
  1. To use the lsqcurvefit function, you need to provide a model with inputs and ouputs over which to optimize. In your case, you have the "Model" function. What are the inputs and outputs of this function supposed to be?
  2. What is the lower-case p in the Model function used for?
  3. What values are being loaded from A.txt and T.txt?
Thanks.

Sign in to comment.

Answers (1)

Matt J
Matt J on 14 Oct 2013
Edited: Matt J on 14 Oct 2013
The error is coming from the fact that your file is called model.m (lower case "m") but you are telling lsqcurvefit that it is called Model.m (upper case "M").
As Jonathan points out, you will also have problems from not assigning values to the output variable "p". Perhaps p was supposed to be IRF instead.

Community Treasure Hunt

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

Start Hunting!