gpt4 book ai didi

Matlab - 用约束参数拟合曲线

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:12 24 4
gpt4 key购买 nike

对于 ( x , y ) 数据集,设一条由 a 中的表达式给出的曲线, b , c ...等,例如f='a*exp(b*x)+c' , 安装为 cfit=fit(x,y,f) .

假设我们有一组约束,例如 b>0 , c+b>a/2 .我应该如何使用 fit在这种情况下命令?

最佳答案

虽然您可以设置一个下限来强制执行 b>0,但我认为以某种方式正确执行 c+b>a/2 是不可能的适合()。但最终每个拟合问题也可以看作是“最小化曲线到数据的距离”问题,因此可以使用 fmincon() 来实现您的目标:

%some sample x values
xdata = rand(1000,1);
%some parameters a,b,c
a = 2;
b = 3;
c = 4;
%resulting y values + some noise
ydata=a*exp(b*xdata)+c+rand(1000,1)*10-5;
plot(xdata,ydata,'o')

%function to minimize. It returns the sum of squared distances between the polynom and the data.
fun = @(coefs) sum((coefs(1)*exp(coefs(2).*xdata)+coefs(3)-ydata).^2);
%nonlinear constaint to enforce c+b>a/2, which is the same as -(c+b-a/2)<0
nonlcon = @(coefs)deal(-(coefs(3)+coefs(2)-coefs(1)/2), 0);
% lower bounds to enforce b>0
lb = [-inf 0 -inf];
%starting values
x0 = [1 1 1];
%finally find the coefficients (which should approximately be the values of a, b and c)
coefs = fmincon(fun,x0,[],[],[],[],lb,[],nonlcon)

关于Matlab - 用约束参数拟合曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46177875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com