Posted: November 19th, 2022

MATLAB PROJECT 3

Please open and read the file Instructions on Project, placed on Project Assignment page, prior to working on the Project: “How to Create a Diary file” and “How to create and Run Function in MATLAB”.

Failure to fulfill the requirements on the format will result in a loss of points.

BEGIN with creating a diary file Project3.

Note: All exercises in this project will be placed in this diary file. Each exercise in the diary file should begin with the line

% Exercise#

Please use the command

format compact

to suppress extra line-feeds.

Part I. Subspaces & Bases Difficulty: Moderate

Exercise 1 (6 points) In this exercise, you will be given two matrices A and B. You will write a set of commands in MATLAB that will determine whether the Col A and Col B are subspaces of the same space m. If yes, your code has to determine if dim ColA = dim ColB and, finally, whether Col A = Col B. (Obviously, when two subspaces have the same dimension, it might not be true that they are the same set. For example, a line through the origin in 3 is a one dimensional subspace of 3 but two lines might be different sets – the sets are equal if they have the same elements.)

Use MATLAB function rank within your code. Remember, the rank of a matrix can be defined as the dimension of the column space of the matrix.

INSTRUCTIONS:

**In MATLAB, create

function [ ] = subspace(A,B)

Begin the function with comparing the numbers of rows in matrices A and B. If the numbers of rows are different, Col A and Col B are subspaces of different spaces and cannot be equal – the program terminates (the command return terminates the program).

A possible code for this part with an output message is:

2

m=size(A,1);

n=size(B,1);

if m ~ = n

disp(‘Col A and Col B are subspaces of different spaces’)

return

else

fprintf(‘Col A and Col B are subspaces of R^ % i \ n’, m)

end

(Note: “n” in “% i\ n” has nothing to do with size(B,1) – it is a part of the syntax for the command “fprintf”.)

If Col A and Col B are subspaces of the same space, the program will continue:

You will calculate the dimensions of Col A and Col B which should be k and l, respectively, and display them in your message. (I suggest using “fprintf” command.).

Next, you will compare k and l and, if they are equal, compare their common value with the rank of a certain matrix that you will create using matrices A and B in order to decide whether Col A = Col B. After the comparison is made in your code, the output will be one of the three possible messages:

“k ~ = l, the dimensions of Col A and Col B are different”,

“k = l, the dimensions of Col A and Col B are the same, but Col A ~ = Col B”,

or

“Col A = Col B”.

Finally, you will compare k and l with m and return messages that will specify whether Col A and Col B are all m for the corresponding m or not. An example of the code of one of the four possible output messages is:

fprintf(‘k = m (% i = % i) Col A is all R^% i \ n’, k, m, m)

**Type the function subspace in your diary file

**Run the function subspace(A,B) on the following matrices:

(a) A=24236958273942216334−−−−−−−−−− , B = rref(A)

(b) A= magic(4), B = eye(4) (c) A= magic(4), B = eye(3) (d) A = magic (5); B=eye(5)

%Comment in your diary file, based on the output for the matrices in part (a), on a possible effect of elementary row operations on the column space of a matrix.

3

Exercise 2 (6 points) In this exercise you will be given an mn× matrix A. Your program has to create two bases (both are named B): one – for Col A and one – for m if Col A m≠, by using the columns of the given matrix A.

Your program should allow a possibility that the columns of A are not linearly independent, that is, not all columns of A will be in a basis. The set of columns of A can be “shrunk” to a basis for Col A by using the function shrink. Here is the code:

function B = shrink(A)

[~, pivot] = rref(A);

B = A(: , pivot);

**Create the function B = shrink(A) in MATLAB.

**Type the function shrink in your diary file

**Run each of the commands listed below (separately) on the matrix A=magic(4)

[~, pivot] = rref(A)

B = A(: , pivot)

% Place a comment in your diary file on the output for each of the commands.

**Create the function in MATLAB

function B=basis(A)

The function should start with the commands:

m=size(A,1);

A=shrink(A);

sprintf(‘a basis for Col A is \ n’)

B=A

This part will give you as an answer a basis B for Col A.

Then, you will be using a conditional MATLAB statement within your code to check whether the matrix B, that you found, is a basis for m (or, the same, whether Col A = m). If yes, the program breaks with the message:

sprintf(‘a basis for R^% i is \ n’, m)

(it will return your B)

If B is not a basis for m, you should expand B to a basis for m. Use the matrices B and eye(m) and the command shrink to create a new matrix D, which will be a basis for m. (A basis has to contain all vectors from B and some vectors from the matrix eye(m)). You should also write a set of commands within your code to verify whether the new matrix D is, indeed, a basis for m – the command rank will be helpful. If your code confirms that D is a basis for m, the output message should be:

sprintf(‘a basis for R^% i is \ n’, m)

B=D;

4

otherwise, the program breaks with a message similar to that:

disp (‘What? It is not a basis!?’)

**Type the function basis within the diary file.

**Run the function B=basis(A) for the following matrices:

(a) A = 10000001 (b) A= 24481200 (c) A=magic(5) (d) A= magic(4)

Part II. Isomorphism & Change of Basis Difficulty: Hard

Exercise 3 (8 points) In this exercise you will be given a set of polynomials P. You will determine whether it is a basis for the corresponding space of the polynomials. This could be done by using the isomorphism between a linear space of polynomials and n, for the corresponding n. If B is a basis, your function will (1) find a vector y of the P-coordinates of the polynomial Q (originally Q is written through the standard basis); and (2) write a given polynomial R through the standard basis if the vector r of its P-coordinates is the given.

**First, you should create the following function in MATLAB that will help you to run the code and also will be used later for a presentation of a matrix with some entries that are very close to 0:

function B=closetozeroroundoff(A)

[m,n]=size(A);

for i=1:m

for j=1:n

if abs(A(i,j))<10^(-7)

A(i,j) = 0;

end

end

end

B=A;

**Then, you will create a

function B=polyspace(P,Q,r)

where [](1),…,()PPPn= is a vector whose components are polynomials from the space 1nP− – a set of polynomials of degree at most (n-1) written through by the standard basis

E={}1,…,,1nxx−(in descending order according to the degree), Q is a single polynomial from the same space 1nP−, and r is a vector with n components.

5

Note on the form of a polynomial: for purpose of this program, it is required that the coefficient at the degree 1nx− must not be zero. However, the zero coefficient is accepted by the definition of the space 1nP− and some given polynomials do not have term 1nx−, that is, the coefficient is zero at 1nx−. Being able to work with such polynomials, we insert the coefficient 10^(-8) at 1nx− and it will be converted into a 0 after you run the function closetozeroroundoff in your code.

**You should begin writing the function polyspace with the commands:

format rat,

u=sym2poly(P(1));

n=length(u);

The command sym2poly(P(1)) takes the coefficients of the polynomial P(1) (in the descending order according to the degree) and writes them as a row vector (a 1n× matrix). Number n is the dimension of the vector space 1nP−, thus, 1nP− is isomorphic to the Euclidean space n. The number n will be used later in this program.

**To use the isomorphism, you will create an nn× matrix C, whose columns are the vectors of coefficients of the polynomials in the set P, and then convert to 0 the entries that are close to zero. The output matrix for this part is B. Here is a suggested code

C=zeros(n);

for i=1: n

C( : , i) = transpose(sym2poly(P(1)));

end

B=closetozeroroundoff(C);

Then you will check if B is a basis for n- I suggest the command rank.

If B is not a basis, the program has to terminate and produce the reduced echelon form of the matrix B.

% Having the reduced echelon form of the matrix B, you will make a comment explaining the reason why P is not a basis for n(put the comment in your diary file). In this case, the set of polynomials P does not form a basis for 1nP− either (a consequence of the isomorphism from 1nP− onto n).

The set of commands that outputs the results for this part may have a form:

sprintf(‘the polynomials in P do not form a basis for P%d’,n-1)

fprintf(‘the reduced echelon form of B is % \ n’)

A=rref(B)

return

**If B is a basis, create a message indicating that the polynomials in P form a basis for the corresponding space of the polynomials, and your function will continue with two more tasks:

6

(1) Find a row vector y of P-coordinates of the polynomial Q. Your output for this part should contain a message and the row vector y. It could have a form:

fprintf(‘the coordinates of the polynomial Q with respect to the basis P are% \ n’)

y = closetozeroroundoff(y)

(2) Find the coordinates of the polynomial R with respect to the standard basis, whose P-coordinates is the given vector r. The outputs have to be a message similar to the one above and the polynomial R written through the standard basis E by using the coordinates that you have found as coefficients. Use the command poly2sym applied to the row vector of the coefficients to get the polynomial R in the required form.

**Type the functions closetozeroroundoff and polyspace in your diary file.

**Then type:

x=sym(‘x’);

The last command will allow you to type the polynomials in the variable x in usual way. For example, you will type a polynomial 3231Qxx=+− in MATLAB command window as Q=x^3+3*x^2-1. If you do not put semicolon at the end of the line and hit “enter”, you will see the polynomial that you have typed

.

**Run the function B=polyspace for each of the sets of the variables below.

Please make sure that you will type single polynomials as it is indicated above. The set P of polynomials has to be typed as a row vector whose components are the polynomials in P separated by comma.

For example, P=[x^3+3*x^2-1, 10^(-8)*x^3+2, 10^(-8)*x^3+4*x^2+x, x^3+x].

This set will be the one in (a) which you will test on a basis for 3P .

(a) {}3283832331,102,104,Pxxxxxxxx−−=+−++++,

832()1064Qxxxx−=+++, 2310−=−

r .

(b) {}3838331,102,10,Pxxxxxx−−=−+++, Q and r are the same as in (a).

(c) {}484384284841,101,101,101,101Pxxxxxxxx−−−−=++++++++

432()1Qxxxxx=++++, =rones(5,1)

7

Part III. Application to Calculus

Exercise 4 (6 points) Write a MATLAB function called c=reimsum(P) which accepts as inputs a polynomial P and three scalars a, b, and n. And it returns a scalar c. The program will use Riemann sums to approximate the definite integral of the polynomial on the interval [,]ab. The Riemann sum calculation will use n subintervals of the equal length with the value of the function taken at the midpoint of each subinterval. The scalar c (output) gives the Riemann sum approximation of the definite integral.

Type the polynomials in the form given below (through the standard basis). The command x=sym(‘x’) will allow you to do that (see Part II of this project).

The program could use for loop, the command sym2poly, and the MATLAB function polyval.

Determine the outputs of your function on the interval []10,10− for the polynomials:

(a) ()42368Pxxx=−− and (b)()5352Pxxxx=−+

for 1,5,10,50,100.n=

For each polynomial give your results in a table which includes entries for n and the outputs c.

Exercise 5 (4 points) In this exercise you are given a polynomial and you will write a code that outputs an antiderivative of the polynomial. Please follow the format requirements that are the same for each exercise in this project.

Write a MATLAB

function B=polint(P)

which accepts as an input a polynomial P written through the standard basis (see Part II). The function has to calculate indefinite integral of the polynomial assigning a value 1 to an arbitrary constant. The output has to be a polynomial written through the standard basis as well.

Suggested commands within your code:

x=sym(‘x’), sym2poly, poly2sym, length.

A single for loop or a MATLAB function int(P) can be used.

**Type the function polint in your diary file and run it for the following polynomials:

(a) 543223456Pxxxxx=+++++, (b) 6341Pxx=−+.

Close out the diary file.

Place an order in 3 easy steps. Takes less than 5 mins.

professionalsessays.com