24小时热门版块排行榜    

Znn3bq.jpeg
查看: 1147  |  回复: 3

smileheaven

金虫 (小有名气)

[求助] MATLAB非线性优化拟合怎么改才正确

想对函数z求一下x和Fval的最优解,其中x1的取值范围在0.3到1.8,x2的取值范围在1到2,均为闭区间。以下是自己编的程序
    function [x fval history] = myproblem1(x0)
        history = []
        options = optimset('OutputFcn', @myoutput)
        [x fval] = fminsearch(@objfun, x0,options)
        function stop = myoutput(x,optimvalues,state)
            stop=[]
            if state == 'iter'
                history = [history; x]
            end
        end
        function z = objfun(x)
            z = 124.5993*x(1)^2-371.4544*x(1)+712.1124-1359.4398*x(2)^2+4028.7765*x(2)-2244.7098
        end
    end
但是返回的错误却是
??? In an assignment  A( = B, the number of elements in A and B
must be the same.

Error in ==> callAllOptimOutputFcns at 12
    stop(i) = feval(OutputFcn{i},xOutputfcn,optimValues,state,varargin{:});

Error in ==> fminsearch>callOutputAndPlotFcns at 478
            stop = callAllOptimOutputFcns(outputfcn,xOutputfcn,optimValues,state,varargin{:}) || stop;

Error in ==> fminsearch at 213
    [xOutputfcn, optimValues, stop] = callOutputAndPlotFcns(outputfcn,plotfcns,v(:,1),xOutputfcn,'init',itercount, ...

Error in ==> myproblem1 at 4
        [x fval history] = fminsearch(@objfun, x0,options)
请各位虫哥虫姐帮忙看看,谢谢啦
回复此楼

» 猜你喜欢

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

探求、奋斗、不达目的誓不罢休
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

csgt0

荣誉版主 (著名写手)

彩色挂图

【答案】应助回帖

★ ★ ★ ★ ★
感谢参与,应助指数 +1
smileheaven: 金币+5, ★★★★★最佳答案, 谢谢 2012-06-11 12:18:10
stop=false;


结果
state =

done


Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: -31981372058113011000000000000000000000000000000000000000000000000000000000000000000000000000.000000


x =

  1.0e+044 *

    0.1905    1.5349


fval =

-3.1981e+091


result =

  1.0e+044 *

    0.1905    1.5349
showmethemoney
2楼2012-05-25 13:35:05
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

smileheaven

金虫 (小有名气)

引用回帖:
2楼: Originally posted by csgt0 at 2012-05-25 13:35:05
stop=false;


结果
state =

done


Exiting: Maximum number of function evaluations has been exceeded
         - increase MaxFunEvals option.
         Current function value: -319813720581 ...

那为什么我这边计算不出来呢?什么地方错了,如果可能,还请您指教,谢谢
探求、奋斗、不达目的誓不罢休
3楼2012-05-25 14:01:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

csgt0

荣誉版主 (著名写手)

彩色挂图

【答案】应助回帖

stop只能是false或true,然后通过stop来判断是否跳出。你不用它就设为false,而不是[];再用state判断

Stop Flag

The output argument stop is a flag that is true or false. The flag tells the optimization function whether the optimization should quit or continue. The following examples show typical ways to use the stop flag.
Stopping an Optimization Based on Data in optimValues

The output function can stop an optimization at any iteration based on the current data in optimValues. For example, the following code sets stop to true if the objective function value is less than 5:

function stop = myoutput(x, optimValues, state)
stop = false;
% Check if objective function is less than 5.
if optimValues.fval < 5
    stop = true;
end

Stopping an Optimization Based on GUI Input

If you design a GUI to perform optimizations, you can make the output function stop an optimization when a user clicks a Stop button on the GUI. The following code shows how to do this, assuming that the Stop button callback stores the value true in the optimstop field of a handles structure called hObject stored in appdata.

function stop = myoutput(x, optimValues, state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');
showmethemoney
4楼2012-05-25 14:25:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 smileheaven 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 332求调剂 +14 蕉蕉123 2026-04-10 14/700 2026-04-12 00:27 by 蓝云思雨
[考研] 材料考研调剂 +24 云木达达 2026-04-11 26/1300 2026-04-12 00:23 by 蓝云思雨
[考研] 286求调剂 +25 Faune 2026-04-06 25/1250 2026-04-11 23:23 by labixiaoqiao
[考研] 求调剂,985材料与化工348分 +7 涵竹刘 2026-04-11 9/450 2026-04-11 23:13 by 溪涧流水
[考研] 调剂求收留 +21 果然有我 2026-04-10 22/1100 2026-04-11 21:37 by 蓝云思雨
[考研] 291分调剂 +5 上岸小莹加油 2026-04-09 6/300 2026-04-11 21:06 by 逆水乘风
[考研] 290调剂生物0860 +17 哇哈哈,。 2026-04-11 19/950 2026-04-11 20:20 by dongdian1
[考研] 求调剂 +10 璃茉一定上岸 2026-04-10 10/500 2026-04-11 13:31 by 1005715100
[考研] 283求调剂,工科! +12 苏打水7777 2026-04-08 12/600 2026-04-11 10:28 by 逆水乘风
[考研] 还有化工二轮调剂的学校吗 5+14 化工人999 2026-04-09 48/2400 2026-04-11 10:27 by 89436494
[考研] 288求调剂 +15 代fish 2026-04-09 16/800 2026-04-11 10:26 by wwj2530616
[考研] 机械专硕270求调剂,接受跨专业 +12 老师看看我吧aba 2026-04-09 14/700 2026-04-11 10:21 by laoshidan
[考研] 材料085601调剂 +25 何润采123 2026-04-10 27/1350 2026-04-10 23:17 by Ftglcn90
[考研] 本9 一志愿西工大085601 324求调剂 +5 wysyjs25 2026-04-10 5/250 2026-04-10 16:57 by luoyongfeng
[考研] 一志愿0703化学招61最终排名62化学求调剂 +24 招61排名62 2026-04-07 28/1400 2026-04-10 16:15 by yx54321
[考研] 本科211 工科085400 280分求调剂 可跨专业 +3 LZH(等待调剂中 2026-04-09 3/150 2026-04-09 21:29 by wutongshun
[考研] 275 求调剂 +8 Lei812514 2026-04-07 8/400 2026-04-08 12:46 by chemisry
[考研] 求调剂 +10 chenxrlkx 2026-04-05 10/500 2026-04-06 11:31 by 猪会飞
[考研] 319求调剂 +3 handrui 2026-04-05 3/150 2026-04-06 09:33 by jp9609
[考研] 296求调剂 +3 汪!?! 2026-04-05 4/200 2026-04-05 20:13 by 啵啵啵0119
信息提示
请填处理意见