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

조회 수: 4 (최근 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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by