简体   繁体   English

返回多个解决方案GNU Octave或Matlab

[英]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. 该方程应该有2个解。 For example, 9=x^2, x can be 3 or -3. 例如,9 = x ^ 2,x可以是3或-3。 How can I get both values to be returned? 如何获得两个值都返回? Right now it only returns the first answer, 3. 现在,它仅返回第一个答案3。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM