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:
Derivative

Subject: Derivative

From: Jeff

Date: 28 Nov, 2009 19:30:20

Message: 1 of 8

Hi. I could not find a function in Matlab to take the derivative of a polynomial, so I started to make my own. The input is a vector of polynomial coefficients. The function was pretty simple, actually, for regular old polynomials (you know, of the type x raised to declining powers). I'm wondering if anyone has suggestions for how I could do the same for polynomials with things like sin and cos in it (transcendetal functions?)? How do I represent them, and how do I derive them? Thanks.

Here is the little bit I have so far:

% polyDeriv(p): Computes derivative function of regular polynomials (ones
% with a variable raised to a power, but not ones with things like sin or
% cos in them).
% Inputs: p = coefficients of polynomial in order of declining exponents.
% Output: c = coefficients of derivative of p, will be one smaller than p.
% Example usage: p=[1 0 -2 -5]
% c=polyDeriv(p)
% Output: c==[3 0 -2]
function c=polyDeriv(p)
    n=size(p,2);
    for j=1:n-1
        d(j) = p(j)*(n-j);
    end
    c=d;
end

Subject: Derivative

From: us

Date: 28 Nov, 2009 19:45:20

Message: 2 of 8

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hertoc$ptf$1@fred.mathworks.com>...
> Hi. I could not find a function in Matlab to take the derivative of a polynomial, so I started to make my own. The input is a vector of polynomial coefficients. The function was pretty simple, actually, for regular old polynomials (you know, of the type x raised to declining powers). I'm wondering if anyone has suggestions for how I could do the same for polynomials with things like sin and cos in it (transcendetal functions?)? How do I represent them, and how do I derive them? Thanks.
>
> Here is the little bit I have so far:
>
> % polyDeriv(p): Computes derivative function of regular polynomials (ones
> % with a variable raised to a power, but not ones with things like sin or
> % cos in them).
> % Inputs: p = coefficients of polynomial in order of declining exponents.
> % Output: c = coefficients of derivative of p, will be one smaller than p.
> % Example usage: p=[1 0 -2 -5]
> % c=polyDeriv(p)
> % Output: c==[3 0 -2]
> function c=polyDeriv(p)
> n=size(p,2);
> for j=1:n-1
> d(j) = p(j)*(n-j);
> end
> c=d;
> end

a hint:
- try to get the symbolic math tbx

us

Subject: Derivative

From: Jeff

Date: 28 Nov, 2009 21:50:20

Message: 3 of 8

"us " <us@neurol.unizh.ch> wrote in message <herukg$jcj$1@fred.mathworks.com>...

>
> a hint:
> - try to get the symbolic math tbx
>
> us

Well, that takes all the fun out of it (but thanks!).

Subject: Derivative

From: us

Date: 28 Nov, 2009 21:58:05

Message: 4 of 8

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hes5ur$dtm$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <herukg$jcj$1@fred.mathworks.com>...
>
> >
> > a hint:
> > - try to get the symbolic math tbx
> >
> > us
>
> Well, that takes all the fun out of it (but thanks!).

well... who ever said life's fun - life's performance...

:-)
us

Subject: Derivative

From: Jeff

Date: 29 Nov, 2009 00:20:03

Message: 5 of 8

"us " <us@neurol.unizh.ch> wrote in message <hes6dd$bea$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hes5ur$dtm$1@fred.mathworks.com>...
> > "us " <us@neurol.unizh.ch> wrote in message <herukg$jcj$1@fred.mathworks.com>...
> >
> > >
> > > a hint:
> > > - try to get the symbolic math tbx
> > >
> > > us
> >
> > Well, that takes all the fun out of it (but thanks!).
>
> well... who ever said life's fun - life's performance...
>
> :-)
> us

OK, so I'm using the symbolic toolbox for a homework problem. But I can't quite understand how to substitute values for the variables when you have more than one variable. If I want to substitute for x, y, and z in a symbolic function, am I stuck with this clunky statement:

subs(subs(subs(F,x,0),y,0),z,0)

The problem (which is 5.21 from Scientific Computing by Heath, pg 252) is to solve the following system of equations using Newton's method. Newton's method is iterative. In other words, I have to supply an x-naught vector (of three variables, x, y, and z), and Newton's method will iteratively modify the x values until it finds the root of the system (got that?). Here's the problem:

10(x-y)=0
28x-y-xz=0
xy-(8/3)z=0

Here is my code so far:

format long g;
clear;
clc;

syms x y z;
sigma=sym(10)
r=sym(28)
b=sym(8/3)
xNaught=[0 0 0];

F(1)=sigma*y-sigma*x;
F(2)=r*x-y-x*z;
F(3)=x*y-b*z;
F=F.'

J(:,1)=diff(F,x);
J(:,2)=diff(F,y);
J(:,3)=diff(F,z);
J


How do I make the substitutions for xNaught into the symbolic functions of F and J? Am I really stuck with the clunky: subs(subs(subs(F,x,0),y,0),z,0)

Thanks,
-Jeff

Subject: Derivative

From: Steven Lord

Date: 30 Nov, 2009 03:02:26

Message: 6 of 8


"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:hesenj$6f6$1@fred.mathworks.com...
> "us " <us@neurol.unizh.ch> wrote in message
> <hes6dd$bea$1@fred.mathworks.com>...
>> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
>> <hes5ur$dtm$1@fred.mathworks.com>...
>> > "us " <us@neurol.unizh.ch> wrote in message
>> > <herukg$jcj$1@fred.mathworks.com>...

*snip*

> OK, so I'm using the symbolic toolbox for a homework problem. But I can't
> quite understand how to substitute values for the variables when you have
> more than one variable. If I want to substitute for x, y, and z in a
> symbolic function, am I stuck with this clunky statement:
>
> subs(subs(subs(F,x,0),y,0),z,0)

Nope, you can have OLD and NEW as vectors or cell arrays. Since the F
function I'm using as an example is pretty boring at [0, 0, 0] I'm going to
use a different point, but the technique will be the same with your F.


syms x y z
F = x+y.^2+z.^3;
subs(F, {x, y, z}, {1, 2, 3})


--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Derivative

From: Georgios

Date: 30 Nov, 2009 03:39:04

Message: 7 of 8

"Steven Lord" <slord@mathworks.com> wrote in message <hevchj$5mm$1@fred.mathworks.com>...
>
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> news:hesenj$6f6$1@fred.mathworks.com...
> > "us " <us@neurol.unizh.ch> wrote in message
> > <hes6dd$bea$1@fred.mathworks.com>...
> >> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> >> <hes5ur$dtm$1@fred.mathworks.com>...
> >> > "us " <us@neurol.unizh.ch> wrote in message
> >> > <herukg$jcj$1@fred.mathworks.com>...
>
> *snip*
>
> > OK, so I'm using the symbolic toolbox for a homework problem. But I can't
> > quite understand how to substitute values for the variables when you have
> > more than one variable. If I want to substitute for x, y, and z in a
> > symbolic function, am I stuck with this clunky statement:
> >
> > subs(subs(subs(F,x,0),y,0),z,0)
>
> Nope, you can have OLD and NEW as vectors or cell arrays. Since the F
> function I'm using as an example is pretty boring at [0, 0, 0] I'm going to
> use a different point, but the technique will be the same with your F.
>
>
> syms x y z
> F = x+y.^2+z.^3;
> subs(F, {x, y, z}, {1, 2, 3})
>
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>


>>help polyder

Does not require the use of the symbolic toolbox. This sounds like what the OP was looking for to begin with.

Regards,
Georgios

Subject: Derivative

From: Jeff

Date: 30 Nov, 2009 04:29:03

Message: 8 of 8

"Steven Lord" <slord@mathworks.com> wrote in message <hevchj$5mm$1@fred.mathworks.com>...
>
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> news:hesenj$6f6$1@fred.mathworks.com...
> > "us " <us@neurol.unizh.ch> wrote in message
> > <hes6dd$bea$1@fred.mathworks.com>...
> >> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> >> <hes5ur$dtm$1@fred.mathworks.com>...
> >> > "us " <us@neurol.unizh.ch> wrote in message
> >> > <herukg$jcj$1@fred.mathworks.com>...
>
> *snip*
>
> > OK, so I'm using the symbolic toolbox for a homework problem. But I can't
> > quite understand how to substitute values for the variables when you have
> > more than one variable. If I want to substitute for x, y, and z in a
> > symbolic function, am I stuck with this clunky statement:
> >
> > subs(subs(subs(F,x,0),y,0),z,0)
>
> Nope, you can have OLD and NEW as vectors or cell arrays. Since the F
> function I'm using as an example is pretty boring at [0, 0, 0] I'm going to
> use a different point, but the technique will be the same with your F.
>
>
> syms x y z
> F = x+y.^2+z.^3;
> subs(F, {x, y, z}, {1, 2, 3})
>
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Georgios, you are right. But I have almost finished my homework using the symbolic toolbox and will continue to use it this way (seeing as how I have other classes, as well).

Steve, that is just what I needed. Thanks.

Tags for this Thread

No tags are associated with 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