Multiple Parameters in fmincon
3 次查看(过去 30 天)
显示 更早的评论
I have a fairly simple model that looks as follows:
, where α is a constant, and X and β are p x 1 vectors.
I have given data for my dependent variable (y) and two covariates ('cov1' and 'cov2'). My approach so far has been the following:
% Data
y = [20, 21, 24, 26, 27, 28, 32, 37, 38, 39, 40, 45, 43, 40, 38];
cov1 = [70, 71, 74, 78, 78, 81, 83, 84, 86, 89, 91, 91, 92, 96, 91];
cov2 = [101, 101, 104, 108, 108, 110, 111, 114, 115, 117, 117, 118, 117, 119, 120];
% initial values
param_init = [0, 0, 0];
% function
y_model = @(p) p(1) + p(2).*cov1 + p(3).*cov2;
objective = @(p) sqrt(mean((y - y_model(p)).^2));
% optimization
[param_opt, fval] = fmincon(objective, param_init)
My goal is to minimize the RMSE between the observed y and my y_model values. At the moment I have specified three parameters for this purpose.
However, it would be interesting to know how I can specify that β is a p x 1 vector without specifying two separate parameters in my function ? I am not sure if this will work though?
0 个评论
回答(1 个)
Matt J
2021-1-19
编辑:Matt J
2021-1-19
I'm not sure why you are using fmincon for something that has a simple, non-iterative solution:
C=[cov1(:).^0,cov1(:),cov2(:)];
beta = C \ y(:);
If you will eventually be adding in nonlinear (and possibly also linear) constraints, then you can set up fmincon as,
C=[cov1(:).^0,cov1(:),cov2(:)];
objective = @(p) norm(C*p(:)-y(:)).^2;
[beta_opt, fval] = fmincon(objective, beta_init,A,b,Aeq,beq,lb,ub,nonlcon);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!