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

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

 Accepted Answer

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 Comments

Thank you so much. Yes Z evaluates to numbers and it sounds like the line you changes solved this issue but now I am getting this error: Error running optimization. The Levenberg-Marquardt algorithm does not handle bound constraints and the trust-region-reflective algorithm requires at least as many equations as variables; aborting.
I think the equations are enough to solve the problem and I am getting responses by other methods like fminsearch. So I am not sure what kind of changes I need to apply here.
Thanks again. By doing this, however, the initial error appeared again; LSQNONLIN requires all values returned by user functions to be of data type double.
Same solution. Convert from symbolic to double before returning the output.
Sorry for too many questions and thanks for answering. I got this error this time: "Cannot set option: HybridInterval"
I don't see you setting any options in the code that you've posted.
Perfect. It's working now. Thank you so much for the great help. You made my day.

Sign in to comment.

More Answers (0)

Asked:

on 20 Jul 2018

Commented:

on 20 Jul 2018

Community Treasure Hunt

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

Start Hunting!