具有动态约束的 MatLab 曲线拟合
我想使用 lsqcurvefit 函数拟合曲线。这就像这个问题:
y = a1 * x + a2 * z
s.t
a1 > 0
a2 > 0
a1 + a2 <= some number
Matlab 中是否可能有这样的动态约束,或者我应该使用其他包吗?
I want to fit a curve using the lsqcurvefit function. It is something like this problem:
y = a1 * x + a2 * z
s.t
a1 > 0
a2 > 0
a1 + a2 <= some number
Is it possible to have such dynamic constraints in matlab or should i use some other package?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要约束是线性等式或不等式,lsqlin 就是解决您问题的工具。您已经描述了线性情况,因此这是正确的解决方案。
如果您展示了一个具有非线性约束的示例,那么您将被迫使用 fmincon (如 Marcin 所建议的),但这对于完全线性问题来说有点过大了。当然,fmincon 的效率也会大大降低,因为 fmincon 不知道您的目标是约束下的线性最小二乘法。
最后,为了使用 fmincon,您需要将目标定义为残差平方和。后一步对于 lsqlin 来说不是必需的,因为 lsqlin 是专门为解决您的问题类别而设计的。
lsqlin is the tool for your problem, as long as the constraints are linear equalities or inequalities. You have described the linear case, so this is the correct solution.
Had you shown an example with nonlinear constraints, you would then be forced to use fmincon (as suggested by Marcin) but this would be overkill for a fully linear problem. And of course, fmincon would also be considerably less efficient, because fmincon does not know that your objective is a linear least squares under constraints.
Finally, in order to use fmincon, you would need to define an objective as a sum of squares of the residuals. This latter step is not necessary for lsqlin, since lsqlin is designed explicitly to solve your class of problem.
fmincon 可能就是您想要的。约束是一个单独的函数,因此您可以在其中塞入任何内容,任意数量的约束,只要您可以将它们表达为方程即可。
fmincon is probably what you want. The constraints are a separate function, so you can cram anything in there, any number of constraints, as long as you can express them as equations .