Question

Exercise 1: Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal,...

Exercise 1: Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1 on the super- and sub-diagonal. Then replace the (1,1) element of A with a 1.
(Make your commands capable of handling an arbitary sized NxN matrix by first defining N=5, then using the variable N as the size in each of the commands.)

Homework Answers

Answer #1

MATLAB Code:

close all
clear
clc

N = 3;
A = make_matrix(N);
disp('A (N = 3) ='), disp(A)

N = 5;
A = make_matrix(N);
disp('A (N = 5) ='), disp(A)

N = 7;
A = make_matrix(N);
disp('A (N = 7) ='), disp(A)

function A = make_matrix(N)
A = diag(2*ones(N,1));
for i = 1:1:N
for j = 1:1:N
if j == i + 1
A(i,j) = -1;
elseif i == j + 1
A(i,j) = -1;
end
end
end
A(1,1) = 1;
end

Output:

A (N = 3) =
1 -1 0
-1 2 -1
0 -1 2
A (N = 5) =
1 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
A (N = 7) =
1 -1 0 0 0 0 0
-1 2 -1 0 0 0 0
0 -1 2 -1 0 0 0
0 0 -1 2 -1 0 0
0 0 0 -1 2 -1 0
0 0 0 0 -1 2 -1
0 0 0 0 0 -1 2

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1...
Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1 on the super- and sub-diagonal. Then replace the (1,1) element of A with a 1. (Make your commands capable of handling an arbitary sized NxN matrix by first defining N=5, then using the variable N as the size in each of the commands.)
MATLAB:: Create a matrix with one through 15 on the diagonal. First create a one row...
MATLAB:: Create a matrix with one through 15 on the diagonal. First create a one row 15 column matrix with 1 through 15 in it. Then use the “diag” command .
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",  ...
MATLAB Write a script "readFile.m" which:   a. reads a given file "file.dat" into a matrix "XYZ",   b. extracts the columns of the data from "XYZ" into arrays "c1", "c2", ..., "cN",   c. plots some column versus another column, with a title, and labeled axes.   To test and debug your code, create a "file.dat" with 3 rows and N=2 columns ( so a 3×2 matrix ).   The first column should be sorted. You can just enter some numbers in "file.dat" in...
Part 2: Solve the following problems in MATLAB 1. Fill in the function E = myElim(A,...
Part 2: Solve the following problems in MATLAB 1. Fill in the function E = myElim(A, r_entry, r_pivot, c) to create an m by m elimination matrix ??. Remember that an elimination matrix looks like an identity matrix with one extra entry of ?? in row r_entry and column r_pivot. 2. Fill in the function M = myMult(A, c_pivot) to create an m by m multiplier matrix ??. Remember that a multiplier matrix looks like an identity matrix with the...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will...
1. Given a matrix variable "mat", write code using for loops, if statements, etc. that will accomplish the same as the following: matsum = sum(mat') Please make sure to store the result in "matsum" and dont use sum. This is what I have so far but it keeps telling me is wrong.... % mat has been initialized for you: mat = randi([-100,100],randi([15,20]),randi([15,20])); % write your statements that accomplish the code above: [r c]=size(mat); for i=1:c matsum=0; for j=1:r matsum=matsum+mat(j,i); end...
the Markov chain on S = {1, 2, 3} with transition matrix p is 0 1...
the Markov chain on S = {1, 2, 3} with transition matrix p is 0 1 0 0 1/2 1/2; 1/2 0 1/2 We will compute the limiting behavior of pn(1,1) “by hand”. (A) Find the three eigenvalues λ1, λ2, λ3 of p. Note: some are complex. (B) Deduce abstractly using linear algebra and part (A) that we can write pn(1, 1) = aλn1 + bλn2 + cλn3 for some constants a, b, c. Don’t find these constants yet. (C)...
Given the codes for X^2(chisq) and G^2(likelihood rato test statisic). Please write the following code in...
Given the codes for X^2(chisq) and G^2(likelihood rato test statisic). Please write the following code in R. (e) (2 points) What proportion (pN ) of the observations generated in part a above, has |χ2 − G2| > 0.05? Note: The proportion you are calculating here (i.e. pN ) is an estimate of P(|χ2 −G2|>0.05). (f) (7 points) In the questions above, we did the calculations with a sample of size n=10. Nowlet’smakeitn=100. Withn=100,calculatepN forN = 200, 400, 600, · ·...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of...
There are N blocks, numbered from 0 to N-1, arranged in a row. A couple of frogs were sitting together on one block when they had a terrible quarrel. Now they want to jump away from one another so that the distance between them will be as large as possible. The distance between blocks numbered J and K, where JK, is computed as K-J+1. The frogs can only jump up, meaning that they can move from one block to another...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT