请会MAtlab的帮帮我!感激不尽!急~~~

来源:百度知道 编辑:UC知道 时间:2024/06/20 02:25:33
有这么个程序:
function y=totle(x)
y=-sqrt(x(1))-sqrt(x(2))-sqrt(x(3))-sqrt(x(4));
A=[1.1,1,0,0;1.21,1.1,1,0;1.331,1.21,1.1,1]
b=[440,484,532.4]’;
lb=[0,0,0,0]’;
ub=[440,1000,1000,1000]’;
x0=[100,100,100,100]’;
[x,faval]=fmincon(‘totle’,x0,A,b,[],[],lb,ub)
然后呢,直接运行的话,MATLAB会说:??? function y=totle(x)
|
Error: Function definitions are not permitted at the prompt or in scripts.
这个我搜了一下,是要创建一个m文件,把那个function什么的定义一下吧?可是,m文件里面应该写什么?然后怎么弄才能出结果呢?!
哎呀请各位大侠帮帮忙吧我什么都不懂!
这个程序结果应该是下面这样:(书上写的)

x=
84.2435
106.6351
128.9034
148.2397
fval=
-43.0821

改成这样就可以了:

totle=@(x) -sqrt(x(1))-sqrt(x(2))-sqrt(x(3))-sqrt(x(4));
A=[1.1,1,0,0;1.21,1.1,1,0;1.331,1.21,1.1,1];
b=[440,484,532.4];
lb=[0,0,0,0];
ub=[440,1000,1000,1000]';
x0=[100,100,100,100]';
[x,faval]=fmincon(totle,x0,A,b,[],[],lb,ub)

结果:
Optimization terminated: Magnitude of directional derivative in search
direction less than 2*options.TolFun and maximum constraint violation
is less than options.TolCon.
Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
3

x =

84.2442
107.6353
128.9030
148.2390

faval =

-43.0821

你编的只是一个函数,你需要调用它才能运行,最简单的方法就是在matlab的命令窗口中直接输入:totle(84.2435,106.6351,128.9034,148.2397)

就可以了!

这是两部分,

% totle.m
function y=totle(x)
y=-sqrt(x(1))-sqrt(x(2))-sqrt(x(3))-sqrt(x(4));