% This demo shows how you can use Matlab to do symbolic calculations.
% and make an easy animation of the position of a particle
% as a function of time.

% With a symbolic calculation Matlab can, for example, compute
% that the derivative of the function a*x^2 + b*x + c is 2*a*x +b.
%
% Let's consider a simple example

% First we declare our variables to be symbolic variables

syms x a b c

% Next we define a quadratic function of x, with parameters a,b,c:

Quadratic = a*x^2 + b*x + c

% To differentiate this function with respect to the variable x

DerivativeOfQuadratic = diff(Quadratic,x)

% To evaluate a symbolic function at choices of the parameters and variables
% we use subs

Expression1 = subs(Quadratic,x,3) % Substitutes in x=3

Expression2 = subs(Quadratic,a,4) % Substitutes in a=4

% To Substitute in a=4, b=5, and c=6:

Expression3 =  subs(Quadratic,{a,b,c},{4,5,6})

% To Substitute in a=4, b=5, c=6 and x=7 to get a numerical value:

Value = subs(Quadratic,{a,b,c,x},{4,5,6,7})

% TRY for yourself:
% Use diff and subs to compute the deivative of the quadratic
% Quadratic at a=3,b=4,c=6

% Next let's plot the symbolic function Quadratic when a=4, b=5, and c=6.
% For this we can use Expression3 which is a symbolic function of the 
% variable x.

% To plot Expression3 as a function of x for -2 <= x <= 2
% we use:

ezplot(Expression3,[-2,2])

% You can also define and plot curves in space symbolically.
% For example:

syms t

Xsym = cos(a*t)
Ysym = sin(a*t)
Zsym = b*t

avalue=2;
bvalue=3;

Xvalue = subs(Xsym,{a,b},{avalue,bvalue})
Yvalue = subs(Ysym,{a,b},{avalue,bvalue})
Zvalue = subs(Zsym,{a,b},{avalue,bvalue})

% The animate option in ezplot3 shows you a movie of a particle
% whose position at time t is given by the curve [Xvalue,Yvalue,Zvalue]

ezplot3(Xvalue,Yvalue,Zvalue,[0,2*pi],'animate')

% COOL!
