Question

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

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

0 0
Add a comment Improve this question Transcribed image text
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

Add a comment
Know the answer?
Add Answer to:
Exercise 1: Write MATLAB code to create a 5x5 matrix A with 2's on the diagonal, and -1 on the su...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use MATLAB to write your codes Consider a matrix A with block matrices as follows: A...

    Use MATLAB to write your codes Consider a matrix A with block matrices as follows: A = {A-11 A_12 0 A-22] It can be shown that the inverse of A can be calculated by inverse of submatrices if A11, and A22 are squared matrices: A^-1 = [A_11 A_12 0 A_22] = [A6-1 _11 -A^-1 _11 A_12 A^-1 _22 0 A^-1 _22 Now consider a Matrix A with following submatrices: A11 = identity matrix A22 = identity matrix A12 = [12...

  • This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an...

    This code NEEDS TO BE DONE IN MATLAB!!!! Write a function that takes one input, an integer number n, and returns a matrix that is nxn, where the value of each number is the distance from the upper-left to bottom-right diagonal. (Use while loops if you can!) Numbers below the diagonal should be negative, and numbers above the diagonal should be positive. For example, if your input was 5, your output would be: 0 1 2 3 4 -1 0...

  • 1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers...

    1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers of 1 through 15. Display your result back to the user with 2 decimal places (yes, 2 decimal places). You must implement your code using a for loop. The result should have the following format: The multiplication of 10*1 =10.00 The multiplication of 10*2 =20.00 The multiplication of 10'3=30.00 The multiplication of 10*15=150.00 2. (35 Points) Write MATLAB code to prompt a user for...

  • Write a MATLAB script, which asks the user to input the angle xd (in deg) and...

    Write a MATLAB script, which asks the user to input the angle xd (in deg) and then approximates sine of the angle using the first 5 terms of Taylor series as follows: n+12n-1 sin n-1 (2n-1)! 1 Note that x in Taylor's equation is in radian, so make sure to do the conversion first Start with defining the vector n=1:5 and then use element-wise operation and basic MATLAB commands to calculate the summation. To calculate the factorial of any number,...

  • on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A,...

    on matlab (1) Matrices are entered row-wise. Row commas. Enter 1 2 3 (2) Element A, of matrix A is accesser (3) Correcting an entry is easy to (4) Any submatrix of Ais obtained by d row wise. Rows are separated by semicolons and columns are separated by spaces ner A l 23:45 6. B and hit the return/enter kry matrix A is accessed as A Enter and hit the returnerter key an entry is easy through indesine Enter 19...

  • In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the ...

    In this exercise, you will work with a QR factorization of an mxn matrix. We will proceed in the way that is chosen by MATLAB, which is different from the textbook presentation. An mxn matrix A can be presented as a product of a unitary (or orthogonal) mxm matrix Q and an upper-triangular m × n matrix R, that is, A = Q * R . Theory: a square mxm matrix Q is called unitary (or orthogona) if -,or equivalently,...

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • Write the MatLab code for the following algorithm: reate maze algorithm 1) Function create mazel maze...

    Write the MatLab code for the following algorithm: reate maze algorithm 1) Function create mazel maze size: whole number, monsters: whole number) returns maze: [1.NI][1.N] of Strings 2) maze = [mazesizelimaze size] array of strings, filled with "0" 3) maze(random # 6etween 1 and mazesize] [random # between 1 and maze-size] 4) tempx - random whole number between 1 and maze size 5) tempy random whole number between 1 and maze size 6) while mazeltempx][tempd="P" "P" temps.# random whole number...

  • 4. Write a Matlab code that computes the Reynolds number, discharge coefficient, in the discharge...

    4. Write a Matlab code that computes the Reynolds number, discharge coefficient, in the discharge coefficient, and air flow rate for a given orifice plate geometry an difference. Your code should be able to compute the flow rate (mass and ΔΡ across an orifice plate. You will continue to use your code for the rest example set of data against which you can test your code is provided in from your code must be in the same units as shown...

  • Problem 1 (6 Points) Create the following variables in the code: Scalar Character (or single letter...

    Problem 1 (6 Points) Create the following variables in the code: Scalar Character (or single letter string) An array of 6x1 A matrix of 2x4 The values could be anything; however, make sure you agree with the variable type the workspace is telling you. You need to have four variables in your workspace after you are done declaring them. Remember that every value (scalar, character, array, or matrix) needs to have a variable name-ans is not an accepted variable name....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT