








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
Examples of using matlab to find eigenvalues and eigenvectors of matrices, as well as solving differential equations. It includes problems on random matrices, finding the eigenvalues of matrix sums and differences, and solving a differential equation using laplace transforms.
Typology: Assignments
1 / 14
This page cannot be seen from the preview
Don't miss anything!









This is one of your assignments which got a nearly perfect score. Matlab Methods in math 341 Problems:
Randomizing the rand function based on the clocking of the computer:
rand('state', sum(100*clock)) Initializing a matrix A of 3x3 random numbers: [A] = rand(3,3) A = 0.0603 0.3633 0. 0.4954 0.5541 0. 0.3147 0.5905 0. The sum of the eigenvalues of A : sum(eig(A)) ans =
The product of the eigenvalues of A:
prod(eig(A)) ans = -0. The determinant of the eigenvalues of A: det(A) ans = -0. The trace of A: trace(A) ans =
The characteristic polynomial of A, starting with the coefficient of the highest power of λ :
poly(A) ans = 1.0000 -1.4527 0.0914 0.
The Rest of the Problems:
[A] = rand(6,6) A = 0.2420 0.2337 0.1465 0.8133 0.3300 0. 0.6352 0.1547 0.5819 0.9942 0.8951 0. 0.9111 0.9765 0.7870 0.4050 0.8239 0. 0.1198 0.2740 0.3764 0.8414 0.0735 0. 0.7898 0.8278 0.3719 0.1107 0.4153 0. 0.8101 0.9099 0.4599 0.6995 0.9227 0. sum(eig(A)) ans =
prod(eig(A)) ans =
poly(A) ans = 1.0000 -3.3719 -0.3452 0.7107 -0.4658 0.0362 0. trace(A) ans =
det(A) ans =
A = rand(7,7) A = 0.5470 0.1405 0.0889 0.2072 0.0071 0.6618 0. 0.2858 0.0779 0.1618 0.7681 0.9186 0.3835 0. 0.4952 0.6354 0.4658 0.1057 0.7340 0.0723 0. 0.6986 0.0276 0.0256 0.1233 0.0582 0.5182 0. 0.9661 0.2668 0.7078 0.2353 0.7246 0.1642 0. 0.9752 0.1201 0.0209 0.3989 0.5398 0.3957 0. 0.4271 0.9599 0.8195 0.4091 0.6087 0.9339 0. sum(eig(A)) ans =
prod(eig(A)) ans =
Eigenvalues of B:
eig(B) ans = -0. -0. -0.
Eigenvalues of C:
eig(C) ans = 0.0000 + 1.3132i 0.0000 - 1.3132i -0.0000 + 0.4014i -0.0000 - 0.4014i
Eigenvalues of the matrix [B C; C B]:
eig([B C; C B]) ans = -0.0197 + 0.9894i -0.0197 - 0.9894i -0.4045 + 0.1758i -0.4045 - 0.1758i
-0.0197 + 0.9894i -0.0197 - 0.9894i -0.4045 + 0.1758i -0.4045 - 0.1758i Eigenvalues of the matrix[B C; -C B]:
eig([B C; -C B]) ans = -1. -1. -0.
Eigenvalues of the matrix [C B; B C]:
eig([C B; B C]) ans =
-4. -0.0197 + 0.9894i -0.0197 - 0.9894i 0.0197 + 0.9894i 0.0197 - 0.9894i -0.4045 + 0.1758i -0.4045 - 0.1758i 0.4045 + 0.1758i 0.4045 - 0.1758i Eigenvalues of the matrix [C B; -B C]:
eig([C B; -B C]) ans = 0.0000 + 4.6015i 0.0000 - 4.6015i 0.0000 + 1.5742i 0.0000 - 1.5742i -0.0000 + 1.3311i -0.0000 - 1.3311i -0.0000 + 0.6298i -0.0000 - 0.6298i 0.0000 + 0.1071i 0.0 - 0.1071i Matrix B has all real eigenvalues, whereas matrix C has all imaginary eigenvalues. Since C has all zero entries on its diagonal, all its eigenvalues necessarily have 0 real part. Both matrix[B C; -C B] and matrix [C B; -B C] have the same magnitudes of their eigenvalues but the former is real and the latter is imaginary. Matrix [B C; -C B] and matrix [C B; -B C] are nearly the same matrix; they have their rows switched and also have a column operation performed on them, so the eigenvalues should be related but not the same. Matrix [B C; C B] and [C B; B C] have nearly the same eigenvalues; they are essentially the same matrix with a few of the rows switched around, so this is why we believe that the eigenvalues are the same with a few sign differences.
Initializing the matrix A:
The diagonal matrix D of the eigenvalues of A:
D = [2 0 0 0; 0 2 0 0; 0 0 2 0; 0 0 0 0] D = 2 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 The nilpotent N of the matrix A: N = inv(P)AP - D N = -20.0000 12.2474 10.9545 0.
-0.0000 0.0000 0.0000 0. -36.5148 22.3607 20.0000 0. -0.0000 0.0000 0.0000 0. N^2 is zero, thus the smallest k that N^k is zero is 2:
N^ ans = 1.0e-012 * -0.1705 0.1137 0.0284 0. -0.0290 0.0178 0.0159 0. -0.0000 -0.0568 -0.1705 0. -0.0162 0.0099 0.0089 0. Verifying that DN is equal to ND: D* N ans = -40.0000 24.4949 21.9089 0. -0.0000 0.0000 0.0000 0. -73.0297 44.7214 40.0000 0. 0 0 0 0 N*D ans = -40.0000 24.4949 21.9089 0 -0.0000 0.0000 0.0000 0 -73.0297 44.7214 40.0000 0 -0.0000 0.0000 0.0000 0
a) Initializing the variables x and y, along with declaring function f in terms of x,y:
syms x y f = y^4 + x^3 - 2xy^2 -x
f = y^4+x^3-2xy^2-x Solving for the critical points, where a are the x-values and b are the y-values:
[a b] = solve(diff(f, 'x'), diff(f, 'y')) a = b = 1/33^(1/2) 0 -1/33^(1/2) 0 1 - 1 1 -1/3 1/3i3^(1/2) -1/3 -1/3i3^(1/2) Since the last two critical points don’t exist in the real plane, we throw them out. Finding the Hessian of the function f: grad = jacobian(f, [x y]) grad = [ 3x^2-2y^2-1, 4y^3-4xy] H = jacobian(grad, [x y]) H = [ 6x, -4y] [ -4y, 12y^2-4x] Plugging in every single critical point from the matrix [a b], then finding the eigenvalues of each point. For critical point 1: subs(H, {x y}, {a(1) b(1)}) ans = [ 23^(1/2), 0] [ 0, -4/33^(1/2)] eig(ans) ans = -4/33^(1/2) 23^(1/2) This is a saddle point because the eigenvalues have mixed signs. For critical point 2: subs(H, {x y}, {a(2) b(2)}) ans = [ -23^(1/2), 0] [ 0, 4/33^(1/2)] eig(ans) ans = -23^(1/2) 4/33^(1/2) This is also a saddle point because the eigenvalues have mixed signs.
[ -2, -z, -y] [ -z, -2, 1-x] [ -y, 1-x, -4] First critical point:
subs(H, {x y z}, {a(1) b(1) c(1)}) ans = [ -2, 0, 0] [ 0, -2, 1] [ 0, 1, -4] eig(ans) ans =
-3+2^(1/2) -3-2^(1/2) Since the eigenvalues are all negative, this critical point is a maximum. Second critical point:
subs(H, {x y z}, {a(2) b(2) c(2)}) ans = [ -2, -(4+2^(1/2))^(1/2), (4+2^(1/2))^(3/2)-4(4+2^(1/2))^(1/2)] [ -(4+2^(1/2))^(1/2), -2, -22^(1/2)] [ (4+2^(1/2))^(3/2)-4(4+2^(1/2))^(1/2), -22^(1/2), -4] Here we used the double function for more readability: eig(double(ans)) ans = -6. -4.
Since the eigenvalues have mixed signs here, they represent a saddle point. Third critical point:
subs(H, {x y z}, {a(3) b(3) c(3)}) ans = [ -2, (4+2^(1/2))^(1/2), -(4+2^(1/2))^(3/2)+4(4+2^(1/2))^(1/2)] [ (4+2^(1/2))^(1/2), -2, -22^(1/2)] [ -(4+2^(1/2))^(3/2)+4(4+2^(1/2))^(1/2), -22^(1/2), -4] eig(double(ans)) ans = -6.
Since the eigenvalues have mixed signs here, they represent a saddle point. Fourth critical point:
subs(H, {x y z}, {a(4) b(4) c(4)}) ans = [ -2, -(4-2^(1/2))^(1/2), (4-2^(1/2))^(3/2)-4(4-2^(1/2))^(1/2)] [ -(4-2^(1/2))^(1/2), -2, 22^(1/2)] [ (4-2^(1/2))^(3/2)-4(4-2^(1/2))^(1/2), 22^(1/2), -4] eig(double(ans)) ans = -6. -3.
Since the eigenvalues have mixed signs here, they represent a saddle point. Fifth critical point:
subs(H, {x y z}, {a(5) b(5) c(5)}) ans = [ -2, (4-2^(1/2))^(1/2), -(4-2^(1/2))^(3/2)+4(4-2^(1/2))^(1/2)] [ (4-2^(1/2))^(1/2), -2, 22^(1/2)] [ -(4-2^(1/2))^(3/2)+4(4-2^(1/2))^(1/2), 22^(1/2), -4] eig(double(ans)) ans = -6. -3.
Since the eigenvalues have mixed signs here, they represent a saddle point. c)
syms x y w z f = x^2 - 4xy + 3y^2 + 6zw - 2xw f = x^2-4xy+3y^2+6zw-2xw [a b c d] = solve(diff(f, 'x'), diff(f, 'y'), diff(f, 'w'), diff(f, 'z')) a = b = c = d =
s^2*Y-
Y1 = sY Y1 = sY Using Matlab to solve the equation and transform back to original form: ilaplace(solve(Y2 - 2Y1 + Y - F, Y)) subs(H, {x y z}, {a(3) b(3) c(3)}) ans = [ -2, (4+2^(1/2))^(1/2), -(4+2^(1/2))^(3/2)+4(4+2^(1/2))^(1/2)] [ (4+2^(1/2))^(1/2), -2, -22^(1/2)] [ -(4+2^(1/2))^(3/2)+4(4+2^(1/2))^(1/2), -22^(1/2), -4] >> eig(double(ans)) ans = -6. Since the eigenvalues have mixed signs here, they represent a saddle point. Fourth critical point: >> subs(H, {x y z}, {a(4) b(4) c(4)}) ans = [ -2, -(4-2^(1/2))^(1/2), (4-2^(1/2))^(3/2)-4(4-2^(1/2))^(1/2)] [ -(4-2^(1/2))^(1/2), -2, 22^(1/2)] [ (4-2^(1/2))^(3/2)-4(4-2^(1/2))^(1/2), 22^(1/2), -4] >> eig(double(ans)) ans = -6. -3. 1. Since the eigenvalues have mixed signs here, they represent a saddle point. Fifth critical point: >> subs(H, {x y z}, {a(5) b(5) c(5)}) ans = [ -2, (4-2^(1/2))^(1/2), -(4-2^(1/2))^(3/2)+4(4-2^(1/2))^(1/2)] [ (4-2^(1/2))^(1/2), -2, 22^(1/2)] [ -(4-2^(1/2))^(3/2)+4(4-2^(1/2))^(1/2), 22^(1/2), -4] >> eig(double(ans)) ans = -6. -3. 1. Since the eigenvalues have mixed signs here, they represent a saddle point. c) >>syms x y w z >> f = x^2 - 4xy + 3y^2 + 6zw - 2xw f = x^2-4xy+3y^2+6zw-2xw >> [a b c d] = solve(diff(f, 'x'), diff(f, 'y'), diff(f, 'w'), diff(f, 'z')) a = b = c = d = s^2Y- >> Y1 = sY Y1 = sY Using Matlab to solve the equation and transform back to original form: >> ilaplace(solve(Y2 - 2Y1 + Y - F, Y)) ans = texp(t)+heaviside(t-1)((t-1)exp(t-1)-exp(t-1)+1)+2heaviside(t-1)exp(1/2t-1/2)((t- 1)cosh(1/2t-1/2)-2sinh(1/2t-1/2))-2heaviside(t-2)((t-2)exp(t-2)-exp(t-2)+1)- 2heaviside(t-2)exp(1/2t-1)((t-2)cosh(1/2t-1)-2sinh(1/2t-1)) d) Transforming the right hand side of the laplace: f = exp(-t) + 3Dirac(t-3) f = exp(-t)+3dirac(t-3) F = laplace(f) F = 1/(s+1)+3exp(-3s) Manually transforming the left hand side of the equation: Y2 = s^2Y - 3 Y2 = s^2Y- Y1 = 2(sY) Y1 = 2sY Solving and transforming the equation back to its original form: ilaplace(solve(Y2+Y1+Y-F,Y)) ans = (3heaviside(t-3)t-9heaviside(t-3))exp(-t+3)+(1/2t^2+3t)exp(-t)
h = @(t,y) [-t^2y(1) + y(2) + sin(t); y(1)] h = @(t,y) [-t^2y(1) + y(2) + sin(t); y(1)] ode45(h, [0 4*pi], [1; 2]) Graph:
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14