Gass Elimination no pivot
32 views (last 30 days)
Show older comments
%Write a MATLAB function to perform Gauss elimination (no pivoting). The function declaration should be
B=[A,y];
[n,m]=size(B);
%Start with Foward sub
for i=1:n
B(i,:)=B(i,:)./B(i,:);
for k=i+1:n
B(k,:)=(-B(i,:)*B(k,i))+B(k,:);
end
end
%Back Substitution
for j=n-1:-1:1
for z=j+1:1:n
B(j,:)=(-B(z,:)*B(j,z))+B(j,:);
end
end
So my problem is I was given this code and was asked to "Write a MATLAB function to perform Gauss elimination (no pivoting). The function declaration should be function x = gausselim(A,y)". Then it asked to submit the code and the results. My problem is I do not understand how I am suposted to display results from just that. Thank you for your help
0 Comments
Answers (1)
KSSV
on 3 Mar 2020
function x = GaussElim(A, b)
% Solve linear system Ax = b
% using Gaussian elimination without pivoting
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:n-1
m = -A(i+1:n,i)/A(i,i); % multipliers
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
Call:
A = rand(3) ;
b = rand(3,1) ;
x = GaussElim(A,b)
0 Comments
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!