How to find sign of a function

8 vues (au cours des 30 derniers jours)
Tina
Tina le 4 Fév 2013
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!)

Réponses (2)

Kye Taylor
Kye Taylor le 4 Fév 2013
Modifié(e) : Kye Taylor le 4 Fév 2013
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 commentaire
Tina
Tina le 4 Fév 2013
Thanks I am gonna try this right now

Connectez-vous pour commenter.


Walter Roberson
Walter Roberson le 4 Fév 2013
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 commentaires
Tina
Tina le 4 Fév 2013
my function is not a polynomial :(
Walter Roberson
Walter Roberson le 4 Fév 2013
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()

Connectez-vous pour commenter.

Catégories

En savoir plus sur Descriptive Statistics dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by