0
votes

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.

1
Please post your code. It will help us answer your question. We don't know what methods you are using to solve the equation. - Cris Luengo
are you using a root finding method like newtons method? - user1911226

1 Answers

1
votes

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