how to calculate a derivative (2024)

900 views (last 30 days)

Show older comments

Nasir Qazi on 26 Feb 2012

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative

Edited: Walter Roberson on 15 Nov 2022

Accepted Answer: bym

can some one guide me how to calculate a derivative and integration in matlab . can you please give a little example.

1 Comment

Show -1 older commentsHide -1 older comments

Jan on 26 Feb 2012

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65117

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65117

Symbolically or numerically?

Sign in to comment.

Sign in to answer this question.

Accepted Answer

bym on 26 Feb 2012

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_38782

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_38782

Open in MATLAB Online

Symbolically

syms x real

f = 1/x

int(f,1,2) % integration

ans =

log(2)

diff(f) %differentiation

ans =

-1/x^2

[edit - amplification]

syms x a b real positive

f = 1/x

f =

1/x

int(f) % without limits

ans =

log(x)

int(f,a,b) % with limits

ans =

log(b) - log(a)

fn = matlabFunction(f) % convert symbolic to anonymous function

fn =

@(x)1./x

quadgk(fn,1,2) % integrate numerically

ans =

0.6931

log(2) % previous result from symbolic integration

ans =

0.6931

(fn(2+1e-6)-fn(2))/1e-6 %numerical derivative at fn(2)

ans =

-0.2500

subs(diff(f),2) %substitute 2 into symbolic result previously obtained

ans =

-0.2500

4 Comments

Show 2 older commentsHide 2 older comments

Nasir Qazi on 27 Feb 2012

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65130

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65130

inside int(f,1,2) what exactly this 1,2 stand for in here. 2ndly what do you means by symbolically of numerically .?, right and wht if we have limits in integration, how can we solve that expression .?

Walter Roberson on 27 Feb 2012

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65135

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65135

int(f,1,2) is integrate the symbolic expression f over the definite interval from 1 to 2.

Nasir Qazi on 27 Feb 2012

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65136

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65136

in case of limits in integration then ?

Jan on 27 Feb 2012

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65155

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_65155

Yes, Nasir, then the integral is calculated from 1 to 2. "Symbolically" mean calculations with symbols, usually characters. The result is a formula. "Numerically" means, that you calculate a numerical value, a number.

Sign in to comment.

More Answers (3)

Magdalena Glinska on 16 Nov 2020

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_545968

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_545968

Open in MATLAB Online

f = @(x) sin(x);

second_derivative_f = matlabFunction(diff(sym(f)));

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Hamza saeed khan on 24 Nov 2020

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_554893

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_554893

Open in MATLAB Online

syms x

f = x;

diff(f,x)

why this code give me error as;

>>Error in diff (line 1)

syms x

2 Comments

Show NoneHide None

Dheeresh agarwal on 22 Dec 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_1221620

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_1221620

diff(f,1)

Walter Roberson on 22 Dec 2020

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_1221860

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#comment_1221860

You probably do not have the symbolic toolbox installed or licensed.

Also, you accidentally named your script diff.m which is going to conflict with calling diff() to differentiate.

Sign in to comment.

Achimsettythanmay on 14 Nov 2022

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_1099318

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/30313-how-to-calculate-a-derivative#answer_1099318

Edited: Walter Roberson on 15 Nov 2022

Open in MATLAB Online

syms x real

f = 1/x

f=

how to calculate a derivative (13)

int(f,1,2) % integration

ans=how to calculate a derivative (14)

diff(f) %differentiation

ans=

how to calculate a derivative (15)

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationSymbolic Math ToolboxMuPADMuPAD Language FundamentalsSpecial Values

Find more on Special Values in Help Center and File Exchange

Tags

  • derivative and integration

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


how to calculate a derivative (16)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

how to calculate a derivative (2024)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 5967

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.