フィルターのクリア

How can I stop a user defined function from returning the output twice?

1 回表示 (過去 30 日間)
craig
craig 2015 年 3 月 22 日
コメント済み: dpb 2015 年 3 月 23 日
The function seems to be looping twice. I cannot see how the code allows this.
Code:
function [ x,solution,logical ] = sim_eq( A,b )
%SIM_EQ solves a system of simultaneous equations.
% Calculate rank and solve
n=input('Input number of unknowns:');
m=input('Input number of equations:');
r1=rank(A);
r2=rank([A b]);
if r1==r2 && r1==n && r2==n
if m>n
display('Overdetermined set of equations. Least-squares fit.');
x=A\b
solution= 'Unique'
logical= 'Approximate'
elseif m==n
x=A\b
solution= 'Unique'
logical= 'Exact'
end
elseif r1==r2 && r1<n && r2<n
x=pinv(A)*b
solution= 'A solution'
logical= 'Exact'
elseif r1~=r2
x= 'No solution'
solution= 'nil'
logical= 'nil'
end
end
  3 件のコメント
craig
craig 2015 年 3 月 23 日
Yeah thanks. I read your first statement and within 2 seconds it dawned on me. I thought I wouldn't put semi-colons so it would display. But it displays automatically. duh. I'm tired. Thanks!!
dpb
dpb 2015 年 3 月 23 日
I had to format the code and admit I then didn't actually read it...hadn't thought it so simple a question... :)

サインインしてコメントする。

採用された回答

Ken Atwell
Ken Atwell 2015 年 3 月 22 日
You don't have semicolons at the end of some lines (so output of those lines will be displayed). Maybe it is running once, but the lack of semicolons is making you think it is running twice?
  1 件のコメント
craig
craig 2015 年 3 月 23 日
Yes thanks. It hit me as soon as I read the previous comment. Can't believe I did that. Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MuPAD についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by