MATLAB: Nonlinear fit for a sum function

fitnlm

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.

Best Answer

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)