Using Solver to solve equations with unknown x, how can i get all the answers sorted out.

1 view (last 30 days)
Trying to make a solver for one uknown x, but im having some issues with getting all the answers sorted out without having to write and "if" and "elseif" for every number of solutions. Is there some function that can do this for me?
syms x % z = input x = solve(z) xx = double(x)
% num2str(length(svar))
disp(['There is ',num2str(length(x)),' solutions'])
if length(xx)==1;
x = xx(1)
elseif length(xx)==2;
x = xx(1);
x = xx(2);
end

Accepted Answer

Star Strider
Star Strider on 2 Oct 2014
You can eliminate the ‘if...else’ block and simply write:
x = xx
  5 Comments
Star Strider
Star Strider on 2 Oct 2014
My pleasure!
I strongly suggest keeping ‘xx’ as its default vector. You can easily access its elements in a loop if you need to. If you want to print them out as ‘x1’, ‘x2’, etc., use this sort of loop:
for k1 = 1:size(xx,1)
fprintf(1,'\n\tx%d = %f\n', k1, xx(k1))
end

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!