**Tags**

Many students ask me how do I do this or that in MATLAB. So I thought why not have a small series of my next few blogs do that. In this blog I show you how to differentiate a function.

The MATLAB program link is here.

The HTML version of the MATLAB program is here.

___________________________________________

%% HOW DO I DO THAT IN MATLAB SERIES?

% In this series, I am answering questions that students have asked

% me about MATLAB. Most of the questions relate to a mathematical

% procedure.

%% TOPIC

% How do I differentiate a function?

%% SUMMARY

% Language : Matlab 2008a

% Authors : Autar Kaw

% Mfile available at

% http://numericalmethods.eng.usf.edu/blog/differentiation.m

% Last Revised : March 21, 2009

% Abstract: This program shows you how to differentiate a given function

%% INTRODUCTION

clc

clear all

disp(‘ABSTRACT’)

disp(‘ This program shows you how to differentiate’)

disp(‘ a given function and then find its value’)

disp(‘ at a given point’)

disp(‘ ‘)

disp(‘AUTHOR’)

disp(‘ Autar K Kaw of https://autarkaw.wordpress.com’)

disp(‘ ‘)

disp(‘MFILE SOURCE’)

disp(‘ http://numericalmethods.eng.usf.edu/blog/differentiation.m’)

disp(‘ ‘)

disp(‘LAST REVISED’)

disp(‘ March 21, 2009’)

disp(‘ ‘)

%% INPUTS

% Differentiate 7 exp(3*x) once and find the value of the

% first derivative at x=0.5

% Define x as a symbol

syms x

% Defining the function to be differentiated

y=7*exp(3*x);

% Defining the point where you want to find the derivative

xx=0.5;

%% DISPLAYING INPUTS

disp(‘INPUTS’)

func=[‘ The function is to be differentiated is ‘ char(y)];

disp(func)

fprintf(‘ Value of x where you want to find the derivative, x= %g’,xx)

disp(‘ ‘)

disp(‘ ‘)

%% THE CODE

% Finding the derivative using the diff command

% Argument 1 is the function to be differentiated

% Argument 2 is the variable with respect to which the

% function is to be differentiated – the independent variable

% Argument 3 is the order of derivative

dydx=diff(y,x,1);

% subs command substitues the value of x

dydx_val=subs(dydx,x,xx);

%% DISPLAYING OUTPUTS

disp(‘OUTPUTS’)

derivative_func=[‘ The derivative of function ‘ char(y) ‘ is ‘ char(dydx)];

disp(derivative_func)

fprintf(‘ Value of dydx at x=%g is =%g’,xx,dydx_val)

disp(‘ ‘)

______________________________________________________________________

This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://numericalmethods.eng.usf.edu, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://numericalmethods.eng.usf.edu/videos and http://www.youtube.com/numericalmethodsguy

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Rohedi

said:I enough understand if so many students ask how to get derivative a function using tool box of matlab, because matlab script for the differential commonly included or available in applications tool box. So, sometime students rather confuse when they would calculate even for drawing the graph of derivatif a function respect to it’s independent variable. I believe the scripts of finding derivative a function examplized at above are not only useful for all students at USF but also for my students at Physics Department of ITS Surabaya. Thanks for the above explanation.

Pingback: 2010 in review | The Numerical Methods Guy

lucy001119

said:Thank you for the posting. It is quite useful to get the derivative using this code. But I wonder is it suitable for matrix differentiation as well? If not, is there any other function that I can use to get matrix differentiation?

hina

said:hi.i want to know how i defrentiate and plot cos2x in matlab

LP

said:l1nkin_park_78@yahoo.com

email me your complete problem statement and code if you have written. I need it

Autar Kaw

said:The links are for the program are in the blog!

retha

said:what a great tutorial. it really saved my time. thanks!