%% 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
% ;
% Last Revised : March 21, 2009;
% Abstract: This program shows you how to differentiate a given function.
clc
clear all
%% INTRODUCTION
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 http://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(' ')