%% Vector Fields
% Table of Contents
% Learning Goals
% Introduction
% A Quick Review of Vectors
% Class Activity- I
% Examples
% Homework
% References/Additional Info
%
%
%% Learning Goals
% This class activity is intended for two 50-minute class periods. The learning objectives of this activity is to familiarize with vector fields, sketch vector fields, and draw flow lines. Particularly, students will
% learn how to sketch vector fields in \mathbb{R}^2
% explore the flow lines of vector fields
% develop writing skills to explain mathematics in their own words
% Student will get to sketch some of the vectors and guess how the vector field will look like. Then they will use MATLAB to check their hand drawn vector fields and verify their guesses. Also, students will use the MATLAB sketches to make sense of flow lines of the vector fields.
%
%% Introduction
% What happens when wind blows in a region? It can be described as the motion of large number of molecules moving in different directions with different speeds in that region. It is useful to note that each molecule is associated with a direction and a speed with which it moves in the region. Hence, each molecule in that region can be assigned with a vector. This process of assigning a vector to each molecule in the region can be described using a vector field function.
% \textbf{Definition:} Let D be a set in \mathbb{R}^2. A \textbf{vector field} on D is a function \textbf{F} that assigns to each point (x,y) in D a two-dimensional vector $\textbf{F} (x,y)$.D is called the domain of the vector field. This definition can be generalized to a vector field on \mathbb{R}^3 and \mathbb{R}^n
% .
% The flow lines of a vector field are the paths followed by a particle guided by the vectors, and the vectors in the vector field are tangent to the flow lines.
%
%% A Quick Review of Vectors
% Recall a \textbf{vector} is a quantity that has both direction and magnitude (length). Geometrically, we represent a vector \vec{v} by drawing an arrow; the direction of the arrow shows the direction of the vector and the length of the arrow shows the magnitude (length) of the vector.
% A two-dimensional vector \vec{v} is determined by two points in the plane: an initial point and a terminal point. One can draw a vector \vec{v}=<a_1,a_2> at a point (0,0) in the xy-plane by starting at (0,0), going a_1 units horizontally to (a_1,0)and then going vertically up a_2 units and joining a line from (0,0) to(a_1,a_2). For example, a vector <1,2> can be obtained by starting at (0,0), going forward 1 unit in the x-direction to (1,0)and then 2 units in the upward direction to (1,2). Then draw the vector <1,2> using an arrow starting at (0,0) and ending at (1,2). The same process works for any vector with initial point other than (0,0). We also notice that the vector <1,2> points in the north-east direction. Hence, one can see if the signs of the components are both positive, then the vector points in the north-east direction. The table below describes the relation between the signs of the components of a vector and the direction it points.
%
%
%% Class Activity- I
% Activity 1: Guess the direction of the vectors at the given points without sketching the vectors.
% a) <1,5>   at (2,3)
% b) <1,5>   at (2,-3)
% c) <-1,5>   at (2,3)
% d) <1,-5>   at (-2,3)
% e) <-1,-5>   at (-2,-3)
% Activity 2: Draw the vectors in Activity 1 at the given points in a piece of paper.
%
%% Examples
% In the next examples, we will explore vector fields that involves polynomials, rational, and trigonometric functions.
% \textbf{Example 1-} A vector field on \mathbb{R}^2 is defined by $\textbf{F}(x,y) = <x,y>$. Describe \textbf{F} by sketching some of the vectors.
% Solution: We first create the following table:
%
% For each point, we draw the corresponding vector at that point.
%
% This gives us an idea about how the vectors might behave in this vector field. We notice that the vectors are radiating in the outward direction.
% Next, we explore the MATLAB code to generate vectors for the above vector field.
% A)  The code below sketches the vector field without automatic scaling.

[x,y] = meshgrid(-8:2:8, -8:2:8);
% meshgrid returns 2-D grid coordinates based on the coordinates contained in vectors x and y.
% This generates a matrix for x and a matrix for y
% the size of the matrix depend on meshgrid limits
U = x;
V = y;
% The x-component and the y-component of the vector field
% All the four matrices x, y, U, and V have same size 
figure
% Create figure window
quiver(x,y,U,V,0,'k')
% quiver(x,y,U,V,scale,color) plots vectors as arrows with components <U,V> at the points (x,y).
% scale = 0 plots the vectors without automatic scaling. 
grid on 
% Add grid lines to a graph
axis([-8 8 -8 8])
% Set axis limits and aspect ratios
xlabel('x-axis')
% labels the x-axis of the current axes
ylabel('y-axis')
% labels the y-axis of the current axes
title('Example 1A','Color', 'b')
% Add title in blue

% B)  The code below sketches the vector field with automatic scaling.

[x,y] = meshgrid(-8:2:8, -8:2:8);
U = x;
V = y;
figure
quiver(x,y,U,V,'r') 
% We used automatic scaling for the velocity vectors by removing the scale = 0 
grid on
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 1B','Color', 'm')
% m refers to magenta

% \textbf{Example 2-} A vector field on \mathbb{R}^2 is defined by $\textbf{F}(x,y) = <-x,y>$. Describe \textbf{F} by sketching some of the vectors. Then use the following code to sketch the vector field.

[x,y] = meshgrid(-8:2:8, -8:2:8);
U = -x;
V = y;
figure
quiver(x,y,U,V,'k')
grid on 
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 2','Color', 'b')

% \textbf{Example 3-} A vector field on \mathbb{R}^2 is defined by $\textbf{F}(x,y) = <x+y,x-y>$. Describe \textbf{F} by sketching some of the vectors. Then use the following code to sketch the vector field.

[x,y] = meshgrid(-8:2:8, -8:2:8);
U = x+y;
V = x-y;
figure
quiver(x,y,U,V,'k')
grid on 
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 3','Color', 'b')

% \textbf{Example 4-} A vector field on \mathbb{R}^2 is defined by $\textbf{F}(x,y) = <xy, 1>$. Describe \textbf{F} by sketching some of the vectors. Then use the following code to sketch the vector field.

[x,y] = meshgrid(-8:2:8, -8:2:8);
U = x.*y;
V = x.^0;
figure
quiver(x,y,U,V,'k')
grid on 
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 4','Color', 'b')

% \textbf{Example 5-} A vector field on \mathbb{R}^2 is defined by $\textbf{F}(x,y) = <\sin x, \cos x>$. Describe \textbf{F} by sketching some of the vectors. Then use the following code to sketch the vector field.

[x,y] = meshgrid(-8:1:8, -8:1:8);
U = sin(x);
V = cos(x);
figure
quiver(x,y,U,V,'k')
grid on 
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 5','Color', 'g')

% \textbf{Example 6-} A vector field on \mathbb{R}^2 is defined by \textbf{F}(x,y)=<\frac{x^2}{x^2+y^2},\frac{y^2}{x^2+y^2}>. Describe \textbf{F} by sketching some of the vectors. Then use the following code to sketch the vector field.

[x,y] = meshgrid(-8:1:8, -8:1:8);
U = x.^2./(x.^2+y.^2);
V = y.^2./(x.^2+y.^2);
figure
quiver(x,y,U,V,0,'k')
grid on 
axis([-8 8 -8 8])
xlabel('x-axis')
ylabel('y-axis')
title('Example 6','Color', 'g')

%
%% Homework
% \textbf{Problem 1)} Describe the flow lines of the vector fields from the examples in words.
% \textbf{Problem 2)} Describe the following vector fields \textbf{F} on \mathbb{R}^2 by sketching some of the vectors.
% a) \textbf{F}(x,y)=<x^2,y^3>



% b) \textbf{F}(x,y)=<x+2,y-2>



% c) \textbf{F}(x,y)=<\sin x,0>



% d)    \textbf{F}(x,y)=<\sqrt{x^2+y^2},\sqrt{x^2+y^2}>



% e) \textbf{F}(x,y)=<\frac{1}{x},\frac{1}{y}> (Hint: Check the domain. F(x,y) is defined for x, y \neq 0 ).



% \textbf{Problem 3)} Create your favorite vector field (use interesting functions for the components).



%
%% References/Additional Info
% MathWorks. (n.d.). Quiver. https://www.mathworks.com/help/matlab/ref/quiver.html
% Stewart, J. (2015). Calculus: Early Transcendentals (8th ed.). Cengage Learning, Inc.
% Rogawski, J. & Adams, C. (2015). Multivariable Calculus: Early Transcendentals (3rd ed.). W.H. Freeman & Company.