MATLAB: Optimize a variable in a function to match experimental data

optimizationsolver

I have a set of experimental data in excel, and I am trying to match that data to a theoretical function, which has a variable that needs to be optimized. If I were doing it in Excel, I would find the sum of squared errors and use solver to optimize that variable. How would I do the same thing but via Matlab?

Best Answer

Also see lsqcurvefit(): https://www.mathworks.com/help/releases/R2020a/optim/ug/lsqcurvefit.html. It minimizes the sum of square errors. The following shows an example of using lsqcurvefit for a fictitious model. You can adapt it according to your model
x = 1:100;
model = @(a,b,c,x) a*x + exp(b*x) + c; % model to estimate
y = model(2,0.1,10,x) + 1000*rand(size(x)); % get response from model and add noise
x0 = rand(3,1); % initial guess for a, b, and c
est_param = lsqcurvefit(@(param, x) model(param(1), param(2), param(3), x), x0, x, y);
plot(x, y, '.', x, model(est_param(1), est_param(2), est_param(3), x))
Related Question