## Vector Calculus Green’s Theorem Math Examples

###### Vector Calculus Green’s Theorem Math Examples:

These are from the book Calculus Early Transcendentals 10th Edition. Refer to 15.4 exercise maths.

#### Green’s Theorm:

Let, $R$ be a simple connected plane region where boundary is a simple, closed, piecewise smooth curve oriented counter clockwise. If $f(x,y) \text{and} g(x,y)$ are continuous and have continuous first partial derivatives on some open set containing R, then

$\int f(x,y) \ dx + g(x,y) \ dy = \iint\limits_R (\frac{\delta g}{\delta x} - \frac{\delta f}{\delta y}) \ dA$

#### Solution:

###### Using Green’s Theorm,
$\oint_c 4xy \ dx + 2xy \ dy = \iint\limits_R \frac{\delta }{\delta x} 2xy - \frac{\delta }{\delta y} 4xy \ dA$

$= \iint\limits_R 2y - 4x \ dA$

$= \int_{-2}^4\int_1^2 2y - 4x \ dy \ dx$

$= \int_{-2}^4 [y^2 - 4xy]_1^2 \ dx$

$= \int_{-2}^4 4 - 8x - 1 + 4x \ dx$

$= \int_{-2}^4 3 - 4x \ dx$

$= [3x - 2x^2]_{-2}^4$

$= 12 - 32 + 6 + 8$

$= -6$


#### Solution:

Since this is a full circle the interval of $\int_{\theta} \text{is from} \ 0 \ to \ 2\pi$
Also, $x^2 + y^2 = 4 = 2^2$. So the interval of $\int_{r} \text{is from} \ 0 \ to \ 2$

###### Using Green’s Theorm,
$\oint_c (x^2 - 3y) + 3x \ dy = \iint\limits_R \frac{\delta }{\delta x} 3x - \frac{\delta }{\delta y} (x^2 - 3y) \ dA$

$= \iint\limits_R 3 + 3 \ dA$

$= \int_0^{2\pi} \int_0^2 6 r \ dr \ d\theta$

$= 6 \int_0^{2\pi} [\frac{r^2}{2}]_0^2 \ dr \ d\theta$

$= 12 \int_0^{2\pi} \ d\theta$

$= 12 [\theta]_0^{2\pi}$

$= 12 \times 2\pi$

$= 24\pi$


#### Solution:

The upper bounds for y can be found using points (2,0) and (0,4).

Here,
$x_1 = 2, y_1 = 0, x_2 = 0, y_2 = 4,$

$\frac{y - y_1}{x - x_1} = \frac{y_1 - y_2}{x_1 - x_2}$

$\frac{y - 0}{x - 2} = \frac{0 - 4}{2 - 0}$

$y = -2x + 4$

###### Using Green’s Theorm,
$\oint_c ln(1 + y) \ dx - \frac{xy}{1 + y} \ dy = \iint\limits_R \frac{\delta }{\delta x} (-\frac{xy}{1 + y}) - \frac{\delta }{\delta y} ln(1 + y) \ dA$

$= \iint\limits_R (-\frac{y}{1 + y} - \frac{1}{1 + y}) \ dA$

$= \int_0^2 \int_0^{-2x + 4} (-\frac{y}{1 + y} - \frac{1}{1 + y}) \ dy \ dx$

$= \int_0^2 \int_0^{-2x + 4} -\frac{1 + y}{1 + y} \ dy \ dx$

$= -1 \int_0^2 \int_0^{-2x + 4} \ dy \ dx$

$= -1 \int_0^2 [y]_0^{-2x + 4} \ dx$

$= -1 \int_0^2 -2x + 4 \ dx$

$= [x^2 - 4x]_0^2$

$= 4 - 8 - 0 + 0$

$= -4$


## Vector Calculus: Finding out divergence and curl of vector field

Vector Calculus – Finding out divergence and curl of vector field:

In case of fluid flow,

Divergence:  Relates to the Way in which fluid flows away
or, toward from a point.
It has Scaler values.

Curl:        Rotational properties of a fluid at a point.
It has vector values.


Vector field in 3 space with xyz co-ordinate system,
$\overrightarrow{F}(x,y,z) = < f(x,y,z), g(x,y,z), h(x,y,z) >$

Then divergence of function $\overrightarrow{F} (x, y, z)$ is,
$div \vec{F} = \nabla . \vec{F} = < \frac{\delta}{\delta x}, \frac{\delta}{\delta y}, \frac{\delta}{\delta z} > . < f, g, h >$
$div \vec{F} = \frac{\delta f}{\delta x} + \frac{\delta g}{\delta y} + \frac{\delta h}{\delta z}$

The curl of the function $\vec{F} (x, y, z)$ is,
$curl \vec{F} = \nabla \times \vec{F}$

$= \begin{vmatrix} \hat{i} & \hat{j} & \hat{k} \\ \frac{\delta}{\delta x} & \frac{\delta}{\delta y} & \frac{\delta}{\delta z} \\ f & g & h \end{vmatrix}$

$curl \vec{F} = < \frac{\delta h}{\delta y} - \frac{\delta g}{\delta z}, \frac{\delta f}{\delta z} - \frac{\delta h}{\delta x}, \frac{\delta g}{\delta x} - \frac{\delta f}{\delta y} >$

#### Example:

$curl \vec{F} = < x^2y, 2y^3z, 3z >$

Here,
$f(x,y,z) = x^2y \\ f(x,y,z) = 2y^3z \\ f(x,y,z) = 3z$

We know divergence function,
$div \vec{F} = \frac{\delta f}{\delta x} + \frac{\delta g}{\delta y} + \frac{\delta h}{\delta z} \ \ \ \ \ (1)$

Now,
$\frac{\delta f}{\delta x} = \frac{\delta}{\delta x} x^2y = 2xy \\ \\ \frac{\delta g}{\delta y} = \frac{\delta}{\delta y} 2y^3z = 6y^2z \\ \\ \frac{\delta h}{\delta z} = \frac{\delta}{\delta z} 3z = 3$

So, from $eqn(1)$ we get,
$div \vec{F} = 2xy + 6y^2z + 3$

Also, The curl of the function,
$curl \vec{F} = \nabla \times \vec{F}$
$= \begin{vmatrix} \hat{k} & \hat{k} & \hat{k} \\ \frac{\delta}{\delta x} & \frac{\delta}{\delta y} & \frac{\delta}{\delta z} \\ f & g & h \end{vmatrix}$
$curl \vec{F} = < \frac{\delta h}{\delta y} - \frac{\delta g}{\delta z}, \frac{\delta f}{\delta z} - \frac{\delta h}{\delta x}, \frac{\delta g}{\delta x} - \frac{\delta f}{\delta y} > \ \ \ \ \ (2)$

calculating partial derivatives,
$\frac{\delta h}{\delta y} = \frac{\delta}{\delta y} 3z = 0 \\ \\ \frac{\delta g}{\delta z} = \frac{\delta}{\delta z} 2y^3z = 2y^3 \\ \\ \frac{\delta f}{\delta z} = \frac{\delta}{\delta z} x^2y = 0 \\ \\ \frac{\delta h}{\delta x} = \frac{\delta}{\delta x} 3z = 0 \\ \\ \frac{\delta g}{\delta x} = \frac{\delta}{\delta x} 2y^3z = 0 \\ \\ \frac{\delta f}{\delta y} = \frac{\delta}{\delta y} x^2y = x^2$

So, from $eqn(2)$ we write,
$curl \vec{F} = < 0 - 2y^3, 0 - 0, 0 - x^2 > \\ \\ curl \vec{F} = < -2y^3, 0, - x^2 > \\ or, \\ curl \vec{F} = -2y^3 \hat{i} - x^2 \hat{k};$

## C++ Code for finding Parametric Equation and Vector form for Line L through two points p1 and p2

#### Description:

Given two points $P_1 (x_1, y_1, z_1)$ and $P_2 (x_2, y_2, z_2)$ we are to find the parametric equation and vector form of line through these points.

The parallel vector to the line is,

$\overrightarrow{P_1 P_2} = < x_2 - x_1, y_2 - y_1, z_2 - z_1 >$

So the parallel vector of $P_1 (2, 4, -1)$ and $P_2 (5, 0, 7)$ is,

$\overrightarrow{P_1 P_2} = < 5 - 2, 0 - 4, 7 - (-1) > = < 3, -4, 8 >$

Note that $\vec{v}$ is the parallel vector. We know,

$\vec{r} = \vec{r_0} + t \vec{v}$

where,
$\vec{r} = < x, y, z >$
$\vec{r_0} = < x_0, y_0, z_0 >$
$\vec{v} = < a, b, c >$

So the vector equation form is,

$\vec{r} = \vec{r_0} + t \vec{v}$
$< x, y, z > = < x_0, y_0, z_0 > + t < a, b, c >$

Also the parametric form is,

$x = x_0 + at$
$y = y_0 + bt$
$z = z_0 + ct$

Now we can use either vector from origin to $P_1$ or origin to $P_2$. Here the vector from origin to  origin to $P_1$ which is (0, 0, 0) to (2, 4, -1) is,

$\vec{r_0} = < 2 - 0, 4 - 0, -1 - 0 > = < 2, 4, -1 >$

So the vector equation of line through $P_1$ and $P_2$ is,

$< x, y, z > = < 2, 4, -1 > + t < 3, -4, 8 >$

calculating further,
$< x, y, z > = < 2, 4, -1 > + < 3t, -4t, 8t >$
$< x, y, z > = < 2 + 3t, 4 - 4t, -1 + 8t >$

Finally,

$L: x = 2 + 3t, y = 4 - 4t, z = -1 + 8t$

Similarly the equation of the line can found out using origin to $P_2$.

### Code:

/**
* @author  Quickgrid ( Asif Ahmed )
* Problem: Find parametric equations for the line through p1 and p2.
*          Also for the segment joining these points.
*/

#include<stdio.h>

struct myVector{
int x, y, z;
} v1, v2;

void input2dPoints(){
printf("Enter p1:");
scanf("%d %d", &v1.x, &v1.y);
printf("Enter p2:");
scanf("%d %d", &v2.x, &v2.y);
}

void input3dPoints(){
printf("Enter p1:");
scanf("%d %d %d", &v1.x, &v1.y, &v1.z);
printf("Enter p2:");
scanf("%d %d %d", &v2.x, &v2.y, &v2.z);
}

struct myVector calculateParallelVector(int choice){
if(choice == 1)
return {v2.x - v1.x, v2.y - v1.y};
else{
return {v2.x - v1.x, v2.y - v1.y, v2.z - v1.z};
}
};

void printParametricAndVectorForm(int point, struct myVector mv, struct myVector parallelVector){
printf("Taking point p%d(%d, %d) on the line L we get parametric equation:\n", point, mv.x, mv.y);
printf("L: x = %d + %dt, y = %d + %dt\n",  mv.x, parallelVector.x, mv.y, parallelVector.y);
printf("\n");
printf("Vector form:\n");
printf("<x, y> = <%d, %d> + t <%d, %d>\n",  mv.x, mv.y, parallelVector.x, parallelVector.y);
printf("\n\n");
}

//@Override
void printParametricAndVectorForm(int point, struct myVector mv, struct myVector parallelVector, int dimension){
printf("Taking point p%d(%d, %d, %d) on the line L we get parametric equation:\n", point, mv.x, mv.y, mv.z);
printf("L: x = %d + %dt, y = %d + %dt, z = %d + %dt\n",  mv.x, parallelVector.x, mv.y, parallelVector.y, mv.z, parallelVector.z);
printf("\n");
printf("Vector form:\n");
printf("<x, y, z> = <%d, %d, %d> + t <%d, %d, %d>\n",  mv.x, mv.y, mv.z, parallelVector.x, parallelVector.y, parallelVector.z);
printf("\n\n");
}

int main(){
int choice;
struct myVector parallelVector;

printf("1. Two Dimensional.\n");
printf("2. Three Dimensional.\n");
printf("\nChoice: ");
scanf("%d", &choice);
printf("\n");

switch(choice){
case 1:
input2dPoints();

printf("\n");
parallelVector = calculateParallelVector(choice);
printf("Parallel vector -> p1p2 to the line L is: <%d, %d>\n", parallelVector.x, parallelVector.y);
printf("\n\n");

printParametricAndVectorForm(1, v1, parallelVector);
printParametricAndVectorForm(2, v2, parallelVector);

break;

case 2:
input3dPoints();

printf("\n");
parallelVector = calculateParallelVector(choice);
printf("Parallel vector -> p1p2 to the line L is: <%d, %d, %d>\n", parallelVector.x, parallelVector.y, parallelVector.z);
printf("\n\n");

printParametricAndVectorForm(1, v1, parallelVector, 3);
printParametricAndVectorForm(2, v2, parallelVector, 3);

break;

default:
printf("Wrong input.\n");
}

return 0;
}


## UVA Problem 10041 – Vito’s Family Solution

UVA Problem 10041 – Vito’s Family Solution:

Solving Technique:

This is a sorting problem. It requires us minimize the distance from Vito’s to every other house. That distance must be median because median value minimizes the distance from every side. So the median is the position of Vito’s house.

The problem requires us to find, “minimal sum of distances from the optimal Vito’s house to each one of his relatives”. So this means we calculate distance from Vito’s house ( Median ) to every other house and sum them. Meaning subtract position of every house from Vito’s house and sum their distance.

I have provided Three commented codes below. First one uses STL Sort and doesn’t calculate absolute value for distance. Second one shows the use of Insertion Sort and calculates absolute value to find their distance. The last one uses vector to store data and STL algorithm to find nth element in an array. Here ( for this code ) the calculated nth element is the median.

Important:  Be sure to add or print a new line after each output unless otherwise specified. The outputs should match exactly because sometimes even a space character causes the answer to be marked as wrong answer.

Input:

2
2 2 4
3 2 4 6

Output:

2
4

### Code ( Without Calculating Absolute Value ):

/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<cstdio>
#include<algorithm>

static int A[512];

int main(){
register int n, m, i, j;
scanf("%d", &n);

while (n--){
scanf("%d", &m);

if (m == 1){
/**
* If only 1 input then no need to process
* For 1 input the distance is always Zero
*/
scanf("%*d");
printf("0\n");
}

else{
int dist = 0, median, key;

for (i = 0; i < m; ++i)
scanf("%d", &A[i]);

/**
* Sort the input using stl sort
* Instead of using sorting algorithm, Try to implement Selection Algorithm to find Median in O(n) time
*/
std::sort(A, A + m);

/**
* Find median dividing by two or shifting bit to left once
* here, key is the value of median
*/
median = m >> 1;
key = A[median];

/**
* here, dist is the summation of distance of every house
* Any value less than Median can subtracted from median will result in a positive value
*/
for (i = 0; i< median; ++i)
dist += key - A[i];

/**
* No need calculate for the median so start from (median + 1)
* In case of value greater than Median, the Median must be subtracted from that value for positive number
* Also by removing duplicates of median we may speed up the code
*/
for (i = median + 1; i < m; ++i)
dist += A[i] - key;

printf("%d\n", dist);
}
}
return 0;
}


### Code ( Insertion Sort ):

/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<stdio.h>
#include<stdlib.h>

static int A[512];

int main(){
register int n,m,i,j;
scanf("%d", &n);

while (n--){
scanf("%d", &m);
int dist = 0, median, key;

for (i = 0; i < m; ++i){
scanf("%d", &A[i]);
}

/**
* Insertion Sort:
* Two portions sorted and unsorted
*/
for (i = 1; i < m; ++i){
key = A[i];
j = i - 1;

/**
* Compare every element with sorted portion ( elements to its left )
*/
while (j >= 0 && A[j] > key){

/**
* While comparing shift items to right until the comparison is false
*/
A[j + 1] = A[j];
--j;
}

/**
* When comparison is False or done, we found the sorted position for that element so we insert it in that position
*/
A[j + 1] = key;
}

/**
* Find Median value, left shift once is divided by Two
*/
key = A[m >> 1];

/**
* Since elements after Median is bigger than Median So, we need to find absolute value of them
*/
for (i = 0; i < m; ++i){
dist += abs(key - A[i]);
}

printf("%d\n", dist);
}
return 0;
}


### Code ( using vector & n-th element to find median ):

/**
* @author  Quickgrid ( Asif Ahmed )
*/

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>

using namespace std;

int main(){
register int n, m, i, j;
int key, num;
vector<int> v;

scanf("%d", &n);
while (n--){
scanf("%d", &m);

if (m == 1){
/**
* If only 1 input then no need to process
* For 1 input the distance is always Zero
*/
scanf("%*d");
printf("0\n");
}

else{
/**
* Input an a value and push it to vector
*/
v.clear();
for (i = 0; i < m; ++i){
scanf("%d", &num);
v.push_back(num);
}

/**
* sort for median
*/
nth_element(v.begin(), v.begin() + v.size()/2, v.end());

/**
* Get the value of median
*/
key = v[v.size()/2];

/**
* Get distance of every value from median
*/
int dist = 0;
for (j = 0; j < i; ++j)
dist += abs(key - v[j]);

printf("%d\n", dist);

}
}
return 0;
}