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

3 views (last 30 days)
The function seems to be looping twice. I cannot see how the code allows this.
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:');
r2=rank([A b]);
if r1==r2 && r1==n && r2==n
if m>n
display('Overdetermined set of equations. Least-squares fit.');
solution= 'Unique'
logical= 'Approximate'
elseif m==n
solution= 'Unique'
logical= 'Exact'
elseif r1==r2 && r1<n && r2<n
solution= 'A solution'
logical= 'Exact'
elseif r1~=r2
x= 'No solution'
solution= 'nil'
logical= 'nil'
dpb on 23 Mar 2015
I had to format the code and admit I then didn't actually read it...hadn't thought it so simple a question... :)

Sign in to comment.

Accepted Answer

Ken Atwell
Ken Atwell on 22 Mar 2015
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 Comment
craig on 23 Mar 2015
Yes thanks. It hit me as soon as I read the previous comment. Can't believe I did that. Thank you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!