How to find sign of a function

8 次查看(过去 30 天)
Tina
Tina 2013-2-4
Hello;
I have a function let's say y=x-c where c is a constant. I want to find when y is positive or negative, as c is changed. Is there a MATLAB function that can do this for me?
(Actually the function I have is a more complex one!)

回答(2 个)

Kye Taylor
Kye Taylor 2013-2-4
编辑:Kye Taylor 2013-2-4
If you can evaluate your function with a command like
y = yourFcn(x);
then you can determine the sign with a logical variable, as in the command:
yPos = y>0;
yPos will be 1 where y is positive, and 0 where y is non-positive. So you can do stuff like
figure(1),hold on
plot(x( yPos ), y( yPos ),'r.')
plot(x( ~yPos ), y( ~yPos ),'b.')
legend('Positive values','Negative Values')
You may also be interested in the sign function
doc sign

Walter Roberson
Walter Roberson 2013-2-4
If you have the symbolic toolbox, and you have the function in symbolic form,
y = f(x) - c
then the zeros, y = 0, are 0 = f(x) - c, and thus f(x) = c, so solve that
syms x c
solve( f(x) - c, x)
If your f(x) is not a polynomial, or is a polynomial of degree 5 or higher, there might not be an explicit solution.
  2 个评论
Tina
Tina 2013-2-4
my function is not a polynomial :(
Walter Roberson
Walter Roberson 2013-2-4
solve() works with some non-polynomials.
Is your function such that you can constrain the maximum number of roots? For example with some trig functions roots can appear in strange places and it can be hard to guess how many you will have.
Systems that have roots that are very close together can be tricky to deal with.
The symbolic toolbox has a routine named numeric::solve that has an option to request that "all" roots be found numerically. The documentation talks about the limitations on finding the roots. numeric::solve has no direct MATLAB interface but can be accessed using feval() or evalin()

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by