how to code a matrix A, such that A = [aij ] is defined by aij = max(i, j)

5 views (last 30 days)
i am trying to perform gaussian elimination with partial pivoting and scaling when n = 10. here is my code:
n = 10;
b = [1,1,1,1,1,1,1,1,1,1]'
p = (1:n)'; % initialize the pivoting vector
s = max(abs(A')); % compute the scale of each row
for k = 1:(n-1)
r = abs(A(p(k),k)/s(p(k)));
kp = k;
for i = (k+1):n
t = abs(A(p(i),k)/s(p(i)));
if t > r, r = t; kp = i; end
end
l = p(kp); p(kp) = p(k); p(k) = l; % interchange p(kp) and p(k)
for i = (k+1):n
A(p(i),k) = A(p(i),k)/A(p(k),k);
for j = (k+1):n
A(p(i),j) = A(p(i),j)-A(p(i),k)*A(p(k),j);
end
end
end
p % output the pivoting vector
A % output the overwritten matrix
% Step 2: Forward subsitiution to solve L*y = b, where
% L(i,j) = 0 for j>i; L(i,i) = 1; L(i,j) = A(p(i),j) for i>j.
y = zeros(n,1); % initialize y to be a column vector
y(1) = b(p(1));
for i = 2:n
y(i) = b(p(i));
for j = 1:(i-1)
y(i) = y(i)-A(p(i),j)*y(j);
end
end
y % output y
% Step 3: Back subsitiution to solve U*x = y
% U(i,j) = A(p(i),j) for j>=i; U(i,j) = 0 for i>j.
x = zeros(n,1); % initialize x to be a column vector
x(n) = y(n)/A(p(n),n);
for i = (n-1):-1:1
x(i) = y(i);
for j = (i+1):n
x(i) = x(i)-A(p(i),j)*x(j);
end
x(i) = x(i)/A(p(i),i);
end
x
I don't know how to write the code for my A matrix when A = [aij ] is defined by aij = max(i, j). Any suggestions? thanks
  1 Comment
Jan
Jan on 16 Oct 2018
How is the question and the code related? In "max(i,j)", what is "i" and "j"? What's wrong with max(i,j)?

Sign in to comment.

Answers (2)

Matt J
Matt J on 16 Oct 2018
A=max(1:n,(1:n).')

Torsten
Torsten on 16 Oct 2018
A loop is the easiest way to define your matrix, I guess:
A = zeros(n);
for i = 1:n
for j = 1:n
A(i,j) = max(i,j)
end
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!