


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Examples and instructions on performing vector operations and symbolic math using matlab. Topics include dot and cross products, finding vector lengths, using built-in functions, and working with symbolic variables and functions.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
Second 340 Matlab assignment
Vector operations with Matlab
You can take the dot and cross product as follows:
P0 = [ 1, 3, 6] P1 = [0 -2 8] a = dot(P0,P1) B = cross(P0,P1)
Then a will be 42, the dot product of P0 and P1, and B will be the vector (36, − 8 , −2) which is the cross product.
dot(P0,B)
gives the answer 0 as it should. You can find the length of a vector by
sqrt(dot(P0,P0))
or you can use the built-in function norm
norm(P0)
Besides these vector functions, Matlab has many functions which work on scalar quantities. Some useful functions are abs, cos, sin, tan, sqrt, log. exp, acos, asin, atan. Type help exp for example to find out about exp. Note that in Matlab, log(x) computes the natural logarithm ln(x). . Example: The length of the projection of b to a is |‖aa·b‖| so to find the length of the projection of (0, 1 , 3)
to (1, 1 , 5) you could type the following (since abs is the absolute value.).
b = [0 1 3] a = [1 1 5] abs(dot(a,b))/norm(a)
Example: Let P0 and P1 be as above and let P 2 = (0, 1 , 2). Find an equation of the plane through P0, P1, and P2.
P2 = [0 1 2] N=cross(P1-P0,P2-P0)
Matlab gives the answer N = (24, − 6 , −3) so you know the equation is 24(x − 1) − 6(y − 3) − 3(z − 6) = 0. Example: Find the distance from P1 to the line through P0 with direction (1, − 1 , 3)
L=[1 -1 3] dist = norm(cross(P1-P0,L))/norm(L)
I should point out that Matlab does not understand the i, j, k vector notation. Try typing in 3i+2j and see what happens. In Matlab, i and j both stand for the complex number
Symbolic math
To use matlab for symbolic math such as differentiation you need to tell it what variables you want to be symbolic with a syms command. For example
syms t x y
says that t, x, and y will all be symbolic variables. All the examples below presume you have first typed in a syms t x y command. We can now define functions in these symbolic variables. For example to define r(t) = ti + etj + sin tk and f (x, y) = cos(x ln y) type in
f = cos(x*log(y)) r = [t, exp(t), sin(t)]
You can differentiate such symbolic functions with the diff command. In the following, g = ∂^2 f /∂y∂x.
dfdx = diff(f,x) dfdy = diff(f,y) g = diff(dfdx,y)
The vector operations dot and cross work for symbolic functions. Unfortunately the norm command does not work for symbolic vectors. Also, while the dot command gives the correct answer, its result looks fairly complicated since it is meant to work with with complex valued functions as well as real valued functions. Consequently its result is peppered with conj() (which stands for complex conjugate). To avoid this you can use the sum( .* ) operation which performs the real dot product. So to calculate r′(t)·r′′(t) and | r′(t) | you could type
v = diff(r,t) a = diff(v,t) sum(v.a) speed = sqrt(sum(v.v))
(For those who wonder why this works, v.*a multiplies the corresponding entries of v and a, and sum adds up the resulting entries.)
Evaluating symbolic functions
Matlab has a subs command which substitutes something in for a variable. Let us find r(.5) and f (1, 2).
subs(r,{t},{.5}) subs(f,{x,y},{1,2})
This substitutes .5 for t in r and substitutes x = 1 and y = 2 in f. You can also use the subs command to compose functions. For example, if s(t) = t^2 i − t^3 j then f (s(t)) can be calculated as
subs(f,{x,y},{t^2,-t^3})
It may also be calculated as follows.
s = [t^2 -t^3] subs(f,{x,y},s)
If you are evaluating a function many times, it may be worthwhile converting it to an inline function or anonymous function which can be evaluated using fewer keystrokes and more natural syntax. This is most easily done with scalar valued functions. Below, the scalar valued function f is converted to an inline function inf and then evaluated at (1, 2) and (3, 4).
inf = inline(vectorize(f),’x’,’y’) inf(1,2) inf(3,4)
Inline functions are relatively slow and recent versions (7.0 and greater) of matlab introduced an alternative, anonymous functions. To convert f to an anonymous function and evaluate:
anf = @(x,y) cos(x*log(y)) anf(1,2) anf(3,4)
Drawing graphs and contour plots.
To draw the graph of f (x, y) = x^2 − y sin(x) for − 1 ≤ x ≤ 2 and 0 ≤ y ≤ 4 you can use the following commands:
syms x y f = x^2 - y*sin(x) ezsurf(f,[-1 2 0 4])
origin. (The limit does not exist because as (x, y) approaches the origin along the parabola x = y^2 then f approaches 1, but as (x, y) approaches the origin along any line y = mx then f approaches 0.)
Problems to turn in, Due Monday, Oct. 30
You are encouraged to work on these problems in groups of two or three. Each group should turn in only one copy of the assignment, (but put all your names on the assignment you turn in). Your assignment should include a printout of your matlab session (i.e., the matlab commands you gave and matlab’s responses). Answers should be clearly indicated and can be hand written or typed. All the problems below should be solved using Matlab, even if they are easy to do by hand.
Problem 1: Find the unit vector in the direction of (1, − 2 , 4).
Problem 2: Find the angle (in radians) between the vectors (1, − 2 , 3) and (3, 1 , 4). (The (radian) arccosine function cos−^1 x is acos(x).)
Problem 3: Print out a graph and a plot of the level curves for the function
f (x, y) = (x^3 − 3 x)(2y^3 − y)
for − 2 ≤ x ≤ 2 and − 1 ≤ y ≤ 1. On your plot of the level curves draw (by hand) vectors in the direction of the gradient of f at four points near the four corners.
Problem 4: Examine the graph of f (x, y) = (x^3 + 8y^3 − 10 y^4 )/(x^2 − 2 xy + 4y^2 ) at several different magnifications. Decide on the basis of your observations whether or not lim(x,y)→(0,0) f (x, y) exists and if so, what it is. Decide on the basis of your observations whether or not f is differentiable at (0, 0) if we set f (0, 0) = lim(x,y)→(0,0) f (x, y).
Problem 5: Examine the graph of f (x, y) = (x^3 − 8 y^3 − 10 y^4 )/(x^2 − 2 xy + 4y^2 ) at several different magnifications. Decide on the basis of your observations whether or not lim(x,y)→(0,0) f (x, y) exists and if so, what it is. Decide on the basis of your observations whether or not f is differentiable at (0, 0) if we set f (0, 0) = lim(x,y)→(0,0) f (x, y).