%Doing calculations using Matlab's numerical methods
clear; clf;
%Define anonymous functions of one variable:
f = @(x) cos(x).^2 - 0.3;
g = @(x) sin(x)/2;
%Note the use of .^ instead of ^ 
%(this defines a function that accepts arrays as inputs)

%Define an input range and plot the functions over it:
x = [0:0.0001:4];
plot(x, f(x), x, g(x), x, x*0);

%Let's compute the area between the curves 
%within the window 0 < x < 4

%To find where f = g, we define a new function H = g - f
%and look for where H = 0:
H = @(x) g(x) - f(x);

%fzero requires a rough guess for a zero to look around it.
%From the graph, it looks like x = 0.7 and x = 2.5 are
%good guesses
x1 = fzero(H, 0.7)
x2 = fzero(H, 2.5)

%and now we integrate g-f = H
area = integral(H,x1,x2)

%Now, say we wanted to compute the volume 
%of the solid of revolution of this region around y = -1.

%We define the function which gives the area of the washer:
W = @(x) pi * ( (g(x)-(-1)).^2 - (f(x)-(-1)).^2 );

%And now we can integrate this function between the bounds:
volume = integral(W,x1,x2)