Question

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.)

Homework Answers

Answer #1

MATLAB Script:

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
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.)
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 .
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...
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...
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)...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Use the given transformation to evaluate the integral. (x − 6y) dA, R where R is...
    asked 17 minutes ago
  • Which document is necessary in establishing outsourcing relationships with an application service provider (ASP)? Service Level...
    asked 26 minutes ago
  • In order to conduct a hypothesis test for the population proportion, you sample 450 observations that...
    asked 32 minutes ago
  • Doctor’s Order: Vancomycin 500mg tab i po q12h X 7 days Available: Vancomycin 500mg tablets What...
    asked 45 minutes ago
  • Calculate the ΔG∘rxn for the reaction using the following information. 4HNO3(g)+5N2H4(l)→7N2(g)+12H2O(l) ΔG∘f(HNO3(g)) = -73.5 kJ/mol; ΔG∘f(N2H4(l))...
    asked 46 minutes ago
  • Question 03: Saturn Shoes (Pvt.) Ltd manufacture multi-style fashion boots for the residents of Missouri. Leather...
    asked 48 minutes ago
  • A highway with a design speed of 100 km/hr is designed with a sag curve connecting...
    asked 1 hour ago
  • Shift Registers can be used for serial/parallel interface applications. True or false?
    asked 1 hour ago
  • Scenario 1: To describe the instructors’ experience, the researcher records the year in which each instructor...
    asked 1 hour ago
  • develop a flowchart or pseudocode to check the prime numbers 1- below 100 what to do...
    asked 1 hour ago
  • Which of the following statements are true? I. The sampling distribution of ¯xx¯ has standard deviation...
    asked 1 hour ago
  • Which of the following methods of reporting cash flows provided by operating activities does the Financial...
    asked 1 hour ago