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:
finding derivate of function with matlab

Subject: finding derivate of function with matlab

From: firecube@myrealbox.com (firecube)

Date: 28 Aug, 2002 08:31:05

Message: 1 of 5

I can easily find the derivatives of a function with Derive.
Dif(x^2,x) for examples gives 2*x using Derive. How can I do this type
of calculations with MatLab. Is there a 'Dif' like function? I am also
interested in finding derivatives of functions of two variables.

Thanks
F.C

Subject: finding derivate of function with matlab

From: Brett Shoelson

Date: 28 Aug, 2002 11:48:58

Message: 2 of 5


"firecube" <firecube@myrealbox.com> wrote in message
news:19f3723f.0208280731.15ad2eb3@posting.google.com...
> I can easily find the derivatives of a function with Derive.
> Dif(x^2,x) for examples gives 2*x using Derive. How can I do this type
> of calculations with MatLab. Is there a 'Dif' like function? I am also
> interested in finding derivatives of functions of two variables.
>
> Thanks
> F.C

If you have the Symbolic Toolbox:
x = sym('x'); y=sym('y');

diff(sin(x*y),x)
 ans =
 cos(x*y)*y

diff(sin(x*y),y)
ans =
cos(x*y)*x

(If you have the toolbox, also check out dsolve.)

Otherwise, for simple polynomial derivatives, you can use polyder. Or you
can approximate the derivative using diff. Or there are some other functions
like pdepe that might be useful, depending on the specifics of your problem.
Do a search for derivative in the helpdesk; there are lots of options, and
lots of toolbox-specific functions.
Cheers,
Brett

Subject: finding derivate of function with matlab

From: $toffel

Date: 28 Aug, 2002 18:07:02

Message: 3 of 5

help DIFF

Christophe


> I can easily find the derivatives of a function with Derive.
> Dif(x^2,x) for examples gives 2*x using Derive. How can I do this type
> of calculations with MatLab. Is there a 'Dif' like function? I am also
> interested in finding derivatives of functions of two variables.
>
> Thanks
> F.C

Subject: finding derivate of function with matlab

From: firecube@myrealbox.com (firecube)

Date: 28 Aug, 2002 15:57:38

Message: 4 of 5

> If you have the Symbolic Toolbox:
> x = sym('x'); y=sym('y');
>
> diff(sin(x*y),x)
> ans =
> cos(x*y)*y
>
> diff(sin(x*y),y)
> ans =
> cos(x*y)*x
 
Thanks for your answer. It really helped me. So I am going to you use
Symbolic Toolbox for that. But how can I have the "ans" (e.g in your
examples) as a function so I can calculate a function value later? For
example: I want to have f(x,y)=cos(x*y)*y after executing the diff
function, so I can calculate the: f(0,1) or calculating the derivative
of the new function after that.

Thanks again.

Subject: finding derivate of function with matlab

From: Brett Shoelson

Date: 29 Aug, 2002 09:54:45

Message: 5 of 5


"firecube" <firecube@myrealbox.com> wrote in message
news:19f3723f.0208281457.9e85435@posting.google.com...
> > If you have the Symbolic Toolbox:
> > x = sym('x'); y=sym('y');
> >
> > diff(sin(x*y),x)
> > ans =
> > cos(x*y)*y
> >
> > diff(sin(x*y),y)
> > ans =
> > cos(x*y)*x
>
> Thanks for your answer. It really helped me. So I am going to you use
> Symbolic Toolbox for that. But how can I have the "ans" (e.g in your
> examples) as a function so I can calculate a function value later? For
> example: I want to have f(x,y)=cos(x*y)*y after executing the diff
> function, so I can calculate the: f(0,1) or calculating the derivative
> of the new function after that.
>
> Thanks again.


I'm sure there are other ways, but:

%Declare your variables as symbolics (note that you may also put constraints
on them: real, positive, etc. See help sym)
>> x= sym('x'); y=sym('y');

%Define your function
>> f=y*sin(x*y);

%Convert symbolic function to string, and write as an inline function
>> fxy=inline(char(f)) fxy =
     Inline function:
     fxy(x,y) = y*sin(x*y)

%Now solve for specific values of x, y
>> fxy(pi,2/3)
ans =
    0.5774
OR
>> sym(fxy(pi,2/3))
 ans =
 sqrt(1/3)

Cheers,
Brett

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