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
  댓글 수: 1
Tina
Tina 2013년 2월 4일
Thanks I am gonna try this right now

댓글을 달려면 로그인하십시오.


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