フィルターのクリア

Info

この質問は閉じられています。 編集または回答するには再度開いてください。

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

1 回表示 (過去 30 日間)
Adomas Bazinys
Adomas Bazinys 2018 年 3 月 25 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
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!

回答 (1 件)

Steven Lord
Steven Lord 2018 年 3 月 26 日
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.

この質問は閉じられています。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by