我要写一个找四个变量的最佳化程式
以下是主程式
=============================
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