%Let's do Calculus using only very simple functions
%First, let's define a function to play with
f = @(x) x.^2;

%To do calculus, define an "infinitesimal" value dx:
dx = 0.0001;

%You know that f'(3) = 6. Let the computer figure it out:
(f(3  + dx) - f(3))/dx

%No magic about it! We can even define this as a function:
df = @(x) (f(x + dx) - f(x))/dx

%..and now we can plot it, say, on x = [-3..3]
x = [-3:dx:3];
plot(x, f(x), x, df(x), x, x*0);

%(you can see that f is decreasing when df is < 0)

%Now, let's do some integrals. 
%Again, you know that integral of f between -3 and 3 is 18
%How do we compute it? An integral is only a sum..
simple_sum = sum([1 2 3])
%..just like the one above. We just need to sum the right thing
area = sum(f(x).*dx)

%This does a Riemmann sum to compute an integral
%We get 18 and a little extra - that's the numerical error
%To get rid of the error, we could make dx smaller
%Try changing it in the beginning of this example!

%We could do numerical solving, too
%For instance, we figure out where f(x) == df(x) for x in [-3..3]
%First, define the function we want to find a zero of:
H = @(x) f(x) - df(x);

%and then just use Matlab's filtering syntax to find zeros.
%Here's how filtering works:
A = [1 2 3 4 5];
B = [1 0 1 0 1];
filtered_A = A(B == 0)
%This returns all entries in A where the corresponding entry in B is 0.
%[this is a simple for-loop if you know what that is]

%We can try do this for x and H(x):
probably_nothing = x(H(x) == 0)
%..but this doesn't work because numerics are not precise.
%Nothing is really EQUAL to zero, but we can look for what is close:
epsilon = 0.0002;
zeros = x(abs(H(x)) < epsilon)

%As you can see, we get 4 answers, but in reality there are 2
%-0.0001 and 0 really correspond to the solution x = 0
%2.0000 and 2.0001 really correspond to the solution x = 2

%See what happens if you change the epsilon above.

%And so, this roughly how all the computations are done.