Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Similar documents
Section 29: What s an Inverse?

Lecture 10: Powers of Matrices, Difference Equations

Numerical Methods Lecture 2 Simultaneous Equations

Basic Linear Algebra in MATLAB

Matrix-Vector Operations

Dot Products, Transposes, and Orthogonal Projections

Lecture 3: Special Matrices

Lab 2: Static Response, Cantilevered Beam

MAT 343 Laboratory 3 The LU factorization

Matrix-Vector Operations

1 Newton s 2nd and 3rd Laws

M. Matrices and Linear Algebra

Getting Started with Communications Engineering

+ MATRIX VARIABLES AND TWO DIMENSIONAL ARRAYS

LAB 2 - ONE DIMENSIONAL MOTION

MATH 320, WEEK 6: Linear Systems, Gaussian Elimination, Coefficient Matrices

Numerical Methods Lecture 2 Simultaneous Equations

Example: 2x y + 3z = 1 5y 6z = 0 x + 4z = 7. Definition: Elementary Row Operations. Example: Type I swap rows 1 and 3

Designing Information Devices and Systems I Fall 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Introduction to Computational Neuroscience

Introduction to Matlab

Linear Algebra Handout

MATLAB BASICS. Instructor: Prof. Shahrouk Ahmadi. TA: Kartik Bulusu

BCMB/CHEM 8190 Lab Exercise Using Maple for NMR Data Processing and Pulse Sequence Design March 2012

Matrix-Vector Products and the Matrix Equation Ax = b

Math Lab 10: Differential Equations and Direction Fields Complete before class Wed. Feb. 28; Due noon Thu. Mar. 1 in class

(Mathematical Operations with Arrays) Applied Linear Algebra in Geoscience Using MATLAB

Linear Algebra, Summer 2011, pt. 2

x n -2.5 Definition A list is a list of objects, where multiplicity is allowed, and order matters. For example, as lists

The Gauss-Jordan Elimination Algorithm

Math 308 Midterm Answers and Comments July 18, Part A. Short answer questions

Chapter 1 Linear Equations. 1.1 Systems of Linear Equations

Math 256: Linear Algebra Dr. Heather Moon contributing-authors: Dr. Thomas J. Asaki, Dr. Marie Snipes, and Dr. Chris Camfield.

Introduction to SVD and Applications

LINEAR ALGEBRA KNOWLEDGE SURVEY

Project 2: Using linear systems for numerical solution of boundary value problems

Designing Information Devices and Systems I Spring 2018 Lecture Notes Note Introduction to Linear Algebra the EECS Way

Linear Algebra Section 2.6 : LU Decomposition Section 2.7 : Permutations and transposes Wednesday, February 13th Math 301 Week #4

Looking hard at algebraic identities.

EXPERIMENT 2 Reaction Time Objectives Theory

Conformational Analysis of n-butane

Lecture 9: Elementary Matrices

Algebra Review C H A P T E R. To solve an algebraic equation with one variable, find the value of the unknown variable.

LS.1 Review of Linear Algebra

Roberto s Notes on Linear Algebra Chapter 4: Matrix Algebra Section 7. Inverse matrices

(Linear equations) Applied Linear Algebra in Geoscience Using MATLAB

Experiment 0 ~ Introduction to Statistics and Excel Tutorial. Introduction to Statistics, Error and Measurement

Chapter 2. Matrix Arithmetic. Chapter 2

Computer Model of Spring-Mass System, Part 3 of Springs Lab

7.6 The Inverse of a Square Matrix

Lesson U2.1 Study Guide

Appendix 2: Linear Algebra

Lecture 5b: Starting Matlab

MAT2342 : Introduction to Applied Linear Algebra Mike Newman, fall Projections. introduction

Math Week 1 notes

Applied Linear Algebra in Geoscience Using MATLAB

Elementary maths for GMT

Linear Algebra. The analysis of many models in the social sciences reduces to the study of systems of equations.

TEACHER NOTES MATH NSPIRED

The following steps will help you to record your work and save and submit it successfully.

MITOCW enzyme_kinetics

Math 552 Scientific Computing II Spring SOLUTIONS: Homework Set 1

(Refer Slide Time: 1:13)

Math 123, Week 2: Matrix Operations, Inverses

SECTION 2: VECTORS AND MATRICES. ENGR 112 Introduction to Engineering Computing

Learning Packet. Lesson 5b Solving Quadratic Equations THIS BOX FOR INSTRUCTOR GRADING USE ONLY

Physics 212E Spring 2004 Classical and Modern Physics. Computer Exercise #2

22A-2 SUMMER 2014 LECTURE Agenda

Linear Systems. Math A Bianca Santoro. September 23, 2016

Recitation 8: Graphs and Adjacency Matrices

Vectors and Vector Arithmetic

1 Review of the dot product

VPython Class 2: Functions, Fields, and the dreaded ˆr

Learning to Use Scigress Wagner, Eugene P. (revised May 15, 2018)

2.1 Definition. Let n be a positive integer. An n-dimensional vector is an ordered list of n real numbers.

CS227-Scientific Computing. Lecture 4: A Crash Course in Linear Algebra

chapter 12 MORE MATRIX ALGEBRA 12.1 Systems of Linear Equations GOALS

MAC Learning Objectives. Learning Objectives (Cont.) Module 10 System of Equations and Inequalities II

Chapter 9: Systems of Equations and Inequalities

courses involve systems of equations in one way or another.

MAC Module 2 Systems of Linear Equations and Matrices II. Learning Objectives. Upon completing this module, you should be able to :

Biological Pathways Representation by Petri Nets and extension

Section 4.6 Negative Exponents

EE263 Review Session 1

Vectors Part 1: Two Dimensions

Numerical Linear Algebra

k is a product of elementary matrices.

A First Course on Kinetics and Reaction Engineering Example 1.4

2 Systems of Linear Equations

Sums of Squares (FNS 195-S) Fall 2014

Row Reduction

Roberto s Notes on Linear Algebra Chapter 10: Eigenvalues and diagonalization Section 3. Diagonal matrices

Numerical Linear Algebra

Linear Motion with Constant Acceleration

Numerical Linear Algebra

Motion II. Goals and Introduction

Recall, we solved the system below in a previous section. Here, we learn another method. x + 4y = 14 5x + 3y = 2

There are two things that are particularly nice about the first basis

Elementary Linear Algebra

Math Lab 8: Electric Fields Integrating Continuous Charge Distributions II Due noon Thu. Feb. 1 in class

Transcription:

Lab 2 Worksheet Problems Problem : Geometry and Linear Equations Linear algebra is, first and foremost, the study of systems of linear equations. You are going to encounter linear systems frequently in real-life biological systems as the stoichiometric coefficients for a biochemical reaction, like you saw in Lab, or as approximations to differential equations in population biology, for example. In high school algebra, you learned that linear equations have a geometric meaning. We re going to explore this more using the techniques the formalism of linear algebra. Try to find a solution to the following system of two equations in two unknowns: 5x y = 2 5x y =. Some of you will point out very quickly that this system has no solution. Explain why. Now, what does this mean geometrically? Use the plot() function to display both of these lines in the same figure within a 20 20 window. Section 2. of Strang gives a nice way of thinking about the rows and columns of a matrix. In linear algebra we go from a system of linear equations (however large) to a matrix equation. The matrix equation consists of a coefficient matrix; a vector of unknowns (corresponding to the coefficient matrix); and a constraint vector (which completes the information we have in the system). For this system, we can read off the coefficients and assign them to the matrix as follows: [ x y 5 5 ] [ x y ] = [ 2 As you ll notice, the first row of this matrix contains the coefficients of the two unknown variables, x and y for the first equation. You have already solved this equation once by hand or by inspection but now let s use the linear algebra tools in MATLAB. ]

In class you will discuss elimination (or Gaussian elimination ), which is a method for solving matrix equations. The most popular algorithm for carrying this out is called LU decomposition ( lower-upper decomposition), in which the coefficient matrix is transformed into a matrix with elements only in the upper-right triangle this immediately leads to back substitution (i.e, solving for one of the equations for y in terms of x, then substituting your answer into the other equation to solve for x). See what happens when you try to solve the above matrix equation by LU decomposition. You can use the command >> x = M\b (Note: this is a forward slash!) Here, M is the coefficient matrix, x is the solution vector, and b is the constraint vector. As expected MATLAB cannot find a solution. Explain in your own words why this is the case. The message on your screen will indicate that M is a singular matrix this means that it is not invertible. This point is a key takeaway from section.: Dependent columns: Mx = 0 has many solutions. M is not invertible. Now, consider the set of equations 2y x = y 7x =. First, solve the system by hand and see if there is a solution. Next, plot the two lines on the same figure (with an appropriate window) and show the intersection (if any). Finally, write down a matrix equation for this system and evaluate it using MATLAB. Do these methods all agree? Problem 2: Writing a Matrix Multiplier In this problem you are going to study the mechanics of matrix multiplication in-depth and finish with your first MATLAB program. Suppose you have two matrices, A and B. If A has n rows and B has n columns, then you can multiply the two. There are several different methods for carrying out this operation, and Strang explains them in detail in section 2.. The two most important methods are the inner-product method (the conventional way) 2

and the outer-product method. Mathematicians call the dot product an inner product we worked with this in Lab. So each element of the product matrix, which we denote by (AB) ij (the entry in the i th row and the j th column), is a dot product: (AB) ij = (row i of A) (column j of B). It should be clear to you that both items in parentheses on the right-hand side of this equation are vectors. This equation tells us why matrix multiplication is only allowed when the number of rows of A matches the number of columns of B. MATLAB has a built-in matrix multiplier, which you call when you type A * B. However, by writing your own version, you will gain a deeper understanding of how the procedure works, and you will accomplish your first substantial MATLAB coding exercise. The best way to start a program is the same way as you would start at term paper by outlining it. For the outer-product multiplication, your outline should include the following (for each item, there is a mini-task to help familiarize you with the operation): Initializing the product, AB, as a matrix with the correct dimensionality. zeros(n, m) creates a matrix with n rows and m columns, and fills it with zeros. Make a 5 matrix filled with zeros. Indexing: In order to assign the correct value to each (AB) ij, you will need to identify it properly, for example, via AB(i, j). You will call these one-at-a-time via the for loops. A nested for loop, which is one for loop contained inside another. This is how you will move through one-by-one the elements of the product matrix. The first loop might step through the rows and the second loop through the columns. Start with a matrix of random entries via rand(). Use a for loop and indexing to print out to the screen all of the elements in the second row. Calculating a given element. Use the dot() function. Calculate the dot product of (, 5, 7, ) and (,,, 9) by hand. Although computational scientists often use the two words interchangeably, script and function mean two slightly different things in MATLAB. Both are created as M-files (.m extension), but functions accept input variables and return outputs, whereas scripts do a fixed set of operations every time you call them. In other words, if you want to do a general operation on any variable you like, use a function; if you want to save several lines of code for something in the future, use a script. You ll be creating a function today. This screen capture shows you how to create a new function () and provides a template that you can use for your program. The function begins with function, followed by the name of the variable you want to output. You will start by naming your function and defining the input variables, which here are A and B (2). Make sure that you choose a name that is clear

to you; that is not the same as a built-in MATLAB command; and that does not contain any spaces. Each time you call this new function, you will use the name that you define now. It s often useful to add a few comments to help you keep track of what your functions do and when you made them (). Comments are indicated by any line that starts with % MATLAB will ignore these lines. Underneath the comments section comes the body of your code (). If you are naming your product matrix AB, then you will need to start by initializing it. Whenever a line of your code performs an operation, end it with a semicolon to avoid outputting the result. You can save your M-file using the dropdown menu near the New menu. By default, MATLAB creates a folder (or directory ), called MATLAB, which, if you are using a Mac, shows up in your Documents folder. This is a good place to store your file. Above the Editor window is a white bar, which displays the current path. This is the place MATLAB looks when you try to execute your function. Make sure that your file is saved to the current path. (If you don t understand this part ask your TA.) Now, test your new function by multiplying two random matrices. If you chose the same name for your function as we use in the example, then your syntax will be >> A = rand() >> B = rand(, ) >> inner multiplier(a, B)

Use the built-in multiplier, A * B, to check this result. Problem : Gene Expression Graphs We have already discussed a few places in biology in which matrix multiplication is useful. Another example is the use of a type of mathematics called graph theory to study gene expression. There is an entire subfield of biology, known as systems biology, which uses these techniques to understand the extremely complex ways in which genes are regulated in an organism (among other things). Even as every cell in the human body has the same genome, each cell can also express differently producing different proteins, a different phenotype, and sometimes an entirely different cell type. Systems biologists often create graphs as a means for visualizing and studying the ways in which one gene s activity affects those of other genes. We re going to look as a very simple example at the graph drawn by Strang in Example 2.A and shown in this problem. In this graph, each point or node represents a gene, and a line or edge represents an interaction. For example, gene, when expressed, can activate both gene 2 and gene. We should note that, in a real-life example, the graph will be a directed graph, in which the edges are replaced by arrows showing the direction of the regulatory action (i.e, which gene does the regulating). To study this system, you can create an adjacency matrix: S = 2 0 0 0 0 0 0 2 5

This matrix contains all of the information about the connectivity of the graph for example, the ones in the first row tell us that gene regulates genes 2 and. Strang explains that S 2 tells us the number of two-step walks or paths between any pair of nodes (recall that S 2 is shorthand for SS). Biologically, this could give us information on indirect regulatory pathways that influence a particular gene. Use your matrix multiplier to determine the number of four-step walks between any pair of genes. You can either call your function four times, or start with S from the textbook. Afterwards, use the built-in multiplier to check your answer. Problem : Outer-Product Multiplier Now, you are going to write another function to implement the outer-product method for matrix multiplication. In the outer-product method, we multiply a column vector ( column) by a row vector ( row) to produce a matrix with the same number of rows as the column vector and the same number of columns as the row vector: (AB) ij = A i B j Note: Since A and B are vectors, we only need one index to identify their elements. This formula means that column i of AB consists of the column vector A scaled by the i th element in the corresponding column of B. To visualize this, plot the vector [ ] 2 then, in the same figure, plot this vector scaled (multiplied) by. Create a function which carries out outer-product multiplication between a 5-D column vector and a -D row vector. Follow the same procedure as before, starting with an outline to understand how this function will differ from the last one that you wrote. One useful command for this is.*, as in v.*, which multiplies each component of the vector v by and returns a vector. Another potentially useful trick is to index an entire column at once, rather than one element at a time. For example, >> M(2, :) identifies the entire second row of matrix M. Once you have written your function, use it to 6

evaluate AB = 6 7 [ ] 5 7. Before you write this function, though, calculate by hand what AB 2 is. Check your answer for AB. Problem 5: Stoichiometry, Revisited In Lab we introduced the stoichiometric matrix equation as a method for checking that a chemical reaction equation is properly balanced. In that problem we studied the first step of glycolysis, and we used the stoichiometric equation to perform elemental balance an expression of conservation of momentum. Another physical quantity that must be conserved in a chemical reaction is electric charge. Let s consider the important biochemical reaction by which the O 2 free radical is destroyed via what s called the superoxide dismutase enzyme: 2O 2 + 2H + O 2 + H 2 O 2 If you haven t learned about free-radical behavior in a biology class, then you might still have heard of these ions in the context of commercial anti-aging products. Indeed, there is biological evidence for the role of oxygen free radicals in aging processes, as well as in inflammation. It might be helpful to consult your Lab worksheet. You are given a partially set-up matrix equation below, where the first matrix, the electric charge matrix (whose entries, w, x, y, and z, are the net charges of each reaction component) needs to be filled in. Complete this matrix and then use your outer-product multiplier to check if the supplied stoichiometric ratios are correct. O 2 H + O 2 H 2 O 2 ES = e [ w x y z ] Here, e is the charge of an electron. The result of this multiplication is a matrix (i.e, a scalar). What operation on two vectors results in a scalar? 2 2 7