hello, can you help me develop a nxn matrix using Doolittle method displaying L,U,X while asking for matrix size, coefficient and the constant Thank you, I can't start it.

4 次查看(过去 30 天)
format short g
A=input('Enter the elements of the coefficient
matrix in array:');
Asks the user to enter the
elements of A
b=input('Enter the elements of the constant
vector:');
Asks the user to enter the
elements of b
L(1,1)=1; L(2,2)=1; L(3,3)=1; Sets the diagonal entries
of L to 1.
U(1,1)=A(1,1); U(1,2)=A(1,2); U(1,3)=A(1,3); Solves all the first row
elements of U.
L(2:3,1)=A(2:3,1)/U(1,1); Solves the 1st col.
Elements of L except L11.
(L11 = 1)
U(2,2)=A(2,2)-L(2,1)*U(1,2); Solves the 2nd row elements
of U except U21. (U21 = 0) U(2,3)=A(2,3)-L(2,1)*U(1,3);
L(3,2)=(A(3,2)-L(3,1)*U(1,2))/U(2,2); Solves the 2nd column
elements of L except L12
and L22. (L12 = 0, L22 = 1)
U(3,3)=A(3,3)-sum([L(3,1:2)].*[U(1:2,3)]'); Solves the 3rd row elements
of U except U31 and U32.
(U31 = 0, U32 = 0)
Y=inv(L)*b; Solves for Y from LY=b
X=inv(U)*Y; Solves for X from UX=Y
disp('L = ') Displays L =
disp(L) Displays the elements of L
disp('U = ') Displays U =
disp(U) Displays the elements of U
disp('X = ') Displays X =
disp(X)

回答(1 个)

Arif Hoq
Arif Hoq 2023-2-12
you defined your A matrix with at least 3 columns. So you need to specify your A matrix elements >= 3. then generate your matrix A.
format short g
A=input('Enter the elements of the coefficient matrix in array:');
A=randi(100,A,A);
% Asks the user to enter the elements of A
b=input('Enter the elements of the constant vector:');
% Asks the user to enter the elements of b
L(1,1)=1;
L(2,2)=1;
L(3,3)=1;
%Sets the diagonal entries of L to 1.
U(1,1)=A(1,1);
U(1,2)=A(1,2);
U(1,3)=A(1,3);
%Solves all the first row elements of U.
L(2:3,1)=A(2:3,1)/U(1,1);
%Solves the 1st col. Elements of L except L11.(L11 = 1)
U(2,2)=A(2,2)-L(2,1)*U(1,2);
% Solves the 2nd row elements of U except U21. (U21 = 0) U(2,3)=A(2,3)-L(2,1)*U(1,3);
L(3,2)=(A(3,2)-L(3,1)*U(1,2))/U(2,2);
% Solves the 2nd column elements of L except L12 and L22. (L12 = 0, L22 = 1)
U(3,3)=A(3,3)-sum((L(3,1:2)).*(U(1:2,3))');
% Solves the 3rd row elements of U except U31 and U32.(U31 = 0, U32 = 0)
Y=inv(L)*b; %Solves for Y from LY=b
X=inv(U)*Y; %Solves for X from UX=Y
disp('L = ') %Displays L =
disp(L) %Displays the elements of L
disp('U = ') %Displays U =
disp(U) %Displays the elements of U
disp('X = ') %Displays X =
disp(X)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by