如何避免Matlab错误Too many output arguments
最优化方法与理论这本书的代码中有很多印刷错误的地方,大家平时要留意下复制最优化理论书上的源代码,在使用Matlab编译时可能会出现类似以下错误:`Error using gt; qmin Too many output arguments.` 这种错误通常是由于代码中输出参数数量不匹配导致的。比如书上的代码示例如下:
```matlab
function [s, phis, ds, dphi, S] qmin (phi, a, b, delta, epsilon)
s0 1;
maxj 20;
maxk 30;
big 1e6;
err 1;
k 1;
S(k) s0;
cond 0;
h 1;
ds 0.00001;
if (abs(s0) > 1e4), h abs(s0) * (1e-4);
end
while (k < maxk err > epsilon cond ~ 5)
f1 (feval(phi, s0 ds) - feval(phi, s0 - ds)) / (2 * ds);
if(f1 > 0), h -abs(h);
end
s1 s0 h;
s2 s0 2 * h;
bars s0;
phi0 feval(phi, s0);
phi1 feval(phi, s1);
phi2 feval(phi, s2);
barphi phi0;
cond 0;
j 0;
while (j < maxj abs(h) > delta cond 0)
if (phi0 < phi1), s2 s1; phi2 phi1; h 0.5 * h; s1 s0 h; phi1 feval(phi, s1);
else if (phi2 < phi1), s1 s2; phi1 phi2; h 2 * h; s2 s0 2 * h; phi2 feval(phi, 2);
else cond -1;
end
end
j j 1;
if (abs(h) > big || abs(s0) > big), cond 5;
end
end
if (cond 5), bars s1; barphi feval(phi, s1);
else d 2 * (2 * phi1 - phi0 - phi2);
if (d < 0), barh h * (4 * phi1 - 3 * phi0 - phi2)/d;
else barh h / 3; cond 4;
end
bars s0 barh; barphi feval(phi, bars);
h abs(h); h0 abs(barh); h1 abs(barh - h); h2 abs(barh - 2 * h);
if (h0 < h), h h0;
end
if (h1 < h), h h1;
end
if (h2 < h), h h2;
end
if(h 0), h barh;
end
if (h < delta), cond 1;
end
if (abs(h) > big || abs(bars) > big), cond 5;
end
err abs(phi1 - barphi);
s0 bars; k k 1; S(k) s0;
end
if (cond 2 h < delta), cond 3;
end
end
ends s0; phis feval(phi, s); ds h; dphi err;
```
解决方法
在Matlab中新建一个M文件,命名为qmin。如果在编译时遇到类似"Too many output arguments"的错误提示,仔细检查代码发现命令语句的输出参数中多了一个不必要的参数k。为了解决这个问题,只需将命令语句中的k参数删除,然后再次运行程序即可成功输出结果。
注意事项
除了代码中可能出现的编译错误外,最优化方法与理论这本书的代码中还存在许多印刷错误。因此,在复制书上的源代码时,需要注意并及时纠正可能存在的错误,以确保代码能够顺利运行。避免类似"Too many output arguments"这样的简单但影响编译的错误。
通过以上方法和注意事项,可以更好地处理Matlab代码中可能出现的错误,并确保顺利编译和执行程序。这样可以有效提高工作效率,减少不必要的麻烦。
版权声明:本文内容由互联网用户自发贡献,本站不承担相关法律责任.如有侵权/违法内容,本站将立刻删除。