clear all;close all;

% Area under a graph Simple Example and plot it

% declare functions
syms x
plot_range = [-3,3];

f1 = -x^2 + 6;

f2 = x^2 - 2*x + 2;

% Find max min values over curves for plots
range = plot_range(1):0.1:plot_range(2);
y = subs(f1,range); %sample values on curve f1

miny = min(y); %for plot y-axis
maxy = max(y);

y = subs(f2,range); %sample values on curve f2

if maxy < max(y) maxy = max(subs(y)); end;
if miny > min(y) miny = min(subs(y)); end;


% Find Points of Intersection of curves
roots_intersect = sort(double(solve (f1 - f2)));

% Definite Integral 
area_intersect = int(f1 - f2,roots_intersect(1),roots_intersect(2))
 

% Set up plot
range = roots_intersect(1):0.1:roots_intersect(2);
y = subs(f1,range); %sample values on curve f1


% Shade Area f1
area(range,y, 'FaceColor',[0, 0, 1], 'LineStyle', 'none')

hold on;


% 'rub out' area above the curve
y = subs(f2,range); %sample values on curve f2
area(range,y, 'FaceColor',[1, 1, 1], 'LineStyle', 'none')

% Plot funtions over plot range
ezplot(f1, plot_range);

hf2 = ezplot(f2, plot_range);
set(hf2,'Color','black');


% plot axes
plot(plot_range,[0 0], 'k'); % x-axis

plot([0 0],[miny maxy], 'k'); % y-axis

axis tight


shg
