24小时热门版块排行榜    

查看: 1110  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 高分子化学与物理调剂 +6 好好好1233 2026-02-28 13/650 2026-03-02 07:27 by 好好好1233
[考研] 求调剂 +5 yunziaaaaa 2026-03-01 6/300 2026-03-01 23:57 by ccp273206157
[考研] 江苏省农科院招调剂1名 +3 Qwertyuop 2026-03-01 3/150 2026-03-01 23:18 by aaadim
[基金申请] 成果系统访问量大,请一小时后再尝试。---NSFC啥时候好哦,已经两天这样了 +4 NSFC2026我来了 2026-02-28 4/200 2026-03-01 22:37 by 铁门栓
[考研] 26考研报考西工大材料308分求调剂 +3 weizhong123 2026-03-01 3/150 2026-03-01 21:42 by 公瑾逍遥
[考研] 299求调剂 +3 Y墨明棋妙Y 2026-02-28 5/250 2026-03-01 21:01 by tangxiaotian
[考研] 306分材料调剂 +4 chuanzhu川烛 2026-03-01 5/250 2026-03-01 19:48 by 无际的草原
[考研] 298求调剂 +6 axyz3 2026-02-28 6/300 2026-03-01 19:00 by 18137688336
[考研] 281求调剂 +4 2026计算机_诚心 2026-03-01 7/350 2026-03-01 17:20 by 2026计算机_诚心
[考研] 321求调剂一志愿东北林业大学材料与化工英二数二 +4 虫虫虫虫虫7 2026-03-01 7/350 2026-03-01 16:52 by caszguilin
[考研] 0856材料求调剂 +4 麻辣鱿鱼 2026-02-28 4/200 2026-03-01 16:51 by caszguilin
[考研] 311求调剂 +6 亭亭亭01 2026-03-01 6/300 2026-03-01 15:41 by 324616
[考研] 307求调剂 +5 wyyyqx 2026-03-01 5/250 2026-03-01 15:21 by Fff-1
[考研] 304求调剂 +6 曼殊2266 2026-02-28 7/350 2026-03-01 15:14 by wjLi2017
[考博] 博士自荐 +4 kkluvs 2026-02-28 4/200 2026-03-01 10:19 by 馥安馥安
[论文投稿] 求助coordination chemistry reviews 的写作模板 10+3 ljplijiapeng 2026-02-27 4/200 2026-03-01 09:07 by babero
[基金申请] 面上模板改不了页边距吧? +5 ieewxg 2026-02-25 6/300 2026-03-01 00:10 by addressing
[考研] 304求调剂 +3 52hz~~ 2026-02-28 5/250 2026-03-01 00:00 by 52hz~~
[考研] 085600材料工程一志愿中科大总分312求调剂 +8 吃宵夜1 2026-02-28 10/500 2026-02-28 20:27 by L135790
[硕博家园] 【博士招生】太原理工大学2026化工博士 +4 N1ce_try 2026-02-24 8/400 2026-02-26 08:40 by N1ce_try
信息提示
请填处理意见