Alan Weiss <aweiss@mathworks.com> wrote in message <ne6ah9$4sh$1@newscl01ah.mathworks.com>...
> On 4/7/2016 10:11 AM, Nana wrote:
> > Alan Weiss <aweiss@mathworks.com> wrote in message
> > <ne5m6f$k02$2@newscl01ah.mathworks.com>...
> >> On 4/7/2016 7:39 AM, Nana wrote:
> >> > I want to optimize p from testfun(). However I want to return
> >> additional
> >> > output variable (b) from the optimization.
> >> >
> >> > Function [p, b] = testfun()
> >> >
> >> > x = fmincon(@testfun, x0,options)
> >> >
> >> > where b is the additional variable passed to fsolve, I am looking to
> >> > achieve something like this,
> >> > [x,b] = fmincon(x0,options)
> >> >
> >> > How can I do that?
> >> > Thx
> >> > Nana
> >>
> >> The easiest way might be to use a nested function along these lines:
> >> http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html#brhkghv-10
> >>
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
> >
> > Hi Alan,
> >
> > Can you give an explicit example to do it using nested function? Because
> > on the link you gave me only return x and fval as outputs.
>
> Modify the listed example to return another output:
>
> function [x,fval,newnew] = runnested2(a,b,c,x0)
> newnew = 0; % allocation
> [x,fval] = fminunc(@nestedfun,x0);
> % Nested function that computes the objective function
> function y = nestedfun(x)
> y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) +...
> (-c + c*x(2)^2)*x(2)^2;
> newnew = y^2 + y;
> end
> end
>
> Try it out:
> a = 4; b = 2.1; c = 4;% Assign parameter values
> x0 = [0.5,0.5];
> [x,fval,newoutput] = runnested2(a,b,c,x0)
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
Worked perfectly! Thanks Alan.
> On 4/7/2016 10:11 AM, Nana wrote:
> > Alan Weiss <aweiss@mathworks.com> wrote in message
> > <ne5m6f$k02$2@newscl01ah.mathworks.com>...
> >> On 4/7/2016 7:39 AM, Nana wrote:
> >> > I want to optimize p from testfun(). However I want to return
> >> additional
> >> > output variable (b) from the optimization.
> >> >
> >> > Function [p, b] = testfun()
> >> >
> >> > x = fmincon(@testfun, x0,options)
> >> >
> >> > where b is the additional variable passed to fsolve, I am looking to
> >> > achieve something like this,
> >> > [x,b] = fmincon(x0,options)
> >> >
> >> > How can I do that?
> >> > Thx
> >> > Nana
> >>
> >> The easiest way might be to use a nested function along these lines:
> >> http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html#brhkghv-10
> >>
> >>
> >> Alan Weiss
> >> MATLAB mathematical toolbox documentation
> >
> > Hi Alan,
> >
> > Can you give an explicit example to do it using nested function? Because
> > on the link you gave me only return x and fval as outputs.
>
> Modify the listed example to return another output:
>
> function [x,fval,newnew] = runnested2(a,b,c,x0)
> newnew = 0; % allocation
> [x,fval] = fminunc(@nestedfun,x0);
> % Nested function that computes the objective function
> function y = nestedfun(x)
> y = (a - b*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) +...
> (-c + c*x(2)^2)*x(2)^2;
> newnew = y^2 + y;
> end
> end
>
> Try it out:
> a = 4; b = 2.1; c = 4;% Assign parameter values
> x0 = [0.5,0.5];
> [x,fval,newoutput] = runnested2(a,b,c,x0)
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
Worked perfectly! Thanks Alan.