http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205
MATLAB Central Newsreader  derivatives, syms and fsolve
Feed for thread: derivatives, syms and fsolve
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 28 Jul 2009 22:59:03 +0000
derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#668945
Andrea Asoni
Hi,<br>
I would like to define a function, take derivatives of this function and then solve the system of equations formed by putting these derivatives equal to zero. A simplified version of my problem is:<br>
<br>
age =[12 13 14 15]; age=age';<br>
sex =[1 1 0 0]; sex=sex';<br>
<br>
syms a b;<br>
lik= (a^2)*b.*age + (1/b).*sex<br>
<br>
da=diff(lik,a); db=diff(lik,b)<br>
D=[da; db]<br>
<br>
x=[a b]<br>
fun=@(x) D<br>
<br>
init=[1 2]<br>
fsolve(fun,init)<br>
<br>
<br>
when I run this it gives me the error: "??? Conversion to double from sym is not possible."<br>
<br>
I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval<br>
Undefined function or variable 'a'."<br>
<br>
I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.<br>
<br>
Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?<br>
<br>
Any help is very appreciated,<br>
Andrea

Wed, 29 Jul 2009 11:48:07 +0000
Re: derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#669075
Alan Weiss
The problem you see is because fsolve requires standard floatingpoint <br>
numbers as inputs, but D is a symbolic object.<br>
<br>
To see an example of converting a problem from symbolic to float, and <br>
then solving with Optimization Toolbox, see<br>
<a href="http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1">http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1</a><br>
<br>
FYI, to find the maximum likelihood solution, you don't need to take <br>
derivatives. Just minimize lik. I believe that in general this will be <br>
more robust, because finding a zero of the gradient might find a <br>
stationary or even local minimum point.<br>
<br>
Alan Weiss<br>
MATLAB mathematical toolbox documentation<br>
<br>
Andrea Asoni wrote:<br>
> Hi,<br>
> I would like to define a function, take derivatives of this function and then solve the system of equations formed by putting these derivatives equal to zero. A simplified version of my problem is:<br>
> <br>
> age =[12 13 14 15]; age=age';<br>
> sex =[1 1 0 0]; sex=sex';<br>
> <br>
> syms a b;<br>
> lik= (a^2)*b.*age + (1/b).*sex<br>
> <br>
> da=diff(lik,a); db=diff(lik,b)<br>
> D=[da; db]<br>
> <br>
> x=[a b]<br>
> fun=@(x) D<br>
> <br>
> init=[1 2]<br>
> fsolve(fun,init)<br>
> <br>
> <br>
> when I run this it gives me the error: "??? Conversion to double from sym is not possible."<br>
> <br>
> I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval<br>
> Undefined function or variable 'a'."<br>
> <br>
> I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.<br>
> <br>
> Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?<br>
> <br>
> Any help is very appreciated,<br>
> Andrea

Wed, 29 Jul 2009 16:20:18 +0000
Re: derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#669166
Andrea Asoni
Alan, thanks.<br>
<br>
I am also trying to minimize the lik (I am using fmicon) but it gives me all sorts of problems (seems really flat or really discontinuous... which shouldn't be, at least discontinuous. haven't figure out yet if i made a mistake in code or it is just my problem... cannot use fminsearch because i have many parameters) so i was trying to do it the old way... :)<br>
<br>
Alan Weiss <aweiss@mathworks.com> wrote in message <h4pcto$srj$1@fred.mathworks.com>...<br>
> The problem you see is because fsolve requires standard floatingpoint <br>
> numbers as inputs, but D is a symbolic object.<br>
> <br>
> To see an example of converting a problem from symbolic to float, and <br>
> then solving with Optimization Toolbox, see<br>
> <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1">http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1</a><br>
> <br>
> FYI, to find the maximum likelihood solution, you don't need to take <br>
> derivatives. Just minimize lik. I believe that in general this will be <br>
> more robust, because finding a zero of the gradient might find a <br>
> stationary or even local minimum point.<br>
> <br>
> Alan Weiss<br>
> MATLAB mathematical toolbox documentation<br>
> <br>
> Andrea Asoni wrote:<br>
> > Hi,<br>
> > I would like to define a function, take derivatives of this function and then solve the system of equations formed by putting these derivatives equal to zero. A simplified version of my problem is:<br>
> > <br>
> > age =[12 13 14 15]; age=age';<br>
> > sex =[1 1 0 0]; sex=sex';<br>
> > <br>
> > syms a b;<br>
> > lik= (a^2)*b.*age + (1/b).*sex<br>
> > <br>
> > da=diff(lik,a); db=diff(lik,b)<br>
> > D=[da; db]<br>
> > <br>
> > x=[a b]<br>
> > fun=@(x) D<br>
> > <br>
> > init=[1 2]<br>
> > fsolve(fun,init)<br>
> > <br>
> > <br>
> > when I run this it gives me the error: "??? Conversion to double from sym is not possible."<br>
> > <br>
> > I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval<br>
> > Undefined function or variable 'a'."<br>
> > <br>
> > I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.<br>
> > <br>
> > Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?<br>
> > <br>
> > Any help is very appreciated,<br>
> > Andrea

Tue, 02 Jul 2013 18:50:11 +0000
Re: derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#907529
Shubham Gupta
Hi Alan, The link you have replied is not working. could you please give a fresh link for the problem. I have a matrix with 15 symbolic variable and also the equation is in Nonlinear form. Could you please suggest something to use FSOLVE for this equation which has syms variable<br>
<br>
Thank You<br>
Shubham Gupta<br>
<br>
Alan Weiss <aweiss@mathworks.com> wrote in message <h4pcto$srj$1@fred.mathworks.com>...<br>
> The problem you see is because fsolve requires standard floatingpoint <br>
> numbers as inputs, but D is a symbolic object.<br>
> <br>
> To see an example of converting a problem from symbolic to float, and <br>
> then solving with Optimization Toolbox, see<br>
> <a href="http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1">http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1</a><br>
> <br>
> FYI, to find the maximum likelihood solution, you don't need to take <br>
> derivatives. Just minimize lik. I believe that in general this will be <br>
> more robust, because finding a zero of the gradient might find a <br>
> stationary or even local minimum point.<br>
> <br>
> Alan Weiss<br>
> MATLAB mathematical toolbox documentation<br>
> <br>
> Andrea Asoni wrote:<br>
> > Hi,<br>
> > I would like to define a function, take derivatives of this function and then solve the system of equations formed by putting these derivatives equal to zero. A simplified version of my problem is:<br>
> > <br>
> > age =[12 13 14 15]; age=age';<br>
> > sex =[1 1 0 0]; sex=sex';<br>
> > <br>
> > syms a b;<br>
> > lik= (a^2)*b.*age + (1/b).*sex<br>
> > <br>
> > da=diff(lik,a); db=diff(lik,b)<br>
> > D=[da; db]<br>
> > <br>
> > x=[a b]<br>
> > fun=@(x) D<br>
> > <br>
> > init=[1 2]<br>
> > fsolve(fun,init)<br>
> > <br>
> > <br>
> > when I run this it gives me the error: "??? Conversion to double from sym is not possible."<br>
> > <br>
> > I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval<br>
> > Undefined function or variable 'a'."<br>
> > <br>
> > I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.<br>
> > <br>
> > Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?<br>
> > <br>
> > Any help is very appreciated,<br>
> > Andrea

Tue, 02 Jul 2013 20:09:01 +0000
Re: derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#907530
Steven_Lord
<br>
<br>
"Shubham Gupta" <king.rona@gmail.com> wrote in message <br>
news:kqv7d3$jit$1@newscl01ah.mathworks.com...<br>
> Hi Alan, The link you have replied is not working. could you please give a <br>
> fresh link for the problem. I have a matrix with 15 symbolic variable and <br>
> also the equation is in Nonlinear form. Could you please suggest <br>
> something to use FSOLVE for this equation which has syms variable<br>
<br>
Either stick completely symbolic and use SOLVE instead of FSOLVE or use <br>
matlabFunction to convert the symbolic expression into a function that you <br>
can use with FSOLVE.<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Mon, 22 Jul 2013 17:27:09 +0000
Re: derivatives, syms and fsolve
http://www.mathworks.com/matlabcentral/newsreader/view_thread/257205#908559
Alan Weiss
"Steven_Lord" <slord@mathworks.com> wrote in message <kqvc0t$2u3$1@newscl01ah.mathworks.com>...<br>
> <br>
> <br>
> "Shubham Gupta" <king.rona@gmail.com> wrote in message <br>
> news:kqv7d3$jit$1@newscl01ah.mathworks.com...<br>
> > Hi Alan, The link you have replied is not working. could you please give a <br>
> > fresh link for the problem. I have a matrix with 15 symbolic variable and <br>
> > also the equation is in Nonlinear form. Could you please suggest <br>
> > something to use FSOLVE for this equation which has syms variable<br>
> <br>
> Either stick completely symbolic and use SOLVE instead of FSOLVE or use <br>
> matlabFunction to convert the symbolic expression into a function that you <br>
> can use with FSOLVE.<br>
> <br>
>  <br>
> Steve Lord<br>
> slord@mathworks.com<br>
> To contact Technical Support use the Contact Us link on <br>
> <a href="http://www.mathworks.com">http://www.mathworks.com</a> <br>
<br>
To complete the thread, the broken link is now<br>
<a href="http://www.mathworks.com/help/optim/ug/symbolicmathtoolboxcalculatesgradientsandhessians.html">http://www.mathworks.com/help/optim/ug/symbolicmathtoolboxcalculatesgradientsandhessians.html</a><br>
There is also information in this example:<br>
<a href="http://www.mathworks.com/help/optim/examples/usingsymbolicmathematicswithoptimizationtoolboxsolvers.html">http://www.mathworks.com/help/optim/examples/usingsymbolicmathematicswithoptimizationtoolboxsolvers.html</a><br>
<br>
Alan Weiss<br>
MATLAB mathematical toolbox documentation