Problem with non-linear fit

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
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
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
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
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
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
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
R7 DR el 9 de Oct. de 2015
Now the program is running but it is only changing 'n' value and 'A' value is always initial guess only. I tried with different initial guesses but as you told the fit is not so acceptable.
A n
8.79422E+11 2
5.15292E+12 5
1.60549E+13 8
4.28179E+13 10
3.19866E+13 12
3.06263E+13 15
3.38504E+14 18
6.7663E+22 20
6.84874E+21 22
I can use the above values as initial guess. Could you please tell me how to introduce these initial guesses into code?
Thanks for your time.
Star Strider
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
R7 DR el 9 de Oct. de 2015
I think I have optimization toolbox but I never worked with a genetic algorithm.
Do you have any suggestions to read about genetic algorithm?
Thanks for your time.
Star Strider
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
R7 DR el 9 de Oct. de 2015
Thank for the information.
I will try to find the solution using genetic algorithms. If I need any help I will come back to you.
Thanks for your time. Have a nice weekend.
Star Strider
Star Strider el 9 de Oct. de 2015
My pleasure.
You, too!

Iniciar sesión para comentar.

R7 DR
R7 DR el 9 de Oct. de 2015

0 votos

Hi
The idea is that, I have data generated from the experiment. I can also calculate same kind of data from mathematical equation which is depended on two coefficients. Now I need to find these coefficients by fitting the mathematical data with experimental data.
The equations is...dalpha_dt= A*exp(-208000/(8.3417*(T+273)*(F^n)
T=loaded from the excel.
F=loaded from excel.
A & n are the coefficients I want to find.
I made the same calculation using excel with an initial guess of 'A & n' and the curves look like the following image.
Thanks

1 comentario

R7 DR
R7 DR el 9 de Oct. de 2015
I dont know whether nonlinear function can be used or not for these kind of problems.
Thanks

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 9 de Oct. de 2015

Comentada:

el 9 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by