%% Problem Set H, Problem 2

clear all, close all, syms x y r theta real
% We will need both programs "polarRegion" and "verticalRegion"
% to solve this problem. They are found in Chapter 8.

%
%% (a)

polarRegion(@(theta) zeros(size(theta)), @(theta) 2*(1+sin(theta)), 0, 2*pi)
integrand = subs(x^2*y^2, [x, y], [r*cos(theta), r*sin(theta)])
int(int(r*integrand, r, 0, 2*(1+sin(theta))), theta, 0, 2*pi)

%
%% (b)

polarRegion(@(theta) zeros(size(theta)), @(theta) sin(theta).^2, 0, pi/2)
int(int(r*cos(r^2), r, 0, sin(theta)^2), theta, 0, pi/2)

%
% Since MATLAB couldn't evaluate this double integral symbolically, we can try computing the numerical value instead.

double(ans)

%
%% (c)
% Let's first draw the picture with verticalRegion to visualize what's going on.

verticalRegion(@(x) zeros(size(x)), @(x) (9-x.^2).^(1/2), 3/sqrt(2), 3)
axis([0, 3, 0, 3])

% So this is the piece of the disk of radius 3 around the origin in the first quadrant to the right of the line x = 3/sqrt(2). This line has polar equation r*cos(theta) = 3/sqrt(2) or r = (3/sqrt(2))*sec(theta). This line meets the circle r = 3 where cos(theta)=1/sqrt(2) or theta = pi/4.
%

polarRegion(@(theta) 3*sec(theta)/sqrt(2), @(theta) 3*ones(size(theta)), 0, pi/4)
int(int(r^3, r, 3*sec(theta)/sqrt(2), 3), theta, 0, pi/4)

%
%% (d)
% Let's first draw the picture with verticalRegion to visualize what's going on.

verticalRegion(@(x) (x-x.^2).^(1/2), @(x) (1-x.^2).^(1/2), 0, 1)

%
% So this is the region between two cicles in the first quadrant. The outer circle is r = 1 and the inner circle is

simplify(subs(y^2 + x^2 == x, [x, y], [r*cos(theta), r*sin(theta)]))

%
% So this is r = cos(theta)

polarRegion(@(theta) cos(theta), @(theta) ones(size(theta)), 0, pi/2)
int(int(r^4, r, cos(theta), 1), theta, 0, pi/2)