Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Math Forum
»
Discussions
»
Software
»
comp.softsys.matlab
Notice: We are no longer accepting new posts, but the forums will continue to be readable.
Topic:
Passing input parameter in Objective function, which has integral function
Replies:
1
Last Post:
Dec 10, 2013 7:55 AM




Re: Passing input parameter in Objective function, which has integral function
Posted:
Dec 10, 2013 7:55 AM


On 12/10/2013 12:53 AM, Rubayet wrote: > Hi, > > I am trying to develop mat lab code for an objective function which > has series of integral function.I have created > mfile(compo1.m,compo2.m,compo3.m) for each integral function.then add > all of these integral function in order to develop my objective > function.But i am facing problem. I am getting error while i run the > problem. > I have expressed objective function as parameterfun.m As i have to > consider input parameter. Here funtion in parameterfun.m is > function y = parameterfun(x,a,b) > y=quad(@compo1,0,x(1))+quad(@compo2,0,x(1))+quad(@compo3,0,x(1))+2*a*x(2)+b; > > > and for each m file(compo1,compo2,compo3) > compo1 > function y=compo1(s,a,b) > y = a  b*s^2 + 2*s; > compo2 > function y=compo2(s,a,b) > y = 2*a  b*s^2 + 4*s; > compo3 > function y=compo3(s,a,b) > y = 6*b*s^2 + 4*s+a; > while i assign input as > a = 4; b = 2.1; % Assign parameter values > x0 = [0.5,0.5]; > f = @(x)parameterfun(x,a,b) > > [x,fval] = fminunc(f,x0) > then i am getting the following error: > ??? Undefined function or method 'compo1' for input arguments of type > 'double'. > > Error in ==> quad at 77 > y = f(x, varargin{:}); > > Error in ==> parameterfun at 2 > y=quad(@compo1,0,x(1))+quad(@compo2,0,x(1))+quad(@compo3,0,x(1))+2*a*x(2)+b; > > > Error in ==> @(x)parameterfun(x,a,b) > > > Error in ==> fminunc at 236 > f = feval(funfcn{3},x,varargin{:}); > > Caused by: > Failure in initial usersupplied objective function evaluation. > FMINUNC cannot continue. > > Can any body help me to figure out this problem? I desperately need to > solve this.. > Regards > Rubayet
I believe that you have several syntax errors.
The documentation for quad states "The function y = fun(x) should accept a vector argument x and return a vector result y, the integrand evaluated at each element of x." So you should change your functions as follows: function y=compo1(s,a,b) y = a  b*s.^2 + 2*s; % notice the . in .^2 By the way, I am assuming that your compo functions are not the actual functions you are eventually going to integrate, they are simplified versions for debugging.
Then when you pass compo to quad, you need to do it like this: @(s)compo(s,a,b) So the call would be quad(@(s)compo(s,a,b),c,d)
Test your objective function before trying to minimize it in fminunc; I mean, compute parameterfun(x0,a,b) where x0 is your initial point.
Good luck,
Alan Weiss MATLAB mathematical toolbox documentation



