关键词 > MATH240/461

Justin’s Guide to MATLAB in MATH240/461 - Part 1

发布时间:2022-06-21

Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Justin’s Guide to MATLAB in MATH240/461 - Part 1

This Justin’s Guide” is an edited version of a guide by Justin Wyss-Gallifent. It is an instructional guide before the actual Project 1, with information you will need to have.

1. Method

The way this guide is written is that it is assumed that you will sit down at a MATLAB terminal and start. The commands that you give to MATLAB are given but the output is not. The output is, however, talked about, with the understanding that you will put in the commands and see the output. Then you can read about it and keep going.

2.  Starting MATLAB

Find a system on campus and run MATLAB. The important window (there will probably be a couple) will have a bunch of subwindows in it but the major panel on the right has a >> in it.  This is the prompt, it’s where we tell MATLAB what we want it to do.

3.  Simple Calculation

For example, to do 4+5 we just type it in and hit “return”

>> 4+5

4.  Solving a System

Now let’s do something having to do with MATH 240.

First we’ll solve a system of linear equations.  We do this using MATLAB’s solve command.  Due to the quirkiness with which this command operates we have to make sure we interpret the result correctly. For example, suppose we wish to solve the system

3x1 + x2 5x3  = 0

x1  5x2 + x3  = 2

x1 + x2 5x3  = 一1

what we type in is the following.

>>  [x1 x2 x3]=solve(’3*x1+x2-5*x3=0’,’x1-5*x2+x3=2’,’x1+x2-5*x3=-1’)

Note the apostrophes (single quotes), necessary because an equation must be treated as a string of characters and strings of characters in MATLAB are delimited by apostrophes.

Note here that we got three solutions.  What would happen if we fed MATLAB a system with not enough equations? Let’s try!

>>  [x1 x2 x3]=solve(’3*x1+x2-5*x3=0’,’x1-5*x2+x3=2’)

If you try this, MATLAB chokes.  The reason is that it doesn’t know what to do or how to give the solution. There are ways for MATLAB to solve this but they demand that we know more than we do, too. For now we’ll need to deal with the corresponding augmented matrices in order to get MATLAB to help us solve this system.

5.  Putting in a Matrix

We can put in a matrix easily. To put in

 5(2)    31

we do

>> A=[2  3  ;  5  -1]

6.  Matrix Operations

Suppose now you put in a matrix and you wish to manually do row operations to it. MATLAB doesn’t have any functions for doing row operations, instead we have to execute low-level commands to do so. This is easier than it seems.

Here is an augmented matrix

>> A=[1  -1  -3  7  ;  -3  1 4  -16  ; 4  -3  -5  12]

To get this to row-echelon form, first we’ll clean out the entries below the upper-left 1. To do this we need to add 3 times row 1 to row 2. In MATLAB the code is

>> A(2,:)=A(2,:)+3*A(1,:)

Confused?  I was!  The expression A(rownumber,:)  refers to the entire row, so basically this line is saying:

A(2,:) = A(2,:) + 3 * A(1,:)

   -    石   -   石           -

Row2             Row2              3*Row1

Try it! The result will be given.

Next we’ll add 一4 times row 1 to row 3.

>> A(3,:)=A(3,:)+(-4)*A(1,:)

Now you’ll see that we have 0s below the upper-left 1.  Next we need to get a 0 in the bottom row where the 1 is. The easiest way to do this is rst to interchange rows 2 and 3.

>> A([2  3],:)=A([3  2],:)

This is a confusing command but it does the job.

Now we add twice row 2 to row 3.

>> A(3,:)=A(3,:)+2*A(2,:)

The result you’ll see is the matrix in row-echelon form.  Remember this means any rows of 0s are at the bottom (none in this case) and the leading entries go down and right.

To get to reduced row-echelon form, we rst have to get 1s for all our leading entries.  The third row is the only problem, it needs to be multiplied by 1/9.

>> A(3,:)=1/9*A(3,:)

This means that row 3 is 1/9 times what it was.

Lastly we must clear out the entries above the leading 1s.

First above the rightmost leading 1.

>> A(1,:)=A(1,:)+3*A(3,:)

>> A(2,:)=A(2,:)+(-7)*A(3,:)

And then above the leading 1 to the left of that one.

>> A(1,:)=A(1,:)+1*A(2,:)

The result is now in reduced row-echelon form.  Since there are no free variables we can simply read off the solution: x1  = 3, x2  = 5 and x3  = 一3.

7.  Skipping to the Reduced Row-Echelon Form

Suppose now you want to solve a system of matrices by getting the augmented matrix in reduced row-echelon form but you don’t want to do all that work on the previous page.  The rref command does this in MATLAB. For example if I put in the augmented matrix (corresponding to a system of linear equations) for the previous page’s problem:

>> A=[1  -1  -3  7  ;  -3  1 4  -16  ; 4  -3  -5  12]

and then reduce it

>> rref(A)

we see that MATLAB saves us time compared to all the work we needed to do on the previous page. Here is another, this one has free variables at the end. We will see that after rref because there are non-pivot columns corresponding to variables.

>> A=[1  3 4  2;1  -3  0  1]

and then reduce it

>> rref(A)

we get the reduced form. It’s in decimals but clearly it corresponds to the augmented matrix

0(1)   1(0)          

Note that the third column is not a pivot column so x3  is free. Hence we have the solution

x1  =   2x3

x2  =   x3

x3  = free

8. Lastly, note that we can deal with matrices with unknown constants in them but we have to explicitly tell MATLAB that they are unknown constants. For example, suppose we wish to enter the matrix

 52   3

First we must tell MATLAB that h and k are to be dealt with symbolically. We do this with the syms

command

>>  syms h

>>  syms k

And now we can do

>> A=[-2  3 h  ;  5  -1 k]

>> rref(A)

Format directions: You should publish your m-file as a .pdf and upload it to ELMS under the Matlab 1 assignment by the due date.

You will need to include items in what you turn in that are not MATLAB commands.  For example, your name. You can do this by typing into the MATLAB prompt >> your name, preceded by the percent symbol %. MATLAB ignores the comment” you type on a line following this symbol. For example, if your name were Terence Hill, your rst lines could look like

>> % Terence Hill

>> % MATH 461 Fall 2020 Project 1

>> % Other group members: Bud Spencer, Franco Nero

Also, when you begin a new problem part, indicate that on the output with something like >> % Problem 1(a)

and you may want to include more to explain, e.g.

>> % Problem 1(a). Here is the augmented matrix A.

Below, a question marked with a star  is to be answered with a typed in answer, following the % .

But rst:  Before you start, if you are new to MATLAB, go through  Justin’s Guide” and type along in MATLAB as you read. That should be enough to get you through this project. Don’t turn in anything from your interactive work following the guide in MATLAB. To start fresh, you can us the edit command clear

command window”, which will erase what you’ve written so far in the command window). Clearing: For safety, before each question, you can type clear to clear out the memory.

Note:  You may work in groups of three or fewer people.  I recommend at least two per group.  You only have to hand in one project per group, so make sure all group members have their name represented.

Format: The command format rat” will cause your matrix outputs to appear as fractions. You might nd that convenient or necessary at times. The command format short” changes back to the standard format.

Use common sense:   For example, if you are asked to put a matrix in a certain form, and your matrix is already in that form, then just answer with some comment line such as

% The matrix is already in reduced echelon form.

The project problems begin on the next page.

1. Consider the system of equations

x1 + 2x2 3x3  = 一3

4x1 5x2 + 2x3  = 一2

2x1 + 3x2 x3  = 2

(a) Enter it into MATLAB as an (augmented) matrix named A.

(b) Use elementary row operations (as in part 6 of the guide) to reduce it to row-echelon form.

(c) Continue to reduced row-echelon form.

(d) 大 Give the solution of the system.

(If the system has no solution, say that. Otherwise, write the basic variables (those corresponding to pivot columns) as functions of numbers and free variables, if there are any.)

2. Consider the system of equations

x1 + 3x3 + x5  = 一1

x1 + x2 + x3 + 6x5  = 1

3x1 3x2 3x3 + x4 19x5  = 6

10x1 4x2 + 38x3 + 2x4 12x5  = 0

(a) Enter it into MATLAB as an (augmented) matrix named B.

(b) Use elementary row operations (as in part 6 of the guide) to reduce it to row-echelon form.

(c) Continue to reduced row-echelon form.

(d) 大 Give the solution of the system.

3. Enter the matrix

A =  2(1) '3

3

2

1

2

3

0

0

2

5

7(1) 7'

(a) Convert it to reduced row-echelon form using rref.

(b) 大 Rewrite this matrix with fractions rather than decimals.

(c) 大 Write the solution of the system which corresponds to this matrix (as if it were an augmented matrix).

4.  Suppose you wish to show that  ┌ ┐b(a) is in the span of ,┌ 12, ┌ ┐、6(1) for any a and b. (a)  First declare a and b as symbolic (unknown constants).

(b)  Enter a matrix A which can help you show this.

(c)  Use rref on the matrix A.

(d) Explicitly give the weights w1  and w2  such that  ┌ ┐b(a) = w1  12+ w2  ┌ ┐6(1) .

5.  Suppose you wish to show that the set of vectors

 '''1232' , '''2031' , '''1111' , '''2217' , '''312(3)'

is linearly dependent.

(a)  Enter a matrix A corresponding to the homogeneous system which can help you solve this.

(b)  Use rref on the matrix A.

(c) Give a nontrivial linear combination of the vectors which yields  .

(Hint: Make a free variable nonzero. You may leave the decimal approximations in your answer.)

6.  Suppose you wish to show that

'(┌)'(┐)    is not in the span of     '(┌)13'(┐)

(a)  Use rref on an appropriate matrix to show this fact.

(b) Explain why your calculation in (a) is sufficient.