How can I fix this problem?

4 次查看(过去 30 天)
fs
fs 2017-1-25
回答: Jan 2017-1-25
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 个评论
Stephen23
Stephen23 2017-1-25
It works for me:
>> LUNEW(1,2)
ans =
0
Guillaume
Guillaume 2017-1-25
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.

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-1-25
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.

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by