Need help with least squares fit (lsqonlin)

2 views (last 30 days)
This should be an easy one for someone I am a matlab newbie (have been for about a year now!) working with pre-existing code. The code was a genetic algorithm that found a bunch of parameters for me, and now I need to see how closely it fits data that I already have without re-running the whole thing, with the ability to manually change some of the parameters(for a sensitivity analysis)
The equations are in a file called WTTNF.m
function dy = WTTNF(~,y,par)
dy = zeros(4,1);
dy(1) = par.alpha*y(1)*(1-y(1)/y(4))-par.beta*y(1)-par.gamma*y(3)*y(1);
dy(2) = par.beta*y(1)+par.zeta*par.gamma*y(3)*y(1)-par.delta*y(2);
dy(3) = par.theta*y(2)+par.epsilon;
dy(4) = par.eta_c*y(3)*y(1)+par.eta_h*y(3)-par.psi*y(4)*(y(4)/par.Ko)^2;
The parameters are stored in par , as par.alpha, par.beta, etc.
The data is stored in a data , with time values and four sets of data points as follows:
>> data.t
ans =
0 1 2 3 6 8
>>data.val
ans =
132 183 358 448 563 526
246 312 741 665 996 1126
193 258 267 295 478 676
185 207 489 574 758 292
Now, this function (WTTNF) works fine within the context of a much larger and more complicated (GA) genetic algorithm. At the end of the GA, it spits out a fitness value, but now I want to start manually messing with some of the parameters (for a sensitivity analysis) and see what fitness value I get, without running the whole GA again. Should be simple
My best guess was to do this,
>> lsqonlin(WTTNF,data)
??? Input argument "par" is undefined.
Error in ==> WTTNF at 7 dy(1) = par.alpha*y(1)*(1-y(1)/y(4))-par.beta*y(1)-par.gamma*y(3)*y(1);
At the time I run this, I do have par in memory with all its associated values for each parameter.
To see what I mean, I can type:
>> par
par =
alpha: 0.1000
beta: 0.0437
gamma: 0.0273
zeta: 0.4935
delta: 6.2036
eta_h: 6.6211
eta_c: 0.0150
theta: 0.0235
epsilon: 5.0568
psi: 0.0243
Ko: 400
>> par.alpha
ans =
0.1000
>> par.beta
ans =
0.0437
This is probably something simple. Thanks for any help.
Regards
-Dave K

Accepted Answer

Matt J
Matt J on 20 May 2015
Edited: Matt J on 20 May 2015
but now I want to start manually messing with some of the parameters (for a sensitivity analysis) and see what fitness value I get, without running the whole GA again.
It can't be deduced from your post what the fitness function optimized by GA actually was, though a good guess is that it was norm(dy) or norm(dy)^2 where dy is the output of WTTNF. In any case, there is no clear reason why you would be using lsqnonlin if all you want to do is re-evaluate the fitness function at a given set of tweaked parameters. Just feed the new parameters to WTTNF again and calculate the fitness based on its output.
  6 Comments
David
David on 28 May 2015
Thanks Matt. I'll give it a try and respond again shortly. Sorry I'm not more fluent or I probably would have listened to you the first time!
David
David on 29 May 2015
I'm going to go ahead and mark this as answered and start a new question, as I am not sure how to use norm. Thanks for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!