Thomas C.
Thomas C.

Reputation: 45

Return multiple solutions GNU Octave or Matlab

I have a function that solves for a variable in an equation. There should be 2 solutions to the equation. For example, 9=x^2, x can be 3 or -3. How can I get both values to be returned? Right now it only returns the first answer, 3.

Upvotes: 0

Views: 274

Answers (1)

jodag
jodag

Reputation: 22274

You can modify your function to return an array of values, for example

function x = solve_square(y)
    % Returns the solutions to y=x^2
    x = [sqrt(y), -sqrt(y)];
end

Usage would be

>> x = solve_square(9)
x =
     3   -3

Upvotes: 1

Related Questions