24小时热门版块排行榜    

查看: 1367  |  回复: 4
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

zhiping780

木虫 (著名写手)

天鹰帮帮主

[求助] matlab程序运行出错,求指点修改

我仿写了一个Matlab的化学反应动力学计算程序,可是运行时有问题,那位大侠帮忙指导下,谢谢


function fourlump
clear all
clc
global theta yexp V21 V23 V24 A xx0 M MH x0
k0 = [1.0  5.0  0.5];         % 参数初值
lb = [0  0  0];                   % 参数下限
ub = [+inf  +inf  +inf];    % 参数上限
xx0=[31.423 42.861 6.403 19.315];
M=[93.99229 83.27657 102.8053 116.2428];
NH=[0.2271 0.2271 0.2271 0.0852 0.0852 0.0852 0.1135 0.1135 .1419 0.1419 0.1419 0.1419 0.1703 0.1703 0.1703];

tspan=[0 1];
ExpData= ...
[1        0.002553        0.002189        0.000629        0.001467
2        0.003354        0.000745        0.000684        0.001973
3        0.002758        0.002128        0.000585        0.001336
4        0.003226        0.003156        0.000672        0.001585
5        0.003495        0.002759        0.000707        0.001658
6        0.003105        0.002974        0.000678        0.001677
7        0.003571        0.001938        0.000791        0.001780
8        0.003073        0.002763        0.000675        0.001594
9        0.002857        0.002761        0.000630        0.001569
10        0.003621        0.001888        0.000679        0.001637
11        0.003228        0.002415        0.000629        0.001522
12        0.003121        0.001553        0.000734        0.001877
13        0.002850        0.002348        0.000636        0.001601
14        0.003406        0.001851        0.000648        0.001570
15        0.003142        0.001809        0.000677        0.001687];
yexp=ExpData(:,2:5);

tspan=[0 1];
V21=[0.8837 1.0897 0.9285 0.8446 0.9119 0.8925 0.9612 0.8992 0.8601 0.9473 0.9115 0.9747 0.8795 0.9365 0.9162]';
V23=[0.8853 0.9914 0.8874 0.8228 0.8501 0.8609 0.9220 0.8669 0.8354 0.8654 0.8592 0.9637 0.8455 0.8638 0.9014]';
V24=[0.7570 0.8668 0.7843 0.7332 0.7551 0.7666 0.7936 0.7678 0.7411 0.7644 0.7638 0.8576 0.7503 0.7658 0.8033]';
theta=[166.8411 250.2616 286.0133 155.4816 248.7706 266.5399 169.4721 593.1523 80.9890 377.9487 566.9230 566.9229 180.9717 361.9435 434.3321];
A=[0.1562 0.1562 0.1562 0.0728 0.0728 0.0728 0.0925 0.0925 0.1106 0.1106 0.1106 0.1106 0.1270 0.1270 0.1270]';
yexp=ExpData(:,2:5);

[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
lsqnonlin(@ObjFunclump,k0,lb,ub,[],tspan,xx0,x0,theta,V21,V23,V24,yexp)
ci = nlparci(k,resid,jacobian)

function f = ObjFunclump(k,tspan,x0,theta,V21,V23,V24,A,yexp)
for i=1:length(theta)
    x0(1)=xx0(1)/((1+2*NH(i))*M(1));
    x0(2)=xx0(2)/((1+2*NH(i))*M(2));
    x0(3)=xx0(3)/((1+2*NH(i))*M(3));
    x0(4)=xx0(4)/((1+2*NH(i))*M(4));
    [t x] = ode45(@FCClump,tspan,x0,[],k,theta(i),V21(i),V23(i),V24(i),A(i));
    y(i,1) = x(end,1);
    y(i,2:4) = x(end,2:4);
end
f1 = y(:,1) - yexp(:,1);
f2 = y(:,2) - yexp(:,2);
f3 = y(:,3) - yexp(:,3);
f4 = y(:,4) - yexp(:,4);
f = [f1;f2;f3;f4];

function dxdt=FCClump(t,x,k,theta,V21,V23,V24,A)

dxdt =  ...
[ theta*k(1)*V21*x(2)/(x(1)+x(2)+x(3)+x(4)+A)
-1*theta*(k(1)+k(2)+k(3))*x(2)/(x(1)+x(2)+x(3)+x(4)+A)
  theta*k(2)*V23*x(2)/(x(1)+x(2)+x(3)+x(4)+A)
  theta*k(3)*V24*x(2)/(x(1)+x(2)+x(3)+x(4)+A)
];


运行后:
??? Error using ==> optim\private\lsqncommon
User supplied function failed with the following error:

Undefined function or variable "y".

Error in ==> lsqnonlin at 147
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Error in ==> fourlump360 at 39
[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

目云天外,志在九霄中。。。。。
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

快乐博士

新虫 (小有名气)

引用回帖:
2楼: Originally posted by xiegangmai at 2012-03-16 17:00:13:
function fourlump
clear all
clc
global theta yexp V21 V23 V24 A xx0 M NH x0
k0 = ;         % 参数初值
lb = ;                   % 参数下限
ub = ;    % 参数上限
xx0=;
M=;
NH=;

tspan=;
Ex ...

按你说的,运行后出现新的问题

Warning: The value of local variables may have been changed to match the
         globals.  Future versions of MATLAB will require that you declare
         a variable to be global before you use that variable.
> In fourlump>ObjFunclump at 45
  In optim\private\lsqncommon at 88
  In lsqnonlin at 147
  In fourlump at 39
Warning: The value of local variables may have been changed to match the
         globals.  Future versions of MATLAB will require that you declare
         a variable to be global before you use that variable.
> In fourlump>ObjFunclump at 45
  In optim\private\lsqncommon at 88
  In lsqnonlin at 147
  In fourlump at 39
??? Error using ==> optim\private\lsqncommon
User supplied function failed with the following error:

Undefined command/function 'xx0'.

Error in ==> lsqnonlin at 147
[x,Resnorm,FVAL,EXITFLAG,OUTPUT,LAMBDA,JACOB] = ...

Error in ==> fourlump at 39
[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
3楼2012-03-16 18:17:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 5 个回答

xiegangmai

版主 (职业作家)

我没头衔

优秀版主优秀版主优秀版主

【答案】应助回帖

感谢参与,应助指数 +1
CODE:
function fourlump
clear all
clc
global theta yexp V21 V23 V24 A xx0 M NH x0
k0 = [1.0  5.0  0.5];         % 参数初值
lb = [0  0  0];                   % 参数下限
ub = [+inf  +inf  +inf];    % 参数上限
xx0=[31.423 42.861 6.403 19.315];
M=[93.99229 83.27657 102.8053 116.2428];
NH=[0.2271 0.2271 0.2271 0.0852 0.0852 0.0852 0.1135 0.1135 .1419 0.1419 0.1419 0.1419 0.1703 0.1703 0.1703];

tspan=[0 1];
ExpData= ...
[1        0.002553        0.002189        0.000629        0.001467
2        0.003354        0.000745        0.000684        0.001973
3        0.002758        0.002128        0.000585        0.001336
4        0.003226        0.003156        0.000672        0.001585
5        0.003495        0.002759        0.000707        0.001658
6        0.003105        0.002974        0.000678        0.001677
7        0.003571        0.001938        0.000791        0.001780
8        0.003073        0.002763        0.000675        0.001594
9        0.002857        0.002761        0.000630        0.001569
10        0.003621        0.001888        0.000679        0.001637
11        0.003228        0.002415        0.000629        0.001522
12        0.003121        0.001553        0.000734        0.001877
13        0.002850        0.002348        0.000636        0.001601
14        0.003406        0.001851        0.000648        0.001570
15        0.003142        0.001809        0.000677        0.001687];
yexp=ExpData(:,2:5);

tspan=[0 1];
V21=[0.8837 1.0897 0.9285 0.8446 0.9119 0.8925 0.9612 0.8992 0.8601 0.9473 0.9115 0.9747 0.8795 0.9365 0.9162]';
V23=[0.8853 0.9914 0.8874 0.8228 0.8501 0.8609 0.9220 0.8669 0.8354 0.8654 0.8592 0.9637 0.8455 0.8638 0.9014]';
V24=[0.7570 0.8668 0.7843 0.7332 0.7551 0.7666 0.7936 0.7678 0.7411 0.7644 0.7638 0.8576 0.7503 0.7658 0.8033]';
theta=[166.8411 250.2616 286.0133 155.4816 248.7706 266.5399 169.4721 593.1523 80.9890 377.9487 566.9230 566.9229 180.9717 361.9435 434.3321];
A=[0.1562 0.1562 0.1562 0.0728 0.0728 0.0728 0.0925 0.0925 0.1106 0.1106 0.1106 0.1106 0.1270 0.1270 0.1270]';
yexp=ExpData(:,2:5);

[k,resnorm,residual,exitflag,output,lambda,jacobian] = ...
lsqnonlin(@ObjFunclump,k0,lb,ub,[],tspan,xx0,x0,theta,V21,V23,V24,yexp)
ci = nlparci(k,resid,jacobian)

其中MH为NH。
CODE:
function f = ObjFunclump(k,tspan,x0,theta,V21,V23,V24,A,yexp)

global theta yexp M NH x0

for i=1:length(theta)
    x0(1)=xx0(1)/((1+2*NH(i))*M(1));
    x0(2)=xx0(2)/((1+2*NH(i))*M(2));
    x0(3)=xx0(3)/((1+2*NH(i))*M(3));
    x0(4)=xx0(4)/((1+2*NH(i))*M(4));
    [t x] = ode45(@FCClump,tspan,x0,[],k,theta(i),V21(i),V23(i),V24(i),A(i));
    y(i,1) = x(end,1);
    y(i,2:4) = x(end,2:4);
end
f1 = y(:,1) - yexp(:,1);
f2 = y(:,2) - yexp(:,2);
f3 = y(:,3) - yexp(:,3);
f4 = y(:,4) - yexp(:,4);
f = [f1;f2;f3;f4];

增加全局变量。
明德厚学、求是创新
2楼2012-03-16 17:00:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xiegangmai

版主 (职业作家)

我没头衔

优秀版主优秀版主优秀版主

【答案】应助回帖

★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ...
zhiping780: 金币+100, 谢谢,你能说明或者帮忙修改一下如何用参数方式传递数据么?我对这些一窍不通 2012-03-17 12:05:35
目的函数ObjFunclump中少了个xx0,加上就没错误。
global theta yexp M NH xx0 x0

至于警告,看英文应该能明白意思。

尽量不采用全局变量,建议用参数方式传递数据。
明德厚学、求是创新
4楼2012-03-16 20:35:46
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhiping780

木虫 (著名写手)

天鹰帮帮主

引用回帖:
4楼: Originally posted by xiegangmai at 2012-03-16 20:35:46:
目的函数ObjFunclump中少了个xx0,加上就没错误。
global theta yexp M NH xx0 x0

至于警告,看英文应该能明白意思。

尽量不采用全局变量,建议用参数方式传递数据。

运行后:
Warning: The value of local variables may have been changed to match the
         globals.  Future versions of MATLAB will require that you declare
         a variable to be global before you use that variable.
> In fourlump>ObjFunclump at 45
  In sfdnls at 90
  In optim\private\snls at 404
  In optim\private\lsqncommon at 213
  In lsqnonlin at 147
  In fourlump at 39
。。。。。。反复出现。。。。。


出结果后:Optimization terminated: relative function value
changing by less than OPTIONS.TolFun.

k =

    0.0000    5.8729    4.9600


resnorm =

    2.9233


residual =

    0.2273
    0.2265
    0.2271
    0.2824
    0.2821
    0.2825
    0.2689
    0.2694
    0.2576
    0.2568
    0.2572
    0.2573
    0.2465
    0.2460
    0.2462
   -0.0022
   -0.0007
   -0.0021
   -0.0032
   -0.0028
   -0.0030
   -0.0019
   -0.0028
   -0.0028
   -0.0019
   -0.0024
   -0.0016
   -0.0023
   -0.0019
   -0.0018
    0.2121
    0.2324
    0.2125
    0.2487
    0.2552
    0.2578
    0.2596
    0.2472
    0.2295
    0.2359
    0.2346
    0.2572
    0.2218
    0.2256
    0.2334
    0.2355
    0.2528
    0.2400
    0.2880
    0.2923
    0.2946
    0.2861
    0.2813
    0.2639
    0.2681
    0.2681
    0.2850
    0.2542
    0.2570
    0.2635


exitflag =

     3


output =

    firstorderopt: 3.9262e-004
       iterations: 12
        funcCount: 52
     cgiterations: 6
        algorithm: 'large-scale: trust-region reflective Newton'
          message: [1x87 char]


lambda =

    lower: [3x1 double]
    upper: [3x1 double]


jacobian =

   (1,1)       0.0289
   (2,1)       0.0356
   (3,1)       0.0303
   (4,1)       0.0343
   (5,1)       0.0370
   (6,1)       0.0362
   (7,1)       0.0372
   (8,1)       0.0348
   (9,1)       0.0318
  (10,1)       0.0351
  (11,1)       0.0337
  (12,1)       0.0361
  (13,1)       0.0312
  (14,1)       0.0332
  (15,1)       0.0325
  (16,1)       0.0001
  (17,1)      -0.0000
  (18,1)      -0.0000
  (19,1)      -0.0000
  (20,1)      -0.0000
  (21,1)      -0.0000
  (22,1)      -0.0001
  (23,1)       0.0002
  (24,1)      -0.0000
  (25,1)       0.0004
  (26,1)      -0.0002
  (27,1)       0.0011
  (28,1)      -0.0000
  (29,1)      -0.0002
  (30,1)       0.0000
  (31,1)      -0.0157
  (32,1)      -0.0176
  (33,1)      -0.0157
  (34,1)      -0.0181
  (35,1)      -0.0187
  (36,1)      -0.0189
  (37,1)      -0.0193
  (38,1)      -0.0183
  (39,1)      -0.0168
  (40,1)      -0.0175
  (41,1)      -0.0171
  (42,1)      -0.0199
  (43,1)      -0.0162
  (44,1)      -0.0165
  (45,1)      -0.0173
  (46,1)      -0.0114
  (47,1)      -0.0130
  (48,1)      -0.0117
  (49,1)      -0.0136
  (50,1)      -0.0140
  (51,1)      -0.0142
  (52,1)      -0.0140
  (53,1)      -0.0137
  (54,1)      -0.0126
  (55,1)      -0.0131
  (56,1)      -0.0129
  (57,1)      -0.0149
  (58,1)      -0.0122
  (59,1)      -0.0124
  (60,1)      -0.0130
   (1,2)      -0.0000
   (2,2)      -0.0000
   (3,2)      -0.0000
   (4,2)      -0.0000
   (5,2)      -0.0000
   (6,2)      -0.0000
   (7,2)      -0.0000
   (8,2)      -0.0000
   (9,2)      -0.0000
  (10,2)      -0.0000
  (11,2)      -0.0000
  (12,2)      -0.0000
  (13,2)      -0.0000
  (14,2)      -0.0000
  (15,2)      -0.0000
  (16,2)       0.0001
  (17,2)      -0.0000
  (18,2)      -0.0000
  (19,2)      -0.0000
  (20,2)      -0.0000
  (21,2)      -0.0000
  (22,2)      -0.0001
  (23,2)       0.0002
  (24,2)      -0.0000
  (25,2)       0.0004
  (26,2)      -0.0002
  (27,2)       0.0011
  (28,2)      -0.0000
  (29,2)      -0.0002
  (30,2)       0.0000
  (31,2)       0.0132
  (32,2)       0.0148
  (33,2)       0.0133
  (34,2)       0.0153
  (35,2)       0.0158
  (36,2)       0.0160
  (37,2)       0.0164
  (38,2)       0.0153
  (39,2)       0.0142
  (40,2)       0.0145
  (41,2)       0.0147
  (42,2)       0.0158
  (43,2)       0.0137
  (44,2)       0.0141
  (45,2)       0.0146
  (46,2)      -0.0114
  (47,2)      -0.0130
  (48,2)      -0.0117
  (49,2)      -0.0136
  (50,2)      -0.0140
  (51,2)      -0.0142
  (52,2)      -0.0140
  (53,2)      -0.0137
  (54,2)      -0.0126
  (55,2)      -0.0131
  (56,2)      -0.0129
  (57,2)      -0.0150
  (58,2)      -0.0122
  (59,2)      -0.0124
  (60,2)      -0.0130
   (1,3)      -0.0000
   (2,3)      -0.0000
   (3,3)      -0.0000
   (4,3)      -0.0000
   (5,3)      -0.0000
   (6,3)      -0.0000
   (7,3)      -0.0000
   (8,3)      -0.0000
   (9,3)      -0.0000
  (10,3)      -0.0000
  (11,3)      -0.0000
  (12,3)      -0.0000
  (13,3)      -0.0000
  (14,3)      -0.0000
  (15,3)      -0.0000
  (16,3)       0.0001
  (17,3)      -0.0000
  (18,3)      -0.0000
  (19,3)      -0.0000
  (20,3)      -0.0000
  (21,3)      -0.0000
  (22,3)      -0.0001
  (23,3)       0.0002
  (24,3)      -0.0000
  (25,3)       0.0004
  (26,3)      -0.0002
  (27,3)       0.0011
  (28,3)      -0.0000
  (29,3)      -0.0002
  (30,3)       0.0000
  (31,3)      -0.0158
  (32,3)      -0.0176
  (33,3)      -0.0157
  (34,3)      -0.0181
  (35,3)      -0.0187
  (36,3)      -0.0189
  (37,3)      -0.0193
  (38,3)      -0.0183
  (39,3)      -0.0168
  (40,3)      -0.0175
  (41,3)      -0.0171
  (42,3)      -0.0199
  (43,3)      -0.0162
  (44,3)      -0.0165
  (45,3)      -0.0173
  (46,3)       0.0134
  (47,3)       0.0154
  (48,3)       0.0139
  (49,3)       0.0161
  (50,3)       0.0166
  (51,3)       0.0169
  (52,3)       0.0167
  (53,3)       0.0160
  (54,3)       0.0149
  (55,3)       0.0152
  (56,3)       0.0154
  (57,3)       0.0168
  (58,3)       0.0144
  (59,3)       0.0148
  (60,3)       0.0154

Usage: RESID(DATA,MODEL)
       E = RESID(DATA,MODEL,No_OF_LAGS,MAXSIZE)
??? One or more output arguments not assigned during call to 'C:\MATLAB701\toolbox\ident\idobsolete\resid.m (resid)'.

Error in ==> fourlump at 41
ci = nlparci(k,resid,jacobian)
目云天外,志在九霄中。。。。。
5楼2012-03-17 12:12:13
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 265求调剂 +9 梁梁校校 2026-03-19 9/450 2026-03-20 12:33 by lature00
[考博] 招收博士1-2人 +3 QGZDSYS 2026-03-18 3/150 2026-03-20 11:58 by 呱呱呱呱叫
[考研] 材料专硕英一数二306 +6 z1z2z3879 2026-03-18 6/300 2026-03-20 08:49 by xingguangj
[考研] 一志愿中国海洋大学,生物学,301分,求调剂 +5 1孙悟空 2026-03-17 6/300 2026-03-19 23:46 by zcl123
[考研] 320求调剂0856 +3 不想起名字112 2026-03-19 3/150 2026-03-19 22:53 by 学员8dgXkO
[考研] 307求调剂 +9 冷笙123 2026-03-17 9/450 2026-03-19 22:44 by 学员8dgXkO
[考研] 288求调剂 +15 于海海海海 2026-03-19 15/750 2026-03-19 22:41 by 学员8dgXkO
[考研] 288求调剂,一志愿华南理工大学071005 +5 ioodiiij 2026-03-17 5/250 2026-03-19 18:22 by zcl123
[考研] 085600材料与化工求调剂 +6 绪幸与子 2026-03-17 6/300 2026-03-19 13:27 by houyaoxu
[考研] 328求调剂,英语六级551,有科研经历 +4 生物工程调剂 2026-03-16 12/600 2026-03-19 11:10 by 生物工程调剂
[考研] 0817调剂 +3 没有答案_ 2026-03-14 3/150 2026-03-19 09:51 by Xu de nuo
[考研] 0703化学调剂,求各位老师收留 +10 秋有木北 2026-03-14 10/500 2026-03-19 05:52 by anny19840123
[考研] 08工科 320总分 求调剂 +5 梨花珞晚风 2026-03-17 5/250 2026-03-18 14:49 by haxia
[考研] 0854可跨调剂,一作一项核心论文五项专利,省、国级证书40+数一英一287 +8 小李0854 2026-03-16 8/400 2026-03-18 14:35 by 搏击518
[考研] 0703化学336分求调剂 +6 zbzihdhd 2026-03-15 7/350 2026-03-18 09:53 by zhukairuo
[考研] 一志愿,福州大学材料专硕339分求调剂 +3 木子momo青争 2026-03-15 3/150 2026-03-17 07:52 by laoshidan
[考研] 11408 一志愿西电,277分求调剂 +3 zhouzhen654 2026-03-16 3/150 2026-03-17 07:03 by laoshidan
[考研] [导师推荐]西南科技大学国防/材料导师推荐 +3 尖角小荷 2026-03-16 6/300 2026-03-16 23:21 by 尖角小荷
[考研] 321求调剂 +5 大米饭! 2026-03-15 5/250 2026-03-16 16:33 by houyaoxu
[考研] 招收0805(材料)调剂 +3 18595523086 2026-03-13 3/150 2026-03-14 00:33 by 123%、
信息提示
请填处理意见