# 10 麦片

050

5主题 0最佳答案

 运行后显示： Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue 下面是我写的代码，刚学matlab，有错误见谅 %目标函数function f=objfun(A)f=0.1*(360*A(1)+360*A(2)+360*A(3)+360*A(4)+360*A(5)+360*A(6)+sqrt(2)*360*A(7)+sqrt(2)*360*A(8)+sqrt(2)*360*A(9)+sqrt(2)*360*A(10)); %约束条件function [c,ce]=nonlcon(A) %不等式约束h=((100-c(8)/sqrt(2))/A(1))-25;(-(100-c(8)/sqrt(2))/A(1))-25; (-c(10)/sqrt(2))/A(2)-25;(-(-c(10)/sqrt(2))/A(2))-25; (-100-200-c(8)/sqrt(2))/A(3)-25;-(-100-200-c(8)/sqrt(2))/A(3)-25; (-100-c(10)/sqrt(2))/A(4)-25;-(-100-c(10)/sqrt(2))/A(4)-25; (-100-c(8)/sqrt(2)-c(10)/sqrt(2))/A(5)-25;-(-100-c(8)/sqrt(2)-c(10)/sqrt(2))/A(5)-25; (-c(10)/sqrt(2))/A(6)-25;-(-c(10)/sqrt(2))/A(6)-25; (sqrt(2)*(100+100)+c(8))/A(7)-25;-(sqrt(2)*(100+100)+c(8))/A(7)-25; ((b1*a22-a12*b2)/(a11*a22-a12*a21))/A(8)-25;-((b1*a22-a12*b2)/(a11*a22-a12*a21))/A(8)-25; (sqrt(2)*100+c(10))/A(9)-75;-(sqrt(2)*100+c(10))/A(9)-75 ((b2*a11-a21*b1)/(a11*a22-a12*a21))/A(10)-25;-((b2*a11-a21*b1)/(a11*a22-a12*a21))/A(10)-25; %等式约束a11=(1/A(1)+1/A(3)+1/A(5)+2*sqrt(2)/A(7)+2*sqrt(2)/A(8));a12=1/A(5);a21=1/A(5);a22=(1/A(2)+1/A(4)+1/A(5)+1/A(6)+2*sqrt(2)/A(9)+2*sqrt(2)/A(10));b1=sqrt(2)*(100/A(1)-(100+200)/A(3)-100/A(5)-2*sqrt(2)*(100+100)/A(7));b2=((-sqrt(2)*100)/A(4)-(sqrt(2)*100)/A(5)-(4*100)/A(9)); ce(1)=100-c(8)/sqrt(2);ce(2)=-c(10)/sqrt(2);ce(3)=-100-200-c(8)/sqrt(2);ce(4)=-100-c(10)/sqrt(2);ce(5)=-100-c(8)/sqrt(2)-c(10)/sqrt(2);ce(6)=-c(10)/sqrt(2);ce(7)=sqrt(2)*(100+100)+c(8);ce(8)=(b1*a22-a12*b2)/(a11*a22-a12*a21);ce(9)=sqrt(2)*100+c(10);ce(10)=(b2*a11-a21*b1)/(a11*a22-a12*a21); %MAIN clc;clear;x0=[1,1,1,1,1,1,1,1,1,1];lb=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1];ub=[10,10,10,10,10,10,10,10,10,10];Aeq=[];beq=[];A=[];b=[];Aopt=fmincion('objfun',x0,A,b,Aeq,beq,lb,ub,'nonclon');options=optimset('Display','iter'); [size=13.3333px]题目大概是这样 p1=p2=100lb   a=b=360ft 许用应力9号杆是正负75之间其他1-8，10 是正负25

# 10 麦片

050

5主题 0最佳答案
楼主| 发表于 2017-12-7 18:46:04 | 显示全部楼层
 最优化 杆断面积

# 8658 麦片

7主题 1923最佳答案
• 关注者： 444

 hl328328 发表于 2017-12-7 18:46 最优化 杆断面积 仔细看看帮助里面nonlcon函数怎么写的，里面有例子。你这错的太多了。

# 10 麦片

050

5主题 0最佳答案
楼主| 发表于 2017-12-7 20:16:00 | 显示全部楼层
 jingzhaos 发表于 2017-12-7 19:19 仔细看看帮助里面nonlcon函数怎么写的，里面有例子。你这错的太多了。 function handle to a file or to an anonymous function,such as mycon: x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon) where mycon isa MATLAB function such as function [c,ceq] = mycon(x) c = ...     % Compute nonlinear inequalities at x. ceq = ...   % Compute nonlinear equalities at x. Ifthe gradients of the constraints can also be computed and the GradConstr optionis 'on', as set by options = optimoptions('fmincon','GradConstr','on') 汗颜，能否帮我找个例子参考下   我只找到这个  感恩

# 10 麦片

050

5主题 0最佳答案
楼主| 发表于 2017-12-7 20:18:38 | 显示全部楼层
 jingzhaos 发表于 2017-12-7 19:19 仔细看看帮助里面nonlcon函数怎么写的，里面有例子。你这错的太多了。 function handle to a file or to an anonymous function,such as mycon: x = fmincon(@myfun,x0,A,b,Aeq,beq,lb,ub,@mycon) where mycon isa MATLAB function such as function [c,ceq] = mycon(x) c = ...     % Compute nonlinear inequalities at x. ceq = ...   % Compute nonlinear equalities at x. Ifthe gradients of the constraints can also be computed and the GradConstr optionis 'on', as set by options = optimoptions('fmincon','GradConstr','on') 汗颜，能否帮我找个例子参考下   我只找到这个  感恩

# 8658 麦片

7主题 1923最佳答案
• 关注者： 444

 帮助里面现成的例子 https://cn.mathworks.com/help/optim/ug/fmincon.html?searchHighlight=fmincon&s_tid=doc_srchtitle

# 10 麦片

050

5主题 0最佳答案
楼主| 发表于 2017-12-7 22:58:10 | 显示全部楼层
 jingzhaos 发表于 2017-12-7 21:26 帮助里面现成的例子 https://cn.mathworks.com/help/optim/ug/fmincon.html?searchHighlight=fmincon&s_tid ... 十分感谢！ 我看完之后  改成这样  可以运行了，但是ans都是 负数是为何 %目标函数 function f=objfun(A) f=0.1*(360*A(1)+360*A(2)+360*A(3)+360*A(4)+360*A(5)+360*A(6)+sqrt(2)*360*A(7)+sqrt(2)*360*A(8)+sqrt(2)*360*A(9)+sqrt(2)*360*A(10)); %约束条件 function [c,ceq]=mycon(A) %不等式约束 a11=(1/A(1)+1/A(3)+1/A(5)+(2*sqrt(2))/A(7)+(2*sqrt(2))/A(8)); a12=1/A(5); a21=1/A(5); a22=(1/A(2)+1/A(4)+1/A(5)+1/A(6)+(2*sqrt(2))/A(9)+(2*sqrt(2))/A(10)); b1=sqrt(2)*(100/A(1)-(100+200)/A(3)-100/A(5)-(2*sqrt(2)*(100+100))/A(7)); b2=((-sqrt(2)*100)/A(4)-(sqrt(2)*100)/A(5)-(4*100)/A(9)); N(8)=(b1*a22-a12*b2)/(a11*a22-a12*a21); N(10)=(b2*a11-a21*b1)/(a11*a22-a12*a21); c=(100-N(8)/sqrt(2))/A(1)-25; -((100-N(8)/sqrt(2))/A(1))-25; (-N(10)/sqrt(2))/A(2)-25; -((-N(10)/sqrt(2))/A(2))-25; (-100-200-N(8)/sqrt(2))/A(3)-25; -((-100-200-N(8)/sqrt(2))/A(3))-25; (-100-N(10)/sqrt(2))/A(4)-25; -((-100-N(10)/sqrt(2))/A(4))-25; (-100-N(8)/sqrt(2)-N(10)/sqrt(2))/A(5)-25; -((-100-N(8)/sqrt(2)-N(10)/sqrt(2))/A(5))-25; (-N(10)/sqrt(2))/A(6)-25; -((-N(10)/sqrt(2))/A(6))-25; (sqrt(2)*(100+100)+N(8))/A(7)-25; -((sqrt(2)*(100+100)+N(8))/A(7))-25; N(8)/A(8)-25; -(N(8)/A(8))-25; (sqrt(2)*100+N(10))/A(9)-75; -((sqrt(2)*100+N(10))/A(9))-75 N(10)/A(10)-25; -(N(10)/A(10))-25; %等式约束 ceq=[] %MAIN clc;clear; x0=[1,1,1,1,1,1,1,1,1,1]; lb=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]; ub=[10,10,10,10,10,10,10,10,10,10]; Aeq=[];beq=[];A=[];b=[]; A=fmincon(@objfun,x0,A,b,Aeq,beq,lb,ub,@mycon);
 您需要登录后才可以回帖 登录 | 注册 本版积分规则 回帖后跳转到最后一页