%====================================
% use MATLAB's symbolic capabilities
%====================================
clear all
clc
format long

% let user input a function as a character string
str_f    = input('please give function: ','s');

% convert the string to a symbolic variable...
sym_f    = sym(str_f);

% ...and use MATLAB's capability to perfom symbolic operations
sym_fder = diff(sym_f);

% convert symbolic results to function handles
%   char()      -> convert symbolix expression to string
%   vectorize() -> replace '/' and '^' with './' and '.^'
%   inline()    -> generate function handle
f    = inline(vectorize(char(sym_f)))
fder = inline(vectorize(char(sym_fder)))

% do whatever you want now, e.g. plot function and derivative on certain interval
x = linspace(-5,2.5,100);
plot(x,f(x),'b'), hold on
plot(x,fder(x),'r')

