楼主: 邹华伟
2261 29

[其他] 悬赏50金币求解如何用matlab编写如下程序 [推广有奖]

21
邹华伟 发表于 2016-4-14 21:04:56
Weierstrass. 发表于 2016-4-14 00:39
// 存储为myfun.m  切记,切记
function y= myfun(x)
y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x ...
你好 你的答案是正确的 十分感谢 我想问一下如果将目标函数改为y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+x(1)+c*(x(2)+x(3)+x(4)+x(5)+x(6)+x(7));c为不定常数,取值为(0,100)那么该如何编写程序 在此感谢

22
邹华伟 发表于 2016-4-14 21:05:43
seaknight 发表于 2016-4-13 21:20
如果不行的话,你可以加入一个X0=[1;1;1;1;1;1;1],然后就把A之前的[],变成X0
我想问一下如果将目标函数改为y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+x(1)+c*(x(2)+x(3)+x(4)+x(5)+x(6)+x(7));c为不定常数,取值为(0,100)那么该如何编写程序 在此感谢

23
Weierstrass. 学生认证  发表于 2016-4-14 23:23:11
在函数定义中加入 循环   
function.....
for c=(0:100)
.....其他不变。最后x0  初始值很重要的,确定只限制x(2)=0?  

function y= myfun(x)
for c=(1:100)
y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+x(1)+c*(x(2)+x(3)+x(4)+x(5)+x(6)+x(7));
end

24
邹华伟 发表于 2016-4-15 10:19:25
Weierstrass. 发表于 2016-4-14 23:23
在函数定义中加入 循环   
function.....
for c=(0:100)
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Error: File: F:\MATLAB7\myfun.m Line: 3 Column: 102
This statement is incomplete.
这是咋的了

25
Weierstrass. 学生认证  发表于 2016-4-15 17:03:22
函数定义不完整,具体看myfun.m的第3行。

26
邹华伟 发表于 2016-4-15 17:10:29
Weierstrass. 发表于 2016-4-15 17:03
函数定义不完整,具体看myfun.m的第3行。
function y=myfun(x)
for c=(0:100)
y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+c*(x(1)+x(2)+x(3)+x(4)+x(5)+x(6)+x(7));
错了吗

27
邹华伟 发表于 2016-4-15 17:33:21
Weierstrass. 发表于 2016-4-15 17:03
函数定义不完整,具体看myfun.m的第3行。
我的具体程序是这样的  你看看哪里出错了?
function y=myfun(x)
for c=(0:100)
y=-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+c*(x(1)+x(2)+x(3)+x(4)+x(5)+x(6)+x(7));

A=[1, 0, 0, 0.65, 0.16, 0, 0.16;
              0, -1, 0, -0.65,0,-0.34,-0.16;
             0, 0, -1, 0, -0.16, -0.34, -0.16];
         B=[4.47;-3.91;-4.6];
Aeq=[];    Beq=[];      
Lb=[0;0;0;0;0;0;0];
[x,fval]=fmincon(@myfun,  0.000000001*ones(1,7)', A,B,Aeq,Beq,Lb);
xmin=x
fmax=-fval
提示我??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Error: File: F:\MATLAB7\myfun2.m Line: 3 Column: 102
This statement is incomplete.

28
邹华伟 发表于 2016-4-15 18:37:33
Weierstrass. 发表于 2016-4-15 17:03
函数定义不完整,具体看myfun.m的第3行。
十分感谢!的确是少end。我想研究的是c取不同值时候 原函数的最优化问题:就是说当c=0,c=0.5,c=1....c=100,分别求出函数的最大值然后画出c的取值与最大值的图像以及最大值时候对应的x1,x2....x7的取值图像

29
Weierstrass. 学生认证  发表于 2016-4-15 23:06:29
含参数的非线性规划,感觉c 如果定义为 x8可能更好一点。或者可以重新考虑模型。
附件无法上传,(仅从数学角度看发现结果不太好,请讨论
  1. function [x,fval] =  runnested(c,x0)
  2.   %定义非线性调用函数 runnested    c为补充变量。
  3.        A=[1, 0, 0, 0.65, 0.16, 0, 0.16;
  4.          0, -1, 0, -0.65,0,-0.34,-0.16;
  5.          0, 0, -1, 0, -0.16, -0.34, -0.16];
  6.          B=[4.47;-3.91;-4.6];
  7.        Aeq=[]; Beq=[];      
  8.        Lb=[0;0;0;0;0;0;0];  
  9.       [x,fval] = fmincon(@nesetfun,x0,A,B,Aeq,Beq,Lb);
  10. % nesetfun function that computes the objective function     
  11.     function y =nesetfun(x)
  12.         %  优化函数  x为变量,c 为不定常量  如果可能---c可以为x(8)---?
  13.              y =-350*(1-exp(-(x(1)+0.65*x(4)+0.16*x(5)+0.16*x(7))))-200-400+x(1)+c*(x(2)+x(3)+x(4)+x(5)+x(6)+x(7));   
  14.     end
  15. end
复制代码
  1. format short
  2. xx=zeros(101,7);yy=zeros(101,1);x0=zeros(1,7);
  3. for c=0:100 %步长为1。
  4.     [x,favl]=runnested(c,x0);
  5.     xx(c+1,:)=x; %将x, max保存到数组
  6.     yy(c+1)=-favl;
  7. end
  8. c=(0:100);plot(c,yy);
  9. hold on
  10. plot(xx,yy)
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-3 01:17