vpa function did not convert sym results as intended

1 次查看(过去 30 天)
I have a function that applied gaussian elimination to a matrix and is suppose to eliminate values from a few rows and columns. A few days ago, the code was working fine and I got a bunch of complex elements as the output of the user defined function below. Today, the output is just a sym format and not double complex. I cannot figure out what the problem is.
I have attached the csv file of the ybus matrix.
function yreduced=gauss_elimination(ybus,yb,Nb)
indexyb=ybus(1,:);
MM=ybus(2:end,2:end); % Augmented Matrix
MM=sym(MM);
%%%%%%%%%%%%%%%% Gauss elimination method %%%%%%%%%%%%%%
disp('Gauss elimination method:');
[m]=size(MM);
y1=size(Nb);
y2=sort(Nb,'descend')
for j=1:y1
for k=1:m
if k==y2(j)
for z=k-3:m
if MM(k,k)==0
disp("replace row"+k + "with row" +z)
tmp=MM(k,:); MM(k,:)=MM(z,:);
MM(z,:)=tmp;
end
end
% end
i=k-3;
ss=1;
while ss<=3
disp("Eliminate ybus("+i+","+k+")")
MM(i,:)=MM(i,:)-MM(k,:)*(MM(i,k)/MM(k,k))
% i=i-3;
ss=ss+3;
end
end
end
end
% yreduced=M;
yreduced=vpa(MM,6);
  2 个评论
Torsten
Torsten 2022-5-30
Why do you define MM as sym ?
MM is a numerical matrix from the .csv file. You will not gain higher precision this way.
Leonie Bule
Leonie Bule 2022-5-30
编辑:Leonie Bule 2022-5-30
I have a live script so I declared MM as sym for algebraic visualization. I have noticed a problem with the for loop
for z=k-3:m
if MM(k,k)==0
disp("replace row"+k + "with row" +z)
tmp=MM(k,:); MM(k,:)=MM(z,:);
MM(z,:)=tmp;
end
end
It doesn't replace the MM(k,k) k rows with non-zero rows. Maybe the sym and vpa function fail when the elements are not double precision?

请先登录,再进行评论。

回答(1 个)

SAI SRUJAN
SAI SRUJAN 2023-10-27
Hi Leonie Bule,
I understand that you are facing an issue regarding the implementation of gauss elimination method.
You can follow the given example to proceed further.
a = [3 4 -2 2
4 9 -3 5
-2 -3 7 6
1 4 6 7 ];
a=sym(a);
[m,n]=size(a);
for j=1:m-1
for z=2:m
if a(j,j)==0
t=a(j,:);
a(j,:)=a(z,:);
a(z,:)=t;
end
end
for i=j+1:m
a(i,:)=a(i,:)-a(j,:)*(a(i,j)/a(j,j));
end
end
vpa(a,6);
You can refer to the following documentation to understand more about "vpa" MATLAB function.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by