[讨论] fmincon出现error

楼主: m06800825 ( )   2015-07-29 22:26:07
我要写一个找四个变量的最佳化程式
以下是主程式
=============================
x0 = [7 7 30 -0.17453292]
options=optimset('LargeScale','off')
[x,fval] = fmincon(@(x)myfun(x),x0,[],[],[],[],[5 5 20 -pi/4],[10 10 35
pi/4],[],options)
=============================
接着写myfun
=========================================
function f = myfun(x)
%UNTITLED3 Summary of this function goes here
syms k r1 r2 r3 r4 th1 th2 ac BCA DCA BCD bd CBD BDC BCE thDIP;
r2=28
x = [r1 r3 r4 th1]
for i=1:110
th2=((90-i)*pi)/180;
thDIP = k*((pi/2)-th2)
ac=sqrt(r1*r1+r2*r2-2*r1*r2*cos(th1-th2));
BCA=acos((r3*r3+ac*ac-r4*r4)/(2*r3*ac));
DCA=acos((r2*r2+ac*ac-r1*r1)/(2*r2*ac));
BCD=BCA-DCA;
bd=sqrt(r3*r3+r2*r2-2*r3*r2*cos(BCD));
CBD=acos((r3*r3+bd*bd-r2*r2)/(2*r3*bd));
BDC=acos((r2*r2+bd*bd-r3*r3)/(2*r2*bd));
BCE=CBD+BDC;
alpha(:,i)=[(BCE+thDIP)] %k=2/3
end;
for i=1:109
diff(:,i)=[((alpha(i)-alpha(i+1))^2)]
end;
% Detailed explanation goes here
f= sum(diff)
end
=============================================
跑到后面却出现
Error using fmincon (line 783)
FMINCON requires all values returned by user functions to be of data type
double.
请问这是什么原因呢?
急需版上大大解惑QAQ
作者: profyang (prof)   2015-07-29 22:35:00
我没在用syms的不太确定 但你确定你f出来是double吗?然后我既的之前给他input的x是个N*1的向量 然后在myfun中是用var1=x(1); var2=x(2); 类似这样去弄的
作者: celestialgod (天)   2015-07-30 08:18:00
f是syms 你要先用double做转换

Links booklink

Contact Us: admin [ a t ] ucptt.com