Problem with non-linear fit
Mostrar comentarios más antiguos
Hi I am trying to fit the data and find the constant values. my code is...
[T]=xlsread('Matlab.xlsx','sheet2','C5:C2685')
[alpha]=xlsread('Matlab.xlsx','sheet2','I5:I2685')%time
[dalpha_dt]=xlsread('Matlab.xlsx','sheet2','J5:J2685')
F=ones(size(alpha));
F=(1-alpha)
coeff0=[2.17E+22, 5] % initial guess
coeff=nlinfit([T,F],dalpha_dt,@model,coeff0);
A=coeff(:,1)
n=coeff(:,2)
Function:
function dhat=model(coef,a)
% par_fit = [a b]
a=coef(1);
b=coef(2);
T=a(:,1);
F=a(:,2);
% predicted model
dhat=a.*exp(208000./(8.3147*(T+273))).*(F^b);
return
I need to find the new coefficient values, but it is showing following error..Please tell me how to resolve this.
Error using nlinfit (line 205)
Error evaluating model function 'model'.
Error in fitting (line 16)
coeff=nlinfit([T,F],dalpha_dt,@model,coeff0);
Caused by:
Attempted to access a(:,2); index out of bounds because numel(a)=1.
Thanks
Respuestas (2)
Star Strider
el 9 de Oct. de 2015
0 votos
You defined ‘a’ as a scalar (appropriately) so it is by definition a (1x1) ‘array’ (taking liberties with the concept here). It doesn’t have a second dimension.
How do you want to define ‘T’ and ‘F’?
11 comentarios
Star Strider
el 9 de Oct. de 2015
R7 DR’s ‘Answer’ moved here_...
Hi
I am importing 'T' and 'F' from excel. Can I remove 'a' and write T and F inplace of a?
Thanks
Star Strider
el 9 de Oct. de 2015
Your objective function needs to be rewritten slightly:
function dhat=model(coef,TF)
% par_fit = [a b]
a=coef(1);
b=coef(2);
T=TF(:,1);
F=TF(:,2);
% predicted model
dhat=a.*exp(208000./(8.3147*(T+273))).*(F.^b);
return
I don’t have your data so I can’t run your code, but this should work, at least as I understand your equation in the context of your nlinfit call (that looks correct). I also vectorised (F.^b) because that is likely necessary. (My philosophy on such is ‘when in doubt, vectorise’, unless you specifically intend matrix operations.)
Star Strider
el 9 de Oct. de 2015
Editada: Star Strider
el 9 de Oct. de 2015
R7 DR’s ‘Answer’ moved here ...
Hi I tried with the new function but getting the following error
Error using nlinfit>checkFunVals (line 612)
The function you provided as the MODELFUN input has returned Inf or NaN values.
Error in nlinfit (line 243)
if funValCheck && ~isfinite(sse), checkFunVals(r); end
Error in fitting (line 16)
coeff=nlinfit([T,F],dalpha_dt,@model,coeff0);
I am attaching the excel file. Please have a look.
---------------------------------------------------------
File re-attached to original post as: R7 DR Matlab.xlsx.
================================================================================
Second ‘Answer’:
Hi
This is the continuation to the code..
A=coeff(:,1)
n=coeff(:,2)
model_dalpha=A.*exp(208000/(8.3417*(T+273))).*(F.^n)
I need to calculate the new 'dalpha' and compare with the data (dalpha_dt) from excel.
Thanks for your time.
Star Strider
el 9 de Oct. de 2015
I got the same error with this version of your code (that works with the posted Excel file):
[T]=xlsread('R7 DR Matlab.xlsx','sheet2','B5:B2685');
[alpha]=xlsread('R7 DR Matlab.xlsx','sheet2','D5:D2685'); %time
[dalpha_dt]=xlsread('R7 DR Matlab.xlsx','sheet2','E5:E2685');
F=(1-alpha);
model = @(b,TF) b(1).*exp(208000./(8.3147*(TF(:,1)+273))).*(TF(:,2).^b(2));
coeff0=[2.17E+22, 5] % initial guess
coeff=nlinfit([T,F],dalpha_dt,model,coeff0);
A = coeff(1);
n = coeff(2);
The error is a problem with either your ‘model’ function or your data. The model output quickly goes to infinity.
You can see that graphically:
dadt = model(coeff0, [T,F]);
figure(1)
plot3(T, F, dadt)
grid on
set(gca, 'ZScale', 'log') % Plot ‘dadt’ On 'log' Scale (Optional)
I will be glad to help you, but you have to sort that problem. I cannot.
Star Strider
el 9 de Oct. de 2015
My response to R7 DR’s ‘Answer’ ...
‘The equations is... dalpha_dt= A*exp(-208000/(8.3417*(T+273)*(F^n)’
The negative sign (that just now appeared) of course makes a huge difference here!
The revised objective function is:
model = @(b,TF) b(1).*exp(-208000./(8.3147*(TF(:,1)+273))).*(TF(:,2).^b(2));
I cannot get the function to provide what I consider to be an acceptable fit. You will have to provide a vector of initial parameter estimates that will converge on a solution that gives an appropriate fit to your data.
Nonlinear parameter estimation is inherently somewhat heuristic. You have to experiment to choose a set of initial parameter estimates that are close enough to the ‘correct’ ones so that your function will converge on the ‘correct’ final estimates, and not diverge or converge on a ‘local minimum’. (A genetic algorithm may be the best way to generate an appropriate set of initial parameter estimates for this problem.)
I got your objective function working correctly for you, so I leave the rest of this experiment to you.
R7 DR
el 9 de Oct. de 2015
Star Strider
el 9 de Oct. de 2015
My pleasure.
Just change the initial estimates in your ‘coeff0’ vector. There is no other way to introduce them into your function.
You have a difficult function to fit, so it will likely require a number of different initial estimates (in your ‘coeff0’ vector) before you arrive at an acceptable fit. I would be tempted to use a genetic algorithm for this, if you have the Global Optimization Toolbox.
R7 DR
el 9 de Oct. de 2015
Star Strider
el 9 de Oct. de 2015
My pleasure.
You need the Global Optimization Toolbox to use the ga function it has, but relatively uncomplicated genetic algorithms are not difficult to program, especially in MATLAB.
Much has been written over the years about genetic algorithms. They are robust — if occasionally slow — problem solvers. The MATLAB documentation is very good (in my opinion). Since it recently changed, I encourage you to read the documentation for R2015a (that offered a different explanation and description of the essential knowledge underlying the various algorithms) as well as for R2015b (the current release).
Real world problems are frequently not easy to solve. You may also need a different model, or a revision of your current model. Since I do not know what you are doing (and I may not have the background to offer specific help even if I did), I cannot suggest any other approach.
R7 DR
el 9 de Oct. de 2015
Star Strider
el 9 de Oct. de 2015
My pleasure.
You, too!
Categorías
Más información sobre Genetic Algorithm en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
