nonlinear fit for a sum function

3 vues (au cours des 30 derniers jours)
Dor Bettran
Dor Bettran le 9 Nov 2020
Commenté : Dor Bettran le 9 Nov 2020
i want to fit the following function to a given data i have.
%DATA
X=linspace(1,20,100)';
Y=2*exp(0.13*X)+11.2 + 4*exp(0.13*X)+11.2 + 7*exp(0.13*X)+11.2; %consider it as a given data
n=[2 4 7]; %given
b=[0.1 11]; %initial guess
i used fitnlm as follows:
modelfun=@(b,x) 2.*exp(b(1).*x)+b(2) + 4.*exp(b(1).*x)+b(2) + 7.*exp(b(1).*x)+b(2);
beta = fitnlm(X,Y,modelfun,b)
Now, i need help with writing 'modelfun' in a nondirect way because the real problem i have is () ,
and obviously it is not practical to write it directly as above.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 9 Nov 2020
This is the general way to write this problem
%DATA
X=linspace(1,20,100)';
Y=2*exp(0.13*X)+11.2 + 4*exp(0.13*X)+11.2 + 7*exp(0.13*X)+11.2; %consider it as a given data
n=[2 4 7]; %given
b=[0.1 11]; %initial guess
m = numel(n);
modelfun = @(b,x) sum(n).*exp(b(1).*x)+m*b(2);
beta = fitnlm(X,Y,modelfun,b)
  3 commentaires
Ameer Hamza
Ameer Hamza le 9 Nov 2020
The solution will change from model to model, for example, for the model in your comment, following will work
modelfun = @(b,x) sum(exp((n-m)/b(1).*x), 2);
This uses some automatic-array expansion trick which might be difficult to understand at the beginning, so for a general solution, you can just write a for-loop.
fun = @(b,x) modelfun(b,x,n,m);
beta = fitnlm(X,Y, fun,b)
function y = modelfun(b,x,n,m)
y = zeros(size(x));
for i = 1:numel(n)
y = y + exp(n(i)-m(i)/b(1).*x);
end
end
Dor Bettran
Dor Bettran le 9 Nov 2020
thank you very much, very helpful!

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by