please I wrote this code to solve an equation of linear systems using LU factorization but it keeps giving me zeros as the value of x. please can someone help? thanks in advance

6 次查看(过去 30 天)
function [ x,u,l ] = trial2( A )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
%let i represent columns
%let j repesent rows
% declaration comes first
n = size(A,1);
x = zeros(n,1);
b = zeros(n,1);
y = zeros (n,1);
% to compute for lower triangular matrix
for i = 1:(n-1);
for j = i+1:n;
d = A(j,i)/A(i,i);
A(:,i) = A(:,i)-d*A(:,j);
end
end
l = tril(A);
% to compute for the value of y using forward elimination
b(n) = l(n,n)*y(n);
for j = 2: n;
b(j) = b(j)-l(j,i)*y(i);
y(j) = b(j)/l(j,j);
end
% to compute for the upper triangular matrix
for i = 1:(n-1);
for j = i+1:n;
d = A(j,i)/A(i,i);
A(j,:) = A(j,:)-d*A(i,:);
end
end
u = triu(A);
% to compute for the value of x using backward substitution
y(n) = u(n,n)*x(n);
for j =n:-1:1;
x(j) = x(j) - u(j,i)* x(i);
x(j) = y(j)/u(j,j);
end
end

回答(1 个)

Jason R
Jason R 2014-6-13
It seems that you initialize y as a vector of zeros (y = zeros (n,1);) and then use y to solve for b(n) and b(j) without updating y to a non-zero vector. This makes b(n) and b(j) end up being 0. There may be more issues but this should help.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by