How to design a proper script/code for estimating multiple parameters (more than three) for a model, without using 'lsqnonline'?

Hello everyone.
I wanted to ask if there is a proper way for designing a script to estimate multiple parameters for a model.
My problem is that I have a model function - let say 'My_model(a,b,c,d)' - that needed 4 different parameters, and I want to fit the model with the measured data. I wanted to estimate the best 4 parameters that will give the least Root-Mean-Square-Error (RMSE). I have designed my codes using for loops. I would like to find a better way to design the code, and would like to ask your help, comments or suggestions on what should I do to improve it.
Below is the scripts:
% Script Example
load('Data'); %load the 'Data_measured' with size 1 x n
% Parameters to estimate
a = 0.12:0.08:4.1; %the estimated range for 'a'
b = 0.01:0.01:1; %the estimated range for 'b'
c = 0.0:0.01:1; %the estimated range for 'c'
d = 1.2:0.02:2; %the estimated range for 'd'
% Declare initial error
RMSEr = 100;
% The Estimation
for i1=1:size(a,2)
for i2=1:size(b,2)
for i3=1:size(c,2)
for i4=1:size(d,2)
Data_Predict = My_model(a(i1), b(i2), c(i3), d(i4)); % Having size 1 x n
Er = sqrt(nansum((Data_measured - Data_Predict).^2)) * 100 / (size(Data_measured,2));
if Er < RMSEr
RMSEr = Er;
a_best = a(i1);
b_best = b(i2);
c_best = c(i3);
d_best = d(i4);
end
end
end
end
end
This script takes a long time to estimates all the best parameters for the model. I would like to know if there is a more suitable way, method, and/or built-in function that can help me to improve this code.
Thank you very much.
Note: I have looked 'lsqnonline' and 'lsqcurvefit' functions, but I don't know how to use it to estimate multiple parameters, or whether it is suitable for my problem.

 Accepted Answer

You could use lsqnonlin. Its application is quite straightforward, the only important thing to be aware of is that the function must accept a vector of values, not separate inputs (in the example below I used an anonymous function to allow this).
You need to carefully read the lsqnonlin documentation, but here is some untested code to get you started:
S = load('Data'); % best practice is to load into a variable.
lb = [0.12;0.01;0;1.2];
ub = [4.10;1.00;1;2.0];
x0 = (lb + ub) / 2;
fun = @(varargin)My_model(varargin{:})-S.Data_measured;
x = lsqnonlin(fun,x0,lb,ub);
It will need some tweaking, and you should also look at the optimization options.

More Answers (0)

Categories

Find more on Robust Control Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!