Tutorial 1

View on MATLAB Online

Solving the components and length of vector

We use norm to calculate magnitude. Specifically
n = norm(v)
returns the Euclidean norm of vector v. This norm is also called the 2-norm, vector magnitude, or Euclidean length.

Question 1

p1=[3 2 -2]; p2=[-1 0 -2]; p3=[-2 1 5];
%% Question (a)
p2-p1, norm(p2-p1)
p3-p1, norm(p3-p1)
%% Question (b)
norm(3*p1+2*p3)
ans =
     -4   -2   0
ans = 4.4721
ans =
     -5   -1   7
ans = 8/6603
ans = 10.2740

Solving the angle between two vectors

Since there is no specific function from MATLAB, we self defined a new function called vectorAngle. The function is at the end of this page.

Question 2(a)

u=[2 -2 1]; v=[3 0 4];
vectorAngle(u,v)
ans = 0.8411

Question 2(b)

u=[sqrt(3) -7 0]; v=[sqrt(3) 1 -2];
vectorAngle(u,v)
ans = 1.7682

Question 2(c)

u=[2 1 0]; v=[1 2 -1];
vectorAngle(u,v)
ans = 0.7520

Area of triangle and unit vector

Question 3(a)

p=[1 -1 2]; q=[2 0 1]; r=[0 2 1];
areaTriangle(p,q,r)
ans = 2.4495

Question 3(b)

p=[2 -2 1]; q=[3 -1 2]; r=[3 -1 1];
areaTriangle(p,q,r)
ans = 0.7071

Question 3(c)

p=[-2 2 0]; q=[0 1 -1]; r=[-1 2 -2];
areaTriangle(p,q,r)
ans = 1.8708

Directional Derivatives

There is no direct function for that, but we will make it ourself here! The steps are as follows.
  1. We find the Jacobian matrix for the function.
  1. Then substitute the Jacobian matrix with the point.
  1. Then perform dot product between unit vector and the answer from (2).
The functions used are:
Jacobian matrix
jacobian(f,v)
computes the Jacobian matrix of f with respect to v.
Dot product
dot(A,B)
returns the scalar dot product of A and B.
Substitution
subs(s,old,new)
returns a copy of s, replacing all occurrences of old with new, and then evaluates s. Here, s is an expression of symbolic scalar variables or a symbolic function, and old specifies the symbolic scalar variables or symbolic function to be substituted.

Question 4(a)

syms x y z;
a=[4 0 -3];
func=x^2*y*z;
point={1,-1,1};
dot((a/norm(a)),subs(jacobian(func,[x y z]),{x,y,z},point))
ans = -1

Question 4(b)

syms x y z;
a=[4 -2 2];
func=sqrt(x*y*z);
point={3,2,6};
dot((a/norm(a)),subs(jacobian(func,[x y z]),{x,y,z},point))
ans =

Question 4(c)

syms x y z;
a=[3 6 -2];
func=x*y+y*z+z*x;
point={1,-1,2};
dot((a/norm(a)),subs(jacobian(func,[x y z]),{x,y,z},point))
ans = 3

Divergence of Vector Field

Divergence
divergence(V,X)
returns the divergence of vector field. V with respect to the vector X in Cartesian coordinates. Vectors V and X must have the same length.

Question 5(a)

syms x y;
f=[y^3 x*y];
divergence(f,[x y])
ans = x

Question 5(b)

syms x y z;
g=[4*y/(x^2) sin(y) 3];
divergence(g,[x y z])
ans =

Question 5(c)

syms x y z;
g=[exp(x) log(x*y) exp(x*y*z)];
divergence(g,[x y z])
ans =

Curl of Vector Fields

curl(V,X)
returns the curl of vector field V with respect to the vector X. The vector field V and the vector X are both three-dimensional.

Question 6(a)

syms x y z;
f=[3*x^2 2*z -x];
curl(f,[x y z])
ans =

Question 6(b)

syms x y z;
f=[y^3 x*y -z];
curl(f,[x y z])
ans =

Question 6(c)

syms x y z;
f=[1+y+z^2 exp(x*y*z) -(x*y*z)];
curl(f,[x y z])
ans =

Self-defined functions

Since MATLAB didn't have built in function for that, let's create our own function! Note that MATLAB requires all function definitions to be at the end of file.

Angle between vector

function angle = vectorAngle(u,v)
    angle=deg2rad(acosd(dot(u,v)/(norm(u)*norm(v))));
end

Area of triangle

function [area,unitVector] = areaTriangle(p,q,r)
    pq=q-p;
    pr=r-p;
    area=1/2*norm(cross(pq,pr));
    unitVector=cross(pq,pr)/norm(cross(pq,pr));
end

References:
  1. https://www.mathworks.com/help/matlab/ref/norm.html
  1. https://www.mathworks.com/matlabcentral/answers/101590-how-can-i-determine-the-angle-between-two-vectors-in-matlab
  1. https://www.mathworks.com/help/symbolic/divergence.html
  1. https://web.ma.utexas.edu/users/jwchong/M427L_ML2_Project_Guide.pdf
  1. https://www.mathworks.com/help/symbolic/sym.jacobian.html
  1. https://www.mathworks.com/help/matlab/ref/dot.html
  1. https://www.mathworks.com/help/symbolic/subs.html