Info

This question is closed. Reopen it to edit or answer.

Change code so that it would generate numbers instead of typing them by user(Gauss method)

1 view (last 30 days)
function C = Gauss1(A,B)
A = [ 8 2 5; 1 5 6; 4 5 6]
B = [-1; 4; 5]
i = 1;
X = [ A B ];
[ nX mX ] = size( X);
tic
while i <= nX
if X(i,i) == 0
disp('Diagonal elements are zero')
return
end
X = elimination(X,i,i);
i = i +1;
end
C = X(:,mX);
function X = elimination(X,i,j)
[ nX mX ] = size( X);
a = X(i,j);
X(i,:) = X(i,:)/a;
for k = 1:nX
if k == i
continue
end
X(k,:) = X(k,:) - X(i,:)*X(k,j);
end
toc
This is a code(Gauss method) that solves AX=B equation. I wrote this code but now I need to change this code so that program would automatically generate numbers for A(N x N size matrice) and for B(N x 1 size matrice). I have not any ideas yet. Please show me how could I solve it if you can!

Answers (1)

Steven Lord
Steven Lord on 26 Mar 2018
function C = Gauss1(A,B)
A = [ 8 2 5; 1 5 6; 4 5 6]
B = [-1; 4; 5]
i = 1;
% etc.
Your function accepts A and B as input arguments, then promptly throws the matrices the user passed into your function as input into the trash and uses these hard-coded matrices instead. Get rid of the lines where you define A and B and let the user enter them.
Alternately, turn those lines into help text for your function, to show users of your code how to call it.
function C = Gauss1(A,B)
% Solve a system of equations. Example:
%
% A = [ 8 2 5; 1 5 6; 4 5 6]
% B = [-1; 4; 5]
% C = Gauss1(A, B)
i = 1;
% etc.
Now when someone says help Gauss1 they'll see this example that they can execute as a test of your function.

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!