How can I fix this problem?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I'm trying to run my script, but I do not get my result. It seems to my Matlab does not execute my code. How can I fix this problem?
function x=LUNEW(A,b)
%First of all, I will combine the matrix A with the matrix b in this way
%[A|b]
% Gauss Elimination
Ab = [A,b];
n = length(A);
% the size of matrix A.
%L=eye(n);
%[A,b]=pivot_element(A,b);
%back substitution
x=zeros(n,1);
% for i=10:-1:1
% x(i)=(Ab(i,end)-Ab(i,i+1:n)*x(i+1:n))/Ab(i,i);
% end
end
%A(1,1) as pivot element
function [A,b]=pivot_element(A,b)
for i=2:n
alpha= Ab(i,1)/Ab(1,1);
L(i,1)=alpha;
Ab(i,:)= Ab(i,:)- alpha*Ab(1,:); % it is like (R2=R2-
alpha*R1)replacement.
end
% % A(2,2) as piovt element
for i=3:n
alpha= Ab(i,2)/Ab(2,2);
L(i,2)=alpha;
Ab(i,:)= Ab(i,:)- alpha*Ab(2,:); %interchange the rows
end
% % A(3,3) as piovt element
for i=4:n
alpha= Ab(i,3)/Ab(3,3);
L(i,3)=alpha;
Ab(i,:)= Ab(i,:)- alpha*Ab(3,:);
end
% % A(4,4) as piovt element
for a=5:n
alpha= Ab(a,4)/Ab(4,4);
L(a,4)=alpha;
Ab(a,:)= Ab(a,:)- alpha*Ab(4,:);
end
% % A(5,5) as piovt element
for d=6:n
alpha= Ab(d,5)/Ab(5,5);
L(d,5)=alpha;
Ab(d,:)= Ab(d,:)- alpha*Ab(5,:);
end
% % A(6,6) as piovt element
for f=7:n
alpha= Ab(f,6)/Ab(6,6);
L(f,6)=alpha;
Ab(f,:)= Ab(f,:)- alpha*Ab(6,:);
end
% % A(7,7) as piovt element
for h=8:n
alpha= Ab(h,7)/Ab(7,7);
L(h,7)=alpha;
Ab(h,:)= Ab(h,:)- alpha*Ab(7,:);
end
% % A(8,8) as piovt element
for j=9:n
alpha= Ab(j,8)/Ab(8,8);
L(j,8)=alpha;
Ab(j,:)= Ab(j,:)- alpha*Ab(8,:);
end
% % A(9,9) as piovt elements
for k=10:n
alpha= Ab(k,9)/Ab(9,9);
Ab(k,:)= Ab(k,:)- alpha*Ab(9,:);
L(k,9)=alpha;
end
% % A(9,9) as piovt element
% for k=9;
% alpha= Ab(k,9)/Ab(9,9);
% Ab(k,:)= Ab(k,:)- alpha*Ab(9,:);
% L(k,9)=alpha;
% end
%U=Ab(1:n,1:n);
%
%L*U;
%%%%%%%%%%%%%%%%%%%%
%back substitution
x=zeros(n,1);
for i=10:-1:1
x(i)=(Ab(i,end)-Ab(i,i+1:n)*x(i+1:n))/Ab(i,i);
end
%
%%%%%%%%%%%%%%%%%%%%
%compare with backslash
%A\b;
end
4 Kommentare
Guillaume
am 25 Jan. 2017
As it is the main function (there's no script in the code you've presented) only does one useful thing: create a zero row vector the same length as the largest dimension of the input A. That is all.
Now if some of the lines were to be uncommented, it could do a lot more but don't expect us to guess which lines that should be.
Antworten (1)
Jan
am 25 Jan. 2017
The code, which is executed is:
function x = LUNEW(A, b)
Ab = [A,b];
n = length(A);
x = zeros(n,1);
end
As Guillaume has mentioned already, all other lines are not executed, because they are commented.
You can check this by using the debugger: https://www.mathworks.com/help/matlab/debugging-code.html. Set a breakpoint in the first line of code (the red dots on the left side in the editor) and step through the code line by line. Then it gets clear, where what is done.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!