Filter löschen
Filter löschen

how to solve this error in lsqnonlin of optim tool?: LSQNONLIN requires all values returned by user functions to be of data type double

9 Ansichten (letzte 30 Tage)
I am trying to use lsnonlin from optimtool for a function I have written. I am providing the function, the start point (as: [2.5*10^7 8*10^9 1.2*10^7 1.9*10^10 91287 169948 105280 152388]) and the lower and upper bounds. when I am hitting start, it gives me this error: Error running optimization. LSQNONLIN requires all values returned by user functions to be of data type double.
Can you please help me to solve this problem. If you could say what should I change in this code, it would be great. Below is my function:
function y=harman(x,data)
format long
j=1;
for T=[453 493 523]
ydata = [0.75;0.70;0.64;0.61;0.72;0.68;0.62;0.58;0.70;0.65;0.59;0.53];
R=8.13;
%Arhenious equations to relate inputs and guesses
empty=[];
k=repmat(empty,4,1);
for i=1:4
k(i)=x(i)*(exp(-1*x(i+4)/(R*T)));
end
k1=k(1);
k2=k(2);
k3=k(3);
k4=k(4);
syms a(t) b(t) c(t) d(t) e(t)
eqn1=diff(a)==(-1*(k1+k3)*a);
eqn2=diff(b)==(k1*a)-((k2+k4)*b);
eqn3=diff(c)==(k2*b);
eqn4=diff(d)==(k3*a);
eqn5=diff(e)==(k4*b);
c1=a(0)==1;
c2=b(0)==0;
c3=c(0)==0;
c4=d(0)==0;
c5=e(0)==0;
S=dsolve(eqn1, eqn2, eqn3, eqn4, eqn5, c1, c2, c3, c4, c5);
dSol(t)=S.d;
eSol(t)=S.e;
for t=[900 2400 5400 7200]
z(j)=abs(ydata(j)-(1-(dSol(t)+eSol(t))));
j=j+1;
end
end
y=sum(z.^2);
end
Thanks, Mohammad

Akzeptierte Antwort

Matt J
Matt J am 20 Jul. 2018
You are doing various symbolic manipulations. Are you sure that z evaluate to a numbers? If so, just convert
y=double( sum(z.^2) );
  8 Kommentare
Mohammad Heidari
Mohammad Heidari am 20 Jul. 2018
Perfect. It's working now. Thank you so much for the great help. You made my day.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by