I have to follow a specific algorithm for this code but my L and U are coming out as single digits, not matrix and I am not sure why/ how to do that (algorithm is attached as picture)

6 views (last 30 days)
function [L, U] = crout_LU( A ) %this function decomposes matrix A into a lower triangular matrix L and an %upper triangular matrix U, using Crout's method such that LU = A % A = matrix of coefficients % U = upper triangular matrix % L = lower triangular matrix
[r,c] = size(A); n = length(r); L = zeros(n); U = eye(n);
if r ~= c error('A is not a square matrix') elseif any(diag(A) == 0) warning('diagnol element of is 0') end
for k = 1:n L(k,1)=A(k,1); U(k,k)=1; for i = 2:n U(1,k)=A(1,k)/L(1,1); end
for i = k:n
sum_i = 0;
for m = 1: k - 1
sum_i = sum_i + L(i,m)*k(m,k);
end
L(i,k) = A(i,k) - sum_i;
end
for j = k+1:n
sum_j = 0;
for z = 1: k-1
sum_j = sum_j + L(k,z)*U(z,j);
end
U(k,j) = (A(k,j) - sum_j)/L(k,k);
end
end
end

Answers (1)

David Goodmanson
David Goodmanson on 20 Oct 2016
Edited: David Goodmanson on 20 Oct 2016
Hi Melissa, you have
[r c] = size(A); n = length(r)
but r is just a scalar that tells you the number of rows in A, so length(r) = 1 and n is set to 1 which of course is not correct.
As a simple alternative to the debugger, it's easy to take semicolons off of the ends of lines to see what is going on. That leads to the problem pretty quickly.

Categories

Find more on Linear Algebra 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!