四时宝库

程序员的知识宝库

MATLAB的fmincon函数拟合未知参数

MATLAB中的fmincon函数可以用于拟合未知参数,求参的过程本质上也是求解规划的过程,目标函数可以是拟合函数与已知输出数据之间的差值绝对值的累加值。未知参数取得何值时,目标函数的误差值的最小。今天主要是以实际的例子讲解MATLAB的fmincon函数拟合未知参数。


1.fmincon函数基础知识回顾

前面也写过fmincon函数求解非线性规划的内容,可以点击跳转到对应的推文。

MATLAB的fmincon函数求解非线性规划

非线性规划模型的写法如下

[x,fval]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
参数说明
x的返回值是决策向量x的取值,fval的返回值是目标函数f(x)的取值。
fun是用M文件定义的函数f(x),代表了(非)线性目标函数。
x0是x的初始值。
A,b,Aeq,beq定义了线性约束 ,如果没有线性约束,则A=[],b=[],Aeq=[],beq=[]。
lb和ub是变量x的下界和上界,如果下界和上界没有约束,则lb=[],ub=[],
  也可以写成lb的各分量都为 -inf,ub的各分量都为inf。
nonlcon是用M文件定义的非线性向量函数约束。
options定义了优化参数,不填写表示使用Matlab默认的参数设置。

2.实例

已知函数的输入和输出的数据,求解非线性函数y =A*sin(x)+b中的未知参数A和b的值。

Step1:产生实际输入和输出数据

clc;
clear all;
close all;
A = 2;
x = 0:1:10
b = 1;
y = A*sin(x)+b

程序运行结果

x =
     0     1     2     3     4     5     6     7     8     9    10
y =
    1.0000    2.6829    2.8186    1.2822   -0.5136   -0.9178    0.441

Step2:构建目标函数fun11.m

function f=fun11(x);
% y = A*sin(x)+b
% 已知11组数据的输入输出
%目标函数 sum(abs(y-y0))  十一组数据的x代入求得y0 与真实数据y(含随机扰动)之间的误差和最小
A = x(1);
b = x(2);
x0 = 0:1:10;
y0 = [1,2.68294196961579,2.81859485365136,1.28224001611973,-0.513604990615856,-0.917848549326277,0.441169003602148,2.31397319743758,2.97871649324676,1.82423697048351,-0.0880422217787396];
r = 0;
y = y0+r*rand(1,length(x0));
f = 0;
for i = 1:length(x0)
    f = f + abs(A*sin(x0(i))+b-y(i));
end
end

Step3:构建非线性约束条件函数fun22.m

function [g,h]=fun22(x);
g=[];%不等式约束
h=[];%等式约束
end

Step4:计算函数fun33.m

function y = fun33(x,A,b)
y = A*sin(x)+b;
end

Step5:求解未知参数,主函数

clc;
clear all;
close all;
lb = [0;0];
ub = [5;2];
x0 = [0.5 0.5];
[x,y]=fmincon('fun11',x0,[],[],[],[],lb,ub,'fun22')
A = x(1);
b = x(2);
xx =0:0.1:10;
yy = fun33(xx,A,b);
x_0 = 0:1:10;
y_0 = [1,2.68294196961579,2.81859485365136,1.28224001611973,-0.513604990615856,-0.917848549326277,0.441169003602148,2.31397319743758,2.97871649324676,1.82423697048351,-0.0880422217787396];
figure;
plot(x_0,y_0,'r-');
hold on;
plot(xx,yy,'b-');
xlabel('x');
legend('原始数据','拟合数据');

运行结果

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the value of the step size tolerance and constraints are 
satisfied to within the value of the constraint tolerance.

<stopping criteria details>

x =

    2.0000    1.0000


y =

   9.6152e-08

3.主函数编程方式2

程序


clc;
clear all;
close all;
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
problem.options = options;
problem.solver = 'fmincon';
problem.objective = @fun11;
problem.x0 = [0.5 0.5];
problem.nonlcon = @fun22;
[x,y] = fmincon(problem)
A = x(1);
b = x(2);
xx =0:0.1:10;
yy = fun33(xx,A,b);
x_0 = 0:1:10;
y_0 = [1,2.68294196961579,2.81859485365136,1.28224001611973,-0.513604990615856,-0.917848549326277,0.441169003602148,2.31397319743758,2.97871649324676,1.82423697048351,-0.0880422217787396];
figure;
plot(x_0,y_0,'r-');
hold on;
plot(xx,yy,'b-');
xlabel('x');
legend('原始数据','拟合数据');

运行结果

 Iter  Func-count            Fval   Feasibility   Step Length       Norm of   First-order  
                                                                       step    optimality
    0           3    1.139603e+01     0.000e+00     1.000e+00     0.000e+00     5.931e+00  
    1          10    7.598189e+00     0.000e+00     2.401e-01     1.862e+00     1.100e+01  
    2          16    5.961618e+00     0.000e+00     3.430e-01     1.280e+00     6.490e+00  
    3          20    3.303133e+00     0.000e+00     7.000e-01     1.169e+00     1.100e+01  
    4          25    2.751320e+00     0.000e+00     4.900e-01     3.485e-01     6.490e+00  
    5          29    2.365345e+00     0.000e+00     7.000e-01     5.548e-01     1.100e+01  
    6          32    3.707200e-01     0.000e+00     1.000e+00     2.159e-01     6.207e+00  
    7          42    2.791841e-01     0.000e+00     8.235e-02     4.662e-02     1.100e+01  
    8          46    2.395154e-01     0.000e+00     7.000e-01     6.774e-02     5.000e+00  
    9          49    8.590304e-02     0.000e+00     1.000e+00     4.567e-02     5.931e+00  
   10          53    4.705927e-02     0.000e+00     7.000e-01     1.265e-02     1.100e+01  
   11          59    3.054764e-02     0.000e+00     3.430e-01     8.349e-03     6.207e+00  
   12          63    2.431266e-02     0.000e+00     7.000e-01     7.926e-03     5.931e+00  
   13          66    6.158076e-03     0.000e+00     1.000e+00     3.897e-03     1.100e+01  
   14          72    5.996440e-03     0.000e+00     3.430e-01     9.064e-04     6.207e+00  
   15          79    4.660536e-03     0.000e+00     2.401e-01     7.619e-04     1.100e+01  
   16          82    2.834112e-04     0.000e+00     1.000e+00     4.829e-04     6.490e+00  
   17          95    2.555885e-04     0.000e+00     2.825e-02     2.833e-05     1.100e+01  
   18         102    1.594123e-04     0.000e+00     2.401e-01     4.575e-05     6.490e+00  
   19         105    5.549944e-05     0.000e+00     1.000e+00     3.100e-05     6.490e+00  
   20         108    5.356758e-05     0.000e+00     1.000e+00     1.550e-05     5.931e+00  
   21         111    6.775893e-06     0.000e+00     1.000e+00     7.106e-06     1.100e+01  
   22         119    6.482512e-06     0.000e+00     1.681e-01     1.851e-06     5.383e+00  
   23         122    6.482512e-06     0.000e+00     3.430e-01     1.672e-06     5.383e+00  

Local minimum possible. Constraints satisfied.

fmincon stopped because the size of the current step is less than
the value of the step size tolerance and constraints are 
satisfied to within the value of the constraint tolerance.

<stopping criteria details>

x =

    2.0000    1.0000


y =

   6.4825e-06

本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。

作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接