You are on page 1of 4

MATH 224 - Tutorial 8 Solving Equations in MATLAB Roots Find the solutions to x2 5x + 6 = 0 by hand. x = 3 and x = 2.

2. Use the roots command to nd the same solutions. x = roots([1 -5 6]) Using a MATLAB script, print (using fprintf) the smaller and larger of the two solutions, in the following format. The smallest solution is x = .... The largest solution is x = .... x = roots([1 -5 6]); fprintf(The smallest solution is x = %.3f\n, min(x)); fprintf(The largest solution is x = %.3f\n, max(x)); real, imag Find the solutions to x2 + 2x + 5 = 0 by hand. x = 1 2 1 = 1 2i Use the roots command to nd the same solutions. x = roots([1 2 5]) What does the i represent in the MATLAB output? i is the square root of minus one. The complex numbers you get form a complex conjugate pair, and can be separated into the form x = a bi, where a is the real part of x, and b is the imaginary part of x. Get MATLAB to print (using fprintf) the real and imaginary part of your rst solution. The real part of the solution is a = .... The imaginary part of the solution is b = .... x = roots([1 2 5]); fprintf(The real part of the solution is a = %.3f\n, real(x(1))); fprintf(The imaginary part of the solution is b = %.3f\n, imag(x(1))); In class this week, well see that some DE solutions take the form of eat cos(bt) when we encounter complex numbers like m = a + bi. 1

MATH 224 - Tutorial 8 In MATLAB (not manually), write a script that sets m = -1+2*i, then separates m into the real and imaginary parts (m = a + bi), and nally plots y = eat cos(bt) on the interval t [0, 10]. m = -1 + 2*i; a = real(m); b = imag(m); t = linspace(0, 10); y = exp(a*t) .* cos(b*t); plot(t, y);
1 0.8

0.6

0.4

0.2

0.2

0.4

10

MATH 224 - Tutorial 8 Solving Linear Systems of Equations In the MATLAB Help, searching for Solving Linear Systems of Equations should get you to a useful page showing you the backslash operator in MATLAB. To solve a matrix equation AX = B for X, you set A and B in MATLAB, then use the command X = A B. Put the equations below into matrix form. 4a 3b = 8 2a + 4b = 1

4 3 2 4 Solve them by hand. a = 3.5 and b = 2

8 a = 1 b

Use a MATLAB script to solve the equations. A = [4 -3; -2 4]; B = [8; 1]; % or B = [7, 1]. means transpose; B must be a column vector X = A\B Extend the script with a fprintf command so that the output is nicer: The solutions are a = ... and b = ... fprintf(The solutions are a = %.2f and b = %.2f\n, X(1), X(2)); In the class, we will be solving for coecients that are part of the general solutions. Consider a general solution of the form y = c1 e2t + c2 e3t and the initial conditions y(0) = 1, y (0) = 2 Set up the equations for c1 and c2 by hand. We will need y : The equations are y = 2c1 e2t 3c2 e3t

y(0) = 1 = c1 e0 + c2 e0 = c1 + c2 y (0) = 2 = 2c1 e0 3c2 e0 = 2c1 3c2

MATH 224 - Tutorial 8 Get MATLAB to solve the equations for c1 and c2 . A = [1 1; -2 -3]; B = [1; 2]; X = A \ B; c1 = X(1) c2 = X(2) Generate a plot of y(t) for t [0, 5]. Conrm visually that your plot satises y(0) = 1 and y (0) = 2. t = linspace(0, 5); y = c1 * exp(-2*t) + c2 * exp(-3*t); plot(t, y);
1.4 1.2

0.8

0.6

0.4

0.2

0.5

1.5

2.5

3.5

4.5

To the plot of the solution, add the line y = 1 + 2t: your solution graph should be tangent to this line, since y(0) = 1 and y (0) = 2. This is a more robust visual check that our solution matches the desired initial conditions. hold on; plot(t, 1 + 2*t, r); xlim([0, 1]); % focus on part near t=0
3 2.5

1.5

0.5

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

You might also like