How do I modify this to efficiently solve several systems of equations using gaussian elimination?
2 次查看(过去 30 天)
显示 更早的评论
I created a script (with code I found in a textbook) to perform gaussian elimination on a system of equations (the system is of two equations and two unknowns). It works fine. But here's the question:
How do I modify the script to efficiently solve a set of 8 seperate systems of equations?
The 8 separate systems needing to be solved come from modifying the value of R in the first of the two equations below from 0.6 to 0.95 in increments of 0.05:
R(0.5x+1.5y)-2.531662198y+0.7=0 and -21.752x+0.5y+37.996=0
I'm a very inexperienced matlab user but I'm sure there's some kind of loop I can incorporate into the code to run the gaussian elimination on all 8 systems one after the other (while storing each set of solutions found) without having to copy and paste the code over and over.
Separate from this is the issue of how to use matlab to determine the equation that varies with R as R changes in the specified increments. I determined this using:
R=[.6:.05:.95] R*1.5-2.531662198
And then manually typed out each of the 8 equations with the varying component. I'm sure there's a better way to do this.
Anyway, here's the code I'm using for the elimination in case that's useful here:
function x = Gaussianpivoting(A,b)
%A is coefficient matrix
%b is the constants vector
Aug=[A b];
[n,ncol]=size(Aug);
x=zeros(n,1); %initializing column vector x
%Elimination with pivoting
for k=1:n-1
[maxele,l]=max(abs(Aug(k:n,k)));
%1 is the location of max value between k and n
p=k+1-1; %row to be interchanged with pivot row
if (p~=k)
Aug ([kp],:)=Aug([pk],:);%array index used for efficient swap
end
pivot=Aug(k,k);
for row=k+1:n
m=Aug(row,k)/pivot;
Aug(row,k+1:ncol)=Aug(row,k+1:ncol)-m*Aug(k,k+1:ncol);
end
end
%backward substitution steps
x(n)=Aug(n,ncol)/Aug(n,n);
for k=n-1:-1:1
x(k)=(Aug(k,ncol)-Aug(k,k+1:n)*x(k+1:n))/Aug(k,k);
%note inner product
end
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!