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.
Contents
TOPIC
How do I differentiate a mathematical function
SUMMARY
Language : Matlab 2010a; Authors : Autar Kaw and Sri Harsha Garapati; Mfile available at Last Revised : January 23, 2012; Abstract: This program shows you how to differentiate a mathematical function
clc
clear all
clf
INTRODUCTION
disp('ABSTRACT') disp(' This program shows you how to differentiate a mathematical expression') disp(' ') disp('AUTHOR') disp(' Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com') disp(' ') disp('MFILE SOURCE') disp(' http://numericalmethods.eng.usf.edu/blog/calc_operations_diff_blog.m') disp(' ') disp('LAST REVISED') disp(' January 23, 2012') disp(' ')
ABSTRACT This program shows you how to differentiate a mathematical expression AUTHOR Autar Kaw and Sri Harsha Garapati of http://autarkaw.wordpress.com MFILE SOURCE http://numericalmethods.eng.usf.edu/blog/calc_operations_diff_blog.m LAST REVISED January 23, 2012
INPUTS
% Inputs % x is symbolic syms x % enter desired equation eqn1 =4*sin(4*x^2)+exp(x); % value of x at which the differential is to be found xnew =3.75;
DISPLAYING INPUTS
disp(' ') disp('INPUTS') % convert the eqn to a string eqn1d=char(eqn1); % printing the converted string to show the function fprintf(' y = %s\n',eqn1d) %printing the value of x at which the differential is to be found fprintf('The value at which the differential of the function to be found =%g\n',xnew)
INPUTS y = 4*sin(4*x^2) + exp(x) The value at which the differential of the function to be found =3.75
THE CODE
derivative of the function(2 represents 2nd derivative)
dydx =diff(eqn1,x,2); % converted to string dydx2=char(dydx); % subs command being used to calculate the value seqn =subs(dydx2,x,xnew);
DISPLAYING OUTPUTS AND PLOTS
disp(' ') disp('OUTPUTS') fprintf(' d2y/dx2 = %s\n',dydx2) fprintf(' d2y/dx2 @ %g = %g\n\n',xnew,seqn)
OUTPUTS d2y/dx2 = 32*cos(4*x^2) + exp(x) - 256*x^2*sin(4*x^2) d2y/dx2 @ 3.75 = 1132.39