Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
derivatives, syms and fsolve

Subject: derivatives, syms and fsolve

From: Andrea Asoni

Date: 28 Jul, 2009 22:59:03

Message: 1 of 6

Hi,
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:
--------------------------------------------------
age =[12 13 14 15]; age=age';
sex =[1 1 0 0]; sex=sex';

syms a b;
lik= (a^2)*b.*age + (1/b).*sex

da=diff(lik,a); db=diff(lik,b)
D=[da; db]

x=[a b]
fun=@(x) D

init=[1 2]
fsolve(fun,init)
---------------------------------------------------

when I run this it gives me the error: "??? Conversion to double from sym is not possible."

I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval
Undefined function or variable 'a'."

I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.

Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?

Any help is very appreciated,
Andrea

Subject: derivatives, syms and fsolve

From: Alan Weiss

Date: 29 Jul, 2009 11:48:07

Message: 2 of 6

The problem you see is because fsolve requires standard floating-point
numbers as inputs, but D is a symbolic object.

To see an example of converting a problem from symbolic to float, and
then solving with Optimization Toolbox, see
http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1

FYI, to find the maximum likelihood solution, you don't need to take
derivatives. Just minimize -lik. I believe that in general this will be
more robust, because finding a zero of the gradient might find a
stationary or even local minimum point.

Alan Weiss
MATLAB mathematical toolbox documentation

Andrea Asoni wrote:
> Hi,
> 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:
> --------------------------------------------------
> age =[12 13 14 15]; age=age';
> sex =[1 1 0 0]; sex=sex';
>
> syms a b;
> lik= (a^2)*b.*age + (1/b).*sex
>
> da=diff(lik,a); db=diff(lik,b)
> D=[da; db]
>
> x=[a b]
> fun=@(x) D
>
> init=[1 2]
> fsolve(fun,init)
> ---------------------------------------------------
>
> when I run this it gives me the error: "??? Conversion to double from sym is not possible."
>
> I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval
> Undefined function or variable 'a'."
>
> I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.
>
> Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?
>
> Any help is very appreciated,
> Andrea

Subject: derivatives, syms and fsolve

From: Andrea Asoni

Date: 29 Jul, 2009 16:20:18

Message: 3 of 6

Alan, thanks.

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... :)

Alan Weiss <aweiss@mathworks.com> wrote in message <h4pcto$srj$1@fred.mathworks.com>...
> The problem you see is because fsolve requires standard floating-point
> numbers as inputs, but D is a symbolic object.
>
> To see an example of converting a problem from symbolic to float, and
> then solving with Optimization Toolbox, see
> http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
>
> FYI, to find the maximum likelihood solution, you don't need to take
> derivatives. Just minimize -lik. I believe that in general this will be
> more robust, because finding a zero of the gradient might find a
> stationary or even local minimum point.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> Andrea Asoni wrote:
> > Hi,
> > 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:
> > --------------------------------------------------
> > age =[12 13 14 15]; age=age';
> > sex =[1 1 0 0]; sex=sex';
> >
> > syms a b;
> > lik= (a^2)*b.*age + (1/b).*sex
> >
> > da=diff(lik,a); db=diff(lik,b)
> > D=[da; db]
> >
> > x=[a b]
> > fun=@(x) D
> >
> > init=[1 2]
> > fsolve(fun,init)
> > ---------------------------------------------------
> >
> > when I run this it gives me the error: "??? Conversion to double from sym is not possible."
> >
> > I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval
> > Undefined function or variable 'a'."
> >
> > I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.
> >
> > Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?
> >
> > Any help is very appreciated,
> > Andrea

Subject: derivatives, syms and fsolve

From: Shubham Gupta

Date: 2 Jul, 2013 18:50:11

Message: 4 of 6

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 Non-linear form. Could you please suggest something to use FSOLVE for this equation which has syms variable

Thank You
Shubham Gupta

Alan Weiss <aweiss@mathworks.com> wrote in message <h4pcto$srj$1@fred.mathworks.com>...
> The problem you see is because fsolve requires standard floating-point
> numbers as inputs, but D is a symbolic object.
>
> To see an example of converting a problem from symbolic to float, and
> then solving with Optimization Toolbox, see
> http://www.mathworks.com/access/helpdesk/help/toolbox/optim/ug/brn4nh7.html#brv_i_1
>
> FYI, to find the maximum likelihood solution, you don't need to take
> derivatives. Just minimize -lik. I believe that in general this will be
> more robust, because finding a zero of the gradient might find a
> stationary or even local minimum point.
>
> Alan Weiss
> MATLAB mathematical toolbox documentation
>
> Andrea Asoni wrote:
> > Hi,
> > 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:
> > --------------------------------------------------
> > age =[12 13 14 15]; age=age';
> > sex =[1 1 0 0]; sex=sex';
> >
> > syms a b;
> > lik= (a^2)*b.*age + (1/b).*sex
> >
> > da=diff(lik,a); db=diff(lik,b)
> > D=[da; db]
> >
> > x=[a b]
> > fun=@(x) D
> >
> > init=[1 2]
> > fsolve(fun,init)
> > ---------------------------------------------------
> >
> > when I run this it gives me the error: "??? Conversion to double from sym is not possible."
> >
> > I tried to add after "D=[da;db]" the line "D=double(D)" but it gives me another error "Error using ==> eval
> > Undefined function or variable 'a'."
> >
> > I tried the function "double" in different places (on x, on D, on fun) but it always gives me an error message.
> >
> > Anyone knows how to deal with this problem? How do I take derivatives and then solve the system?
> >
> > Any help is very appreciated,
> > Andrea

Subject: derivatives, syms and fsolve

From: Steven_Lord

Date: 2 Jul, 2013 20:09:01

Message: 5 of 6



"Shubham Gupta" <king.rona@gmail.com> wrote in message
news:kqv7d3$jit$1@newscl01ah.mathworks.com...
> 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 Non-linear form. Could you please suggest
> something to use FSOLVE for this equation which has syms variable

Either stick completely symbolic and use SOLVE instead of FSOLVE or use
matlabFunction to convert the symbolic expression into a function that you
can use with FSOLVE.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: derivatives, syms and fsolve

From: Alan Weiss

Date: 22 Jul, 2013 17:27:09

Message: 6 of 6

"Steven_Lord" <slord@mathworks.com> wrote in message <kqvc0t$2u3$1@newscl01ah.mathworks.com>...
>
>
> "Shubham Gupta" <king.rona@gmail.com> wrote in message
> news:kqv7d3$jit$1@newscl01ah.mathworks.com...
> > 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 Non-linear form. Could you please suggest
> > something to use FSOLVE for this equation which has syms variable
>
> Either stick completely symbolic and use SOLVE instead of FSOLVE or use
> matlabFunction to convert the symbolic expression into a function that you
> can use with FSOLVE.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

To complete the thread, the broken link is now
http://www.mathworks.com/help/optim/ug/symbolic-math-toolbox-calculates-gradients-and-hessians.html
There is also information in this example:
http://www.mathworks.com/help/optim/examples/using-symbolic-mathematics-with-optimization-toolbox-solvers.html

Alan Weiss
MATLAB mathematical toolbox documentation

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us