data=[0;
0;
0;
37248;
58649;
85974;
132620;
200698;
266406;
325423;
383975;
443234;
498503;
558777;
612881;
659367;
710580;
749772;
794515;
847288;
910312;
963746;
1016473;
1060359;
1097173;
1135293;
1176867;
1212260;
1239208;
1267657]
function res= Bass(x,v,data )
%objective function
F=[ -data+ (x(1)*x(2)*(1 - exp( x(2) + (( X(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*X(4))/ x(3)-x(4))*exp(-x(5)*v)))]
res=sum(F.^2)
%constriant
function [c ceq]= nonlinc(x,v,data)
c=[];
ceq=[ -data +( x(1)*x(2)*(1 - exp( x(2) + (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))]
fun=@Bass;
v=100;
nl=@nonlinc;
lb=[20,0,0,0,0]
ub=[inf,1,1,1,inf]
x0=[ 0,0,0,0,0]
j=fmincon(fun,x0,[],[],[],[],lb,ub,nl)
my aim was to find the parameters of a system on non_linear equations. I used the Res as my objective function and the constraints were the equations. I didn't use fsolve because I had boundaries.data is a vector . I have corrected this several times but results are always : Error using Bass (line 3) Not enough input arguments.
Error in fmincon (line 564) initVals.f = feval(funfcn{3},X,varargin{:});
Error in Untitled2 (line 41) x= fmincon(fun,x0,A,b,Aeq,beq,lb,ub);
Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
>> I would be glad for a little assistance.Thank you :)

6 件のコメント

Torsten
Torsten 2018 年 7 月 13 日
You want to force "fmincon" find parameters x(1),...,x(5) such that one single number (namely your expression
(x(1)*x(2)*(1 - exp( x(2) + (( X(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ (1+ (( x(3)*X(4))/ x(3)-x(4))*exp(-x(5)*v)))
)
is simultaneously equal to all your data points.
That doesn't make sense.
Best wishes
Torsten.
Honey Adams
Honey Adams 2018 年 7 月 13 日
編集済み: Honey Adams 2018 年 7 月 13 日
its suppose to be a vector of equations and i cant use fsolve to because I have constraints
Torsten
Torsten 2018 年 7 月 13 日
For simplicity, let's assume that
data=[3 4 5 6]
and your expression is
x(1)+3*x(2).
Then your vector of equations is
3 = x(1)+3*x(2)
4 = x(1)+3*x(2)
5 = x(1)+3*x(2)
6 = x(1)+3*x(2)
Thus one single number (x(1)+3*x(2)) shall approximate 3,4,5 and 6 simultaneously.
This doesn't make sense.
Best wishes
Torsten.
Honey Adams
Honey Adams 2018 年 7 月 13 日
I am rectifying the original equation.Will post it when an try again
Honey Adams
Honey Adams 2018 年 7 月 13 日
I found the error in the original equation.its is quite complex
P*m*((1-e^(-(p+(q_m q_0)/(q_(0+) (q_m-q_0)e^(-vw) ))*t)))/(P+ ((q_m q_0)/(q_(0+) (q_m-q_0)e^(-vw) ))*e^(-(P+ ((q_m q_0)/(q_(0+) (q_m-q_0 ) e^(-vw) ))*t) )
I then tried to define the function using the general equation. When I attempted to test my function I was told Error using exp Not enough input arguments.
Error in Bass (line 5) F=[-data + (x(1)*x(2)*(1- exp*(-x(1)- ((x(3)*x(4))/(x(4)+(x(3)-x(4))*exp(-x(5)*v))))*t)/...
Honey Adams
Honey Adams 2018 年 7 月 13 日
t was omitted in the first case and it is a vector from 1 to 30.

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2018 年 7 月 13 日

0 投票

function main
xdata=1:30;
ydata=[0,0,0,37248,58649,85974,132620,200698,266406,325423,383975,443234,498503,558777,612881,659367,710580,749772,794515,847288,910312,963746,1016473,1060359,1097173,1135293,1176867,1212260,1239208,1267657];
v=100;
fun=@(x,xdata)Bass(x,xdata,v);
lb=[20,0,0,0,0]
ub=[inf,1,1,1,inf]
x0=[0,0,0,0,0]
j=lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
end
%objective function
function F = Bass(x,xdata,v)
F=x(1)*x(2)*(1-exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v)))*xdata))./...
(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))*exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v)))*xdata));
end
You can't specify every single equation as a constraint. Minimizing the objective function does the job.
Best wishes
Torsten.

32 件のコメント

Honey Adams
Honey Adams 2018 年 7 月 13 日
編集済み: Matt J 2018 年 7 月 14 日
function main
t=1:30;
ydata=[0,0,0,37248,58649,85974,132620,200698,266406,325423,383975,443234,498503,558777,612881,659367,710580,749772,794515,847288,910312,963746,1016473,1060359,1097173,1135293,1176867,1212260,1239208,1267657];
v=1000;
fun=@(x,t)Bassmodel(x,t,v);
lb=[20,0,0,0,0];
ub=[inf,1,1,1,inf];
x0=[20,0,0,0,300];
x = lsqcurvefit(fun,x0,t,ydata,lb,ub)
end
%objective function
function F = Bassmodel(x,t,v)
F=[(x(1)*x(2)*((1-exp(-x(1)-((x(3)*x(4))/(x(4)+(x(3)-x(4))*exp(-x(5)*v))))*t)/...
(x(1) + ((x(3)*x(4)) / (x(4)+((x(3)-x(4))*exp(-x(5)*v)))) * exp(-x(1) - ((x(3)*x(4)) /( x(4)+(x(3)-x(4))*exp(-x(5)*v))))*t)))]
end
Dear Torsten,
I corrected my objective function and tested it.it works fine but when I try to run the lsqcurvefit this is what I get
Warning: Rank deficient, rank = 0, tol = NaN.
> In Bassmodel at 4
In main>@(x,t)Bassmodel(x,t,v) at 5
In lsqcurvefit at 198
In main at 9
F =
0
Error using lsqcurvefit (line 247)
Function value and YDATA sizes are not equal.
Error in main (line 9)
x = lsqcurvefit(fun,x0,t,ydata,lb,ub)
Honey Adams
Honey Adams 2018 年 7 月 14 日
I run the model just as you described but it didn't work out well.:((
Stephan
Stephan 2018 年 7 月 15 日
編集済み: Stephan 2018 年 7 月 15 日
Hi,
there were 2 more problems in the new solution, use:
F=x(1)*x(2)*(1-exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t))./...
(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))*exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t))
The function you were using returs a scalar, but you want it to return a vevtor of the same size of t and data.
Also do not use:
x0=[20,0,0,0,300];
because this returns NaN. If you use:
lb=[0,0,0,0,0];
ub=[Inf, Inf, Inf, Inf, Inf];
x0=[20,1,1,1,300];
you get:
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the default value of the function tolerance.
<stopping criteria details>
x =
1.0e+06 *
0.0000 1.3829 0.0000 0.0000 0.0003
which is a good result, as you can see on the plot:
Best regards
Stephan
Honey Adams
Honey Adams 2018 年 7 月 16 日
編集済み: Honey Adams 2018 年 7 月 16 日
Stephan thank you soo much. I almost gave up. But my concern is that the variables x(2),x(3), and x(4) are to be in the ranges from[0-1].I get poor results when I set the upper boundaries at 1.
Stephan
Stephan 2018 年 7 月 16 日
Hi,
the bounds you specify actually lead to very bad results. I saw that and that was the reason to change the bounds and see what happens.
Now that you have a working code, you can try more settings.
You might also consider whether the equation is coded correctly, whether there might be a problem with the units or a scaling problem, or if your data is bad.
I can not really help you with this, as long as it is not a problem regarding matlab and there could be many causes why the bounds which should work are not leading to the expected result.
I wish you success.
Best regards
Stephan
Honey Adams
Honey Adams 2018 年 7 月 16 日
Do you think a diffrent solver should be used rather than lsqcurvefit.Hoping to hear from you soon .
Stephan
Stephan 2018 年 7 月 16 日
編集済み: Stephan 2018 年 7 月 16 日
Hi again,
see this "improved" version of the script:
%lsqcurvefit
t=1:30;
ydata=[0,0,0,37248,58649,85974,132620,200698,266406,325423,383975,443234,498503,558777,612881,659367,710580,749772,794515,847288,910312,963746,1016473,1060359,1097173,1135293,1176867,1212260,1239208,1267657];
v=1000;
fun=@(x,t)Bassmodel(x,t,v);
lb=[0,0,0,0,0];
ub=[1, Inf, 1, 1, Inf];
x0=[20,1,1,1,300];
options = optimoptions(@lsqcurvefit,'Display','off');
x = lsqcurvefit(fun,x0,t,ydata,lb,ub,options);
y_val = Bassmodel(x,t,v);
% lsqnonlin
t=1:30;
ydata=[0,0,0,37248,58649,85974,132620,200698,266406,325423,383975,443234,498503,558777,612881,659367,710580,749772,794515,847288,910312,963746,1016473,1060359,1097173,1135293,1176867,1212260,1239208,1267657];
v=1000;
lb=[0,0,0,0,0];
ub=[1, Inf, 1, 1, Inf];
x0=x;
fun = @(x)ydata-(x(1)*x(2)*(1-exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t))./...
(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))*exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t)));
options2 = optimoptions(@lsqnonlin,'Display','off');
x2 = lsqnonlin(fun,x0,lb,ub,options2);
y_val2 = Bassmodel(x2,t,v);
% plot results
plot(t,ydata,'x')
hold on
plot(t,y_val,'o')
hold on
plot(t,y_val2,'*')
hold off
%show results
disp(['x2(1): ', num2str(x2(1))])
disp(['x2(2): ', num2str(x2(2))])
disp(['x2(3): ', num2str(x2(3))])
disp(['x2(4): ', num2str(x2(4))])
disp(['x2(5): ', num2str(x2(5))])
%objective function for lsqcurvefit
function [F] = Bassmodel(x,t,v)
F=x(1)*x(2)*(1-exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t))./...
(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))*exp(-(x(1)+x(3)*x(4)/(x(4)+(x(3)-x(4))*exp(-x(5)*v))).*t));
end
What does it do? At first in calculates the same what you already got. Result is vector x. Then it uses lsqnonlin to calcualte x again as x2, but as x0 input for this we use the result x from the first run. Then it plots all 3 versions (original, lsqcurvefit & lsqnonlin) into one graph and writes the new x-values (x2) into the command window.
Note: Using lsqnonlin is equivalent to lsqcurvefit - the small differences are beause of the start Point x0. If you start both with the same x0 you will get the exactly same results. The same result would be possible to reach by running lsqcurvefit in a loop with updating x0 from x of the run before)
More interesting is what you get, when you look to the results for x(1)...x(5):
x2(1): 0.01122
x2(2): 1382939.8173
x2(3): 0.15215
x2(4): 1
x2(5): 300
You have no chance to get x(2) between [0...1] without getting a very poor result. But x(1), x(3) & x(4) are in the interval of [0...1]
--> Is it possible that by writing the equation to matlab code you confused x(1) with x(2)??? This would explain the problem.
I recommend you check this.
Best regards
Stephan
Honey Adams
Honey Adams 2018 年 7 月 16 日
I am checking through the code.THANK YOU for your feedback.I am going to rewrite the whole equation again.
Stephan
Stephan 2018 年 7 月 16 日
編集済み: Stephan 2018 年 7 月 16 日
Hi,
just to check this again - you have:
P = x(1);
m = x(2);
q_m = x(3);
q_0 = x(4);
w = x(5);
with m, q_m and q_0 between [0...1]
???
I rewrote your objective function a Little bit:
function [F] = Bassmodel(x,t,v)
P = x(1);
m = x(2);
q_m = x(3);
q_0 = x(4);
w = x(5);
F = -(P.*m.*(exp(-t.*(P+(q_0.*q_m)./(q_0-exp(-v.*w).*(q_0-q_m))))-1.0))./(P+(q_0.*q_m.*exp(-t.*(P+(q_0.*q_m)./(q_0-exp(-v.*w).*(q_0-q_m)))))./(q_0-exp(-v.*w).*(q_0-q_m)));
end
I tested this function in the script and i get the same results we already know- so it works in principal. Now it is a little clearer which x value means which function parameter. With this setting you get the solution for x already mentioned above, which does not meet your expectations.
If you should find out, that it is P which should be x(2) and not m, then copy that code into your script and change the assignment from your Parameters to x(i).
I attached the way i "build" this function for transparency.
Best regards
Stephan
Honey Adams
Honey Adams 2018 年 7 月 16 日
q_m,q_0,p are between [0 and 1]
Stephan
Stephan 2018 年 7 月 16 日
Then you should be happy now ;-)
Honey Adams
Honey Adams 2018 年 7 月 16 日
qm, go, and w =[0:1] not yet, iam trying to implement it your way with the syms function.i was explaining the ranges of the parameters.Its a paper I used to proved the equation and followed their steps to achieve the same results I proved the formula from their description, I am trying to rewrite the equation. But the whole steps makes sense now Thank you so much, Stephan.My other question is can we use the genetic algorithm if i don't get the same result dues to the boundaries i set :
Stephan
Stephan 2018 年 7 月 16 日
編集済み: Stephan 2018 年 7 月 16 日
In the above solution w=300 - is that not inside the bounds?
And yes i think you could use ga - but im sure we did it - didnt we? Whats wrong now? Im confused - please clarify
Honey Adams
Honey Adams 2018 年 7 月 17 日
You are a genius. Thank you soooo much. It works .its just the variable assignment I missed. And thank you for the perspective from the lsqnonlin.i feel like crying :):):):):):):):).Thank you soooooo much for your patience. By the way, did you write the function in the file you shared on Matlab .
Honey Adams
Honey Adams 2018 年 7 月 17 日
The equation equation has no problems.
Stephan
Stephan 2018 年 7 月 17 日
Eureka!
yes i wrote the honey-fun function as a live script, because i thought that there is just an assignment which is wrong...
Honey Adams
Honey Adams 2018 年 7 月 17 日
編集済み: Honey Adams 2018 年 7 月 17 日
Yh i just googled it.its not opening on my matlab 2014a
Stephan
Stephan 2018 年 7 月 17 日
I made it with 2018a, maybe it will not work. There is a pdf file also. You can copy it from this to a new live script
Honey Adams
Honey Adams 2018 年 7 月 17 日
yh.Thank you. I love the use of the syms .makes its understandable.The lsqcurvefit works and I are trying to use the lsqnonlin.I think there is a problem with it. It gives me an error. I stored the values of x in the workspace so that when i run, the lsqnonlin, i would it will m fetch it. this is what i get : Attempt to execute SCRIPT lsqnonlin as a function:
Honey Adams
Honey Adams 2018 年 7 月 17 日
But I am extremly happy the lsqcurve fit worked . I would like to make the lsqnonlin work too
Stephan
Stephan 2018 年 7 月 17 日
Did you named your file lsqnonlin ? This is problematic, when you do so. Matlab gets confused and can no more call the correct function from the corresponding toolbox. Name it lsqnonlin_test or something like this.
Honey Adams
Honey Adams 2018 年 7 月 17 日
i changed it from lsqnonlin still same results .
Stephan
Stephan 2018 年 7 月 17 日
編集済み: Stephan 2018 年 7 月 17 日
Attach the script please or provide the code. Adittionally check for some copies which are in the folder.
Use:
which lsqnonlin
There should be only one result.
Honey Adams
Honey Adams 2018 年 7 月 17 日
menu
Attempt to execute SCRIPT lsqnonlin as a function:
C:\Users\Hanan Yakubu\Documents\MATLAB\lsqnonlin.m
Error in menu (line 11) x2 = lsqnonlin(fun,x0,lb,ub,options2);
Honey Adams
Honey Adams 2018 年 7 月 17 日
please Find attached the script file.
Walter Roberson
Walter Roberson 2018 年 7 月 17 日
You have your own lsqnonlin.m that is interfering with using the Mathworks supplied one. You need to rename or delete C:\Users\Hanan Yakubu\Documents\MATLAB\lsqnonlin.m
Honey Adams
Honey Adams 2018 年 7 月 17 日
IT WORKEDDDDDDDDD :)))):)))
Stephan
Stephan 2018 年 7 月 17 日
Rename the file in the path you provided. This is the reason for the problem
Stephan
Stephan 2018 年 7 月 17 日
編集済み: Stephan 2018 年 7 月 17 日
Eureka - the second time in 1 hour... if we go on this speed, next year we go to oslo and get a price ;-)
Honey Adams
Honey Adams 2018 年 7 月 17 日
編集済み: Honey Adams 2018 年 7 月 17 日
lol.Thank you for your patience. I have a lot to practice to identify bugs.espec;y when I failed to pass a vector and was passing a scalar due to the omission of the pointwise operator.Lets rest here. Will defintely aknowledge you in any work I do.
Stephan
Stephan 2018 年 7 月 17 日
It was a kind of fun to help. I will now unfollow this question, since you seem to be happy. If you have further problems, come back with a new question.
Best regards
Stephan
Honey Adams
Honey Adams 2018 年 7 月 17 日
Yes, thank you.

サインインしてコメントする。

その他の回答 (1 件)

Stephan
Stephan 2018 年 7 月 13 日

1 投票

Hi,
in addition to the issues that Torsten noted in his comments, note the following errors:
  • you have X(i) instead of x(i) in your objective function, which will cause problems. I found 2 locations in your Bass function where this error appears.
  • your functions should end with an end - this will also cause errors, when executing the code
  • functions in a script have to be at the end of the script - this will also cause Errors, when executing the code
  • the problem regarding the input arguments is described here. The way you do it, will not pass the required arguments to the function calls of nonlinc and Bass, when fmincon calls the functions to evaluate the actual values of vector x in every iteration. In the result you will get the error not enough input arguments. To resolve this you need a kind of outer function, like described in the link above.
Here is a corrected version that considered all the issues that i could find:
data=[0;
0;
0;
37248;
58649;
85974;
132620;
200698;
266406;
325423;
383975;
443234;
498503;
558777;
612881;
659367;
710580;
749772;
794515;
847288;
910312;
963746;
1016473;
1060359;
1097173;
1135293;
1176867;
1212260;
1239208;
1267657];
v=100;
x = outer_function(v, data);
disp(x)
function j = outer_function(v, data)
fun=@Bass;
nl=@nonlinc;
lb=[20,0,0,0,0];
ub=[inf,1,1,1,inf];
x0=[ 0,0,0,0,0];
j=fmincon(fun,x0,[],[],[],[],lb,ub,nl);
function res= Bass(x)
%objective function
F= (- data + x(1)*x(2)*(1 - exp( x(2) + (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)));
res=sum(F.^2);
end
function [c, ceq]= nonlinc(x)
%constraint
c=[];
ceq=- data + ( x(1)*x(2)*(1 - exp( x(2) + (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)))/ ( 1+ (( x(3)*x(4))/ x(3)-x(4))*exp(-x(5)*v)));
end
end
If you run this script, it will work, but converges to an infeasible point, which may be because of what Torsten noted in his commentary:
Converged to an infeasible point.
fmincon stopped because the size of the current step is less than
the default value of the step size tolerance but constraints are not
satisfied to within the default value of the constraint tolerance.
<stopping criteria details>
28.4354 0.0000 0.5313 0.5313 0.4404
So what you have to do is check what Torsten told you.
Best regards
Stephan

3 件のコメント

Honey Adams
Honey Adams 2018 年 7 月 13 日
Point 1,2 3 noted it is well noted now. Will get back to you after going through the link in point four.Thank you for the insights.:)
Honey Adams
Honey Adams 2018 年 7 月 13 日
編集済み: Honey Adams 2018 年 7 月 13 日
Great. I understand now how the fmincon works even tough from Torsten, I have come to realize that using the fmincon isn't the right optimizer to use for my problem. Thank you for bearing with me and using part of ur precious time to go through the code and point out all the errors. I am grateful. I accepted Tortsen answers since I will be using the lsqcurvefit. But I gained a lot of insight from your short tutorial.
Stephan
Stephan 2018 年 7 月 13 日
With every question here i also learn - thats why im here. The way Torsten solved the problem is interesting for me too.

サインインしてコメントする。

カテゴリ

タグ

質問済み:

2018 年 7 月 12 日

コメント済み:

2018 年 7 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by