How to use fmincon on an symbolic integral function
3 views (last 30 days)
Show older comments
Update: just rewrote my question... should be clear now...
I was trying to use fmincon to find the optimizer x=[x(1),x(2)] of a integral function where x(1) and x(2) are in the expression of the lower and higher limits of the integral, but I failed a lot of attempts which has driven me crazy!!!! Could someone help? A simple example to illustrate my problem, I want to find values for x and y to minimize function: integral(z^2,x,y)=(y^3)/3-(x^3)/3, such that 0<=x<=1,2<=y<=5.
Below are the code I wrote:
fun=@(x,y) int(@(z) z^2,x,y);
a0=[0,0];
sol=fmincon(fun,a0,[],[],[],[],[0,2],[1,5]);
Of course the code does not work and feedbacks are:
Error using symengine>makeFhandle/@(x,y)x.^3.*(-1.0./3.0)+y.^3.*(1.0./3.0) Not enough input arguments.
Error in fmincon (line 564)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in test (line 12)
sol=fmincon(ht,a0,[-1,-1],0)
Caused by:
Failure in initial user-supplied objective function evaluation.
FMINCON cannot continue.
0 Comments
Answers (3)
Walter Roberson
on 1 Nov 2016
fun = @(xy) integral(@(z) z^2, xy(1), xy(2));
a0 = [0,0];
sol = fmincon(fun,a0,[-1,-1],0);
Alan Weiss
on 1 Nov 2016
I do not understand your example, but perhaps a completely new start will help you. Suppose that you have a parameterized function of x and y, say with parameter a:
f = sin(a*x.^3)./3 - cos(a*y.^3)./3
Suppose that you want to minimize the integral of this function over 0 <= x <= 1, 1 <= y <= 2, and for -5 <= a <= 1.
fun = @(a)integral2(@(x,y)sin(a*x.^3)./3 - cos(a*y.^3)./3,0,1,1,2);
[asolution,intval] = fminbnd(fun,-5,1)
asolution =
-3.3592
intval =
-0.1405
You see, I defined the objective function of a as an integral, and found the minimum value of the integral over values of a in the interval -5 <= a <= 1.
Alan Weiss
MATLAB mathematical toolbox documentation
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!