| 5 | 1/1 | 返回列表 |
| 查看: 1374 | 回復: 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]; % 參數(shù)初值 lb = [0 0 0]; % 參數(shù)下限 ub = [+inf +inf +inf]; % 參數(shù)上限 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] = ... |


新蟲 (小有名氣)
|
按你說的,運行后出現(xiàn)新的問題 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] = ... |
|
目的函數(shù)ObjFunclump中少了個xx0,加上就沒錯誤。 global theta yexp M NH xx0 x0 至于警告,看英文應該能明白意思。 盡量不采用全局變量,建議用參數(shù)方式傳遞數(shù)據(jù)。 |

木蟲 (著名寫手)
天鷹幫幫主
|
運行后: 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 。。。。。。反復出現(xiàn)。。。。。 出結果后: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 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[電化學] 070300化學調劑 +5 | 山頂見α 2026-03-25 | 5/250 |
|
|---|---|---|---|---|
|
[考研] 考研調劑求助 +7 | 13287130938 2026-03-31 | 7/350 |
|
|
[考研] 289求調劑 +3 | Acesczlo 2026-03-29 | 4/200 |
|
|
[考研] 347求調劑 +11 | 山頂見α 2026-03-25 | 11/550 |
|
|
[考研]
材料調劑
5+5
|
想要一壺桃花水 2026-03-25 | 11/550 |
|
|
[考研] 一志愿大連理工大學,機械工程學碩,341 +3 | 西瓜田的守望者 2026-03-30 | 3/150 |
|
|
[考研] 266分,求材料相關專業(yè)調劑 +10 | 哇呼哼呼哼 2026-03-30 | 12/600 |
|
|
[考研] 085601一志愿中山大學深圳材料工程330求調劑 +5 | pipiver 2026-03-30 | 5/250 |
|
|
[考研] 370求調劑 +3 | 080700調劑 2026-03-30 | 3/150 |
|
|
[考研] 085701環(huán)境工程求調劑 +11 | 多久上課 2026-03-27 | 12/600 |
|
|
[考研] 生物學學碩,一志愿湖南大學,初試成績338 +7 | YYYYYNNNNN 2026-03-26 | 9/450 |
|
|
[考研] 085602 化學工程專碩 340分求調劑 +4 | qianbai11 2026-03-29 | 4/200 |
|
|
[考研] 【求調劑】085601材料工程專碩 | 總分272 | +7 | 腳滑的守法公民 2026-03-27 | 7/350 |
|
|
[考研] 壓國家一區(qū)線,求導師收留,有恩必謝! +7 | 迷人的哈哈 2026-03-28 | 7/350 |
|
|
[考研]
|
y7czhao 2026-03-26 | 10/500 |
|
|
[考研] 265求調劑 +8 | 小木蟲085600 2026-03-27 | 8/400 |
|
|
[考研] 266求調劑 +11 | 陽陽哇塞 2026-03-27 | 12/600 |
|
|
[考研] 314求調劑 +3 | 溪云珂 2026-03-26 | 3/150 |
|
|
[考研] 考研調劑 +9 | 小蠟新筆 2026-03-26 | 9/450 |
|
|
[考研] 一志愿 南京郵電大學 288分 材料考研 求調劑 +3 | jl0720 2026-03-26 | 3/150 |
|