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 .
Please do this in Matlab Using a single for-loop and no if-statements, write a code that...
Please do this in Matlab Using a single for-loop and no if-statements, write a code that will create a 10x10 matrix with 2 on the diagonal, and -1 on any entry right next to the diagonal.
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input...
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input matrix A as its only input argument. Matrix A is a 3x3 matrix of random integers between 1 and 4 2. Produces and output matrix B as its only output argument. Matrix B will also be a 3x3 matrix. 3. Calculates the elements of matrix B such that: a. the values of B along the diagonal are equal to the respective elements in A...
Matlab Queatoin: The test suite generates a random M x N matrix with a random number...
Matlab Queatoin: The test suite generates a random M x N matrix with a random number of rows and columns that is assigned to the variable A . The element values of the matrix are random double precision numbers that fall in the range from -10 to 10. The matrix has between 5 and 10 rows and between 5 and 10 columns. Assuming A is defined in the workspace, write a script with commands to perform the operations described below...
Write a code in matlab for going through every element in a imported image matrix that...
Write a code in matlab for going through every element in a imported image matrix that has been gray scaled using for loops (going through every row and column) and changing every element that is equal to 255 to 1 and every number less then 255 equal to 0. Then find the x and y coordinate of each element now equal to 0 in that image matrix once again going through each row and column.
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)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT