我写的代码如下:function ODEfunctiongroup_boke_wo

%  动力学ODE方程模型的参数估计

%  此例数据只有t,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10

%   k1-k12 为待拟合参数

%   The variables  y here are y(1)=x1, y(2)=x2, y(3)=x3,y(4)=x4,y(5)=x5,y(6)=x6,y(7)=x7,y(8)=x8,y(9)=x9,y(10)=x10.

%

clear all;

clc;

global t0

k0 = [5 5 5 5 5 5 5 5 5 5 5 5];        % 参数初值

lb = [0 0 0 0 0 0 0 0 0 0 0 0];        % 参数下限

ub = [+inf +inf +inf +inf +inf +inf +inf +inf +inf +inf +inf +inf];    % 参数上限

x0 = [2276.45 20.11 0.029 0.0010 0.19 0.23 0.12 0.045 119.24 0.089];  %初始状态 %动力学数据:

% t      x1    x2    x3   x4   x5      x6  x7   x8      x9   x10

ExpData = ...

[0 2276.45  20.11  0.03  0.00  0.19  0.23  0.12  0.04  119.24  0.09

4 2003.47  67.60  0.22  0.00  0.57  0.19  0.18  0.29  746.86  0.16

8 2032.79  77.54  0.29  0.00  0.93  0.15  0.24  0.50  1101.89 0.23

12 2149.09  56.71  0.21  0.00  0.94  0.15  0.24  0.47  1176.67 0.35

16 2011.55  42.71  0.15  0.01  0.89  0.14  0.26  0.47  995.61  0.33

20 1839.29  48.31  0.15  0.01  0.79  0.13  0.27  0.56  864.48  0.52

24 2003.07  34.93  0.19  0.02  0.88  0.11  0.28  0.79  737.48  0.60

28 1923.94  37.12  0.16  0.02  1.07  0.13  0.30  0.96  670.42  0.60

32 1790.84  60.29  0.19  0.03  1.34  0.14  0.52  1.54  672.09  1.16

36 1909.44  45.17  0.19  0.03  1.25  0.13  0.48  0.90  710.46  1.29

40 1700.47  45.07  0.21  0.03  1.06  0.14  0.33  0.44  672.98  0.78

];

t0 = ExpData(:,1);

yexp = ExpData(:,2:11);                 % yexp: 对应实验数据[x1 x2 x3 x4 x5 x6 x7 x8 x9 x10]

%% 使用函数fmincon()进行参数估计

% [k,fval,flag] = fmincon(@ObjFunc4Fmincon,k0,[],[],[],[],lb,ub,[],[],x0,yexp);

% fprintf('\n使用函数fmincon()估计得到的参数值为:\n')

% fprintf('\tk1 = %.4f\n',k(1))

% fprintf('\tk2 = %.4f\n',k(2))

% fprintf('\tk3 = %.4f\n',k(3))

% fprintf('\tk4 = %.4f\n',k(4))

% fprintf('\tk5 = %.4f\n',k(5))

% fprintf('\tk6 = %.4f\n',k(6))

% fprintf('\tk7 = %.4f\n',k(7))

% fprintf('\tk8 = %.4f\n',k(8))

% fprintf('\tk9 = %.4f\n',k(9))

% fprintf('\tk10 = %.4f\n',k(10))

% fprintf('\tk11 = %.4f\n',k(11))

% fprintf('\tk12 = %.4f\n',k(12))

% fprintf('The sum of the squares is: %.1e\n\n',fval)

% k_fmincon = k;

% 这一步通常被省略,通过反复迭代初始值得到最优解,加上后可以降低对初始值的依赖。

% 以函数fmincon()估计得到的结果为初值,使用函数lsqnonlin()进行参数估计

% 需要指出,这种方法并非在所有场合均有效,但有时确实可以改善求解效果。

%% 使用函数lsqnonlin()进行参数估计

[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...

lsqnonlin(@ObjFunc4LNL,k0,lb,ub,[],x0,yexp);

ci = nlparci(k,residual,jacobian);

fprintf('\n\n使用函数lsqnonlin()估计得到的参数值为:\n')

Output(k,ci,resnorm)

%% 以函数fmincon()估计得到的结果为初值,使用函数lsqnonlin()进行参数估计

% k0 = k_fmincon;

% [k,resnorm,residual,exitflag,output,lambda,jacobian] = ...

%    lsqnonlin(@ObjFunc4LNL,k0,lb,ub,[],x0,yexp);

% ci = nlparci(k,residual,jacobian);

% fprintf('\n\n以fmincon()的结果为初值,使用函数lsqnonlin()估计得到的参数值为:\n')

% Output(k,ci,resnorm)

%% ----------------------------------------------------

tspan = t0';     % 指定点时间,也可增加步数,只需多调用一次ode45函数

[t x] = ode45(@KineticEqs,tspan,x0,[],k);

y(:,1:10) = x(:,1:10);

figure(1)

plot(tspan,y(:,1),'b',ExpData(:,1),yexp(:,1),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y1(x1)');

figure(2)

plot(tspan,y(:,2),'b',ExpData(:,1),yexp(:,2),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y2(x2)');

figure(3)

plot(tspan,y(:,3),'b',ExpData(:,1),yexp(:,3),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y3(x3)');

figure(4)

plot(tspan,y(:,4),'b',ExpData(:,1),yexp(:,4),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y4(x4)');

figure(5)

plot(tspan,y(:,5),'b',ExpData(:,1),yexp(:,5),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y5(x5)');

figure(6)

plot(tspan,y(:,6),'b',ExpData(:,1),yexp(:,6),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y6(x6)');

figure(7)

plot(tspan,y(:,7),'b',ExpData(:,1),yexp(:,7),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y7(x7)');

figure(8)

plot(tspan,y(:,8),'b',ExpData(:,1),yexp(:,8),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y8(x8)');

figure(9)

plot(tspan,y(:,9),'b',ExpData(:,1),yexp(:,9),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y9(x9)');

figure(4)

plot(tspan,y(:,10),'b',ExpData(:,1),yexp(:,10),'or'),legend('计算值','实验值','Location','best')

xlabel('时间');ylabel('y10(x10)');

end

%% ------------------------------------------------------------------

function Output(k,ci,resnorm)

% Output.m

fprintf('\tk1 = %.4f ± %.4f\n',k(1),ci(1,2)-k(1))

fprintf('\tk2 = %.4f ± %.4f\n',k(2),ci(2,2)-k(2))

fprintf('\tk3 = %.4f ± %.4f\n',k(3),ci(3,2)-k(3))

fprintf('\tk4 = %.4f ± %.4f\n',k(4),ci(4,2)-k(4))

fprintf('\tk5 = %.4f ± %.4f\n',k(5),ci(5,2)-k(5))

fprintf('\tk6 = %.4f ± %.4f\n',k(6),ci(6,2)-k(6))

fprintf('\tk7 = %.4f ± %.4f\n',k(7),ci(7,2)-k(7))

fprintf('\tk8 = %.4f ± %.4f\n',k(8),ci(8,2)-k(8))

fprintf('\tk9 = %.4f ± %.4f\n',k(9),ci(9,2)-k(9))

fprintf('\tk10 = %.4f ± %.4f\n',k(10),ci(10,2)-k(10))

fprintf('\tk11 = %.4f ± %.4f\n',k(11),ci(11,2)-k(11))

fprintf('\tk12 = %.4f ± %.4f\n',k(12),ci(12,2)-k(12))

fprintf('The sum of the squares is: %.1e\n\n',resnorm)

end

%% ------------------------------------------------------------------

% function f = ObjFunc4Fmincon(k,x0,yexp)

% global t0

% tspan = t0';

% [t x] = ode45(@KineticEqs,tspan,x0,[],k);

% y(:,1:10) = x(:,1:10);      % 对应实验数据  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10

% f = sum((y(:,1)-yexp(:,1)).^2) + sum((y(:,2)-yexp(:,2)).^2)  ...

%     + sum((y(:,3)-yexp(:,3)).^2) + sum((y(:,4)-yexp(:,4)).^2)+sum((y(:,5)-yexp(:,5)).^2)+sum((y(:,6)-yexp(:,6)).^2)

%     +sum((y(:,7)-yexp(:,7)).^2)+sum((y(:,8)-yexp(:,8)).^2)+sum((y(:,9)-yexp(:,9)).^2)+sum((y(:,10)-yexp(:,10)).^2);   % 计算平方和,供fmincon调用

% end

%% ------------------------------------------------------------------

function f = ObjFunc4LNL(k,x0,yexp)

global t0

tspan = t0';

[t x] = ode45(@KineticEqs,tspan,x0,[],k);

y(:,1:10) = x(:,1:10);            % 对应实验数据  x1 x2 x3 x4 x5 x6 x7 x8 x9 x10

f1 = y(:,1) - yexp(:,1);

f2 = y(:,2) - yexp(:,2);

f3 = y(:,3) - yexp(:,3);

f4 = y(:,4) - yexp(:,4);

f5 = y(:,5) - yexp(:,5);

f6 = y(:,6) - yexp(:,6);

f7 = y(:,7) - yexp(:,7);

f8 = y(:,8) - yexp(:,8);

f9 = y(:,9) - yexp(:,9);

f10 = y(:,10) - yexp(:,10);

f = [f1; f2; f3; f4;f5;f6;f7;f8;f9;f10]; % 理论值与实验值的差值,残差

% figure(5)

% subplot(2,2,1)

% plot(f1);

% subplot(2,2,2)

% plot(f2);

% subplot(2,2,3)

% plot(f3);

% subplot(2,2,4)

% plot(f4);

end

%% ------------------------------------------------------------------

function dxdt = KineticEqs(t,x,k)   % 微分方程组

dxdt =  ...

[(-k(1)*x(1));

(k(1)*x(1)-k(2)*x(3)-k(5)*x(3)-k(7)*x(3)-k(9)*x(3));

(k(2)*x(3)-k(3)*x(4)+k(13)*x(5));

(k(7)*x(3)-k(8)*x(5)-k(12)*x(5)-k(13)*x(5));

(k(9)*x(3)-k(10)*x(6)-k(11)*x(6)-k(14)*x(6));

(k(5)*x(3)-k(6)*x(7)+k(14)*x(6));

(k(8)*x(5)+k(11)*x(6));

(k(3)*x(4));

(k(12)*x(5)+k(10)*x(6));

(k(6)*x(7))

];

end

% code end

错误提示:索引超出数组元素的数目(12)。

出错 ODEfunctiongroup_boke_wo>KineticEqs (line 159)

(k(2)*x(3)-k(3)*x(4)+k(13)*x(5));

出错 odearguments (line 90)

f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

出错 ode45 (line 115)

odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

出错 ODEfunctiongroup_boke_wo>ObjFunc4LNL (line 131)

[t x] = ode45(@KineticEqs,tspan,x0,[],k);

出错 lsqnonlin (line 215)

initVals.F = feval(funfcn{3},xCurrent,varargin{:});

出错 ODEfunctiongroup_boke_wo (line 54)

lsqnonlin(@ObjFunc4LNL,k0,lb,ub,[],x0,yexp);

原因:  Failure in initial objective function evaluation. LSQNONLIN cannot continue.

matlab错误的代码,matlab代码纠正错误相关推荐

  1. matlab错误使用builtin,MATLAB环境下运行MATLAB函数时发生异常

    首先,我想在演练中测试代码,但每一次我得到相同的异常: myfunc.m: function myfunc() disp('hello from MYFUNC') end 的Java: class S ...

  2. matlab错误使用 conv2,matlab conv2函数

    场景:图像处理中随便核卷积(matlab中conv2函数)的快速实现 图像处理中任意核卷积(matlab中conv2函数)的快速实现. 卷积其实是图像处理中最基本的操作,我们常见的一些算法比如:均值模 ...

  3. matlab错误使用meshline47,matlab里mesh出错Z 必须为矩阵,不能是标量或矢量怎么解决...

    matlab 求和的出错 symsum是符号运算,要先用syms定义符号变量用法详见docsymsum 已知等长向量X,Y,Z,如何利用mesh或surf函数在MATLAB中绘制三维曲面图? [x,y ...

  4. matlab错误使用disp,Matlab初学1-disp()的使用

    由于一些个人原因开始使用Matlab,今天就学习了disp()函数的使用. disp()可以输出字符串,其语法格式如下: >> disp('Hello Word!') Hello Word ...

  5. Matlab/Simulink自动生成STM32代码_基于模型的开发_环境搭建

    目录 前言 官方简介 Matlab R2018b安装 STM32-MAT/TARGET 安装 STM32CubeMX 安装 STM32CubeIDE, Keil安装 ST-Link驱动安装 微信公众号 ...

  6. matlab绘制频散曲线,Matlab绘制频散曲线程序代码.docx

    Matlab绘制频散曲线程序代码.docx 下载提示(请认真阅读)1.请仔细阅读文档,确保文档完整性,对于不预览.不比对内容而直接下载带来的问题本站不予受理. 2.下载的文档,不会出现我们的网址水印. ...

  7. Matlab:序列分析法MATLAB代码

    Matlab:序列分析法MATLAB代码 目录 输出结果 设计代码 输出结果 更新-- 设计代码 ###下面所有带代码中的n值需要以自己输入的数据为准###1.简单一次滑动平均法预测MATLAB程序代 ...

  8. 天空之城 matlab,[转载]matlab演奏《天空之城》代码

    %matlab演奏<天空之城>代码 l_dao=262; %将"l_dao"宏定义为低音"1"的频率262Hz l_re =286; %将" ...

  9. 精馏塔matlab,MATLAB图解精馏塔理论塔板数程序代码

    <MATLAB图解精馏塔理论塔板数程序代码>由会员分享,可在线阅读,更多相关<MATLAB图解精馏塔理论塔板数程序代码(6页珍藏版)>请在人人文库网上搜索. 1.MATLAB图 ...

  10. 【VS Code配置matlab】手把手教学,matlab也能自动补全+瞬间启动+代码整理!

    前言: matlab很好地集成了大量数学处理函数,甚至封装了包括信号处理.图像处理.神经网络.音乐等在内的方法.但matlab启动慢.没有代码补全.开发环境不友善等缺点常受人诟病,算法编写者往往需要进 ...

最新文章

  1. Android网络框架Volley的快速使用
  2. Dos攻击工具(ZAmbIE)
  3. Java程序员从阿里面试回来,这些面试题你们会吗?
  4. scaling之旅_机器学习算法之旅
  5. python做电脑软件-作为一个Python程序员,电脑上应该具备哪些软件?
  6. Windbg dump分析 学习总结
  7. [读书笔记]《Head First Servlets JSP》2nd
  8. 【NLP】Google T5速读
  9. 1-5 线性表元素的区间删除 (20 分)
  10. 没有别家钱多,没有别家人多,小型培训机构招生怎么做?
  11. 用开关控制蜂鸣器_蜂鸣器驱动电路(实践出真理)
  12. ​​spss13.0 附安装教程
  13. python应用实例:北京城市地方坐标系向BJ54坐标系的变换程序【测绘地质工作者福利】
  14. 中国移动飞信的研究 笔记二
  15. Word 关闭拼写检查 (去掉Word中拼写检查的所有红色和绿色的浪线)
  16. 计算机一级怎么加波浪下划线,Word快速添加下划线,双下划线条、波浪线、虚线一键搞定...
  17. 2020年408真题_2020年港澳台联考真题——英语!
  18. 20191202Spark
  19. 网页中的动漫人物互动——看板娘
  20. 2019吉大软件C++课设——模拟即时通信系统

热门文章

  1. 1-5(中文版)听力积累
  2. 怎么恢复删除的微信通讯录好友?这样恢复将友谊进行到底!
  3. c语言自己走时间的程序,c语言用哪些语句能实现时间暂停?比方说按某一个键使时间暂停,再按一次使时间继续。...
  4. 如何搬运短视频,从快手搬运视频图文教程攻略
  5. 国内技术管理人员批阅google的“春运交通图”项目
  6. 中移动浦发联合发布四款产品 ,NFC手机年底上市
  7. Revit二次开发之 自定义选项卡排在最前端
  8. 电脑回收站删除的文件还能找回吗 电脑回收站删除的文件怎么恢复
  9. 查询最近三个月的数据
  10. 中首清算|大数据助力灵活用工保驾护航