I am trying to solve a polynomial equation f(x) using NSolve and in the equation there is a known parameter a. I want to have only the pure real roots lying in the range 0<=x<=1. These is the code which I have tried

x /. NSolve[f[x_] == 0 && 0 <= x <= 1, x, Reals]

When I am calculating the value of x for parameter {a1,a2}, I am getting

{{0.950681}, x}

Then I found that the possible real roots (without the constraints) are

x /. NSolve[f[x_] == 0, x, Reals]

{{-3296.29, -0.0708381, -0.0000599572, 0.950681}, {-3296.29, 1.01867}}

This means that when I am taking the parameter a2 there are no roots that lie between the interval 0<=x<=1.

Is the constraint 0<=x<=1 which I have written right ?

So if there are no roots then why its showing x as the answer ?

Last, if there is no way possible then how to simply select the solutions that lie in the range from the list

{{-3296.29, -0.0708381, -0.0000599572, 0.950681}, {-3296.29, 1.01867}} 
  • $\begingroup$ Your first line gives me a couple of errors. Probably want f[x] or f[x, a] instead of f[x_], since patterns such as x_ are used for function definitions and pattern matching, but not in equations. Also with no definition of f, it's hard to say exactly what the problem would be. Since you're solve for x, trying to replace y should result in a list of one or more y. Since you're getting a list with a number, we'll need to see the code that generates the output you show. $\endgroup$
    – Michael E2
    Commented Sep 24, 2018 at 10:39
  • $\begingroup$ Here's an example that fits the code and description presented in the first line of code: y /. NSolve[(4 x^2 - 1) (9 x^2 - 1) == 0 && 0 <= x <= 1, x] $\endgroup$
    – Michael E2
    Commented Sep 24, 2018 at 10:41
  • $\begingroup$ @MichaelE2 I made an error while typing the question. Now I have corrected it. $\endgroup$
    – T S Singh
    Commented Sep 24, 2018 at 10:58
  • $\begingroup$ @MichaelE2 I hope you understand what I am trying to say. x /. NSolve[a+f[x_]== 0 && 0 <= x <= 1, x] (Letz say a is a parameter). If suppose at a=a1 it has a solution in the range 0 <= x <= 1, then the command line is OK, but if at a=a2 say the roots are available but not in the range 0 <= x <= 1. Then I am getting {x} as answer . $\endgroup$
    – T S Singh
    Commented Sep 24, 2018 at 11:05
  • $\begingroup$ The _ is meant for pattern matching and definitions, so you really shouldn't be putting it in your NSolve[] expression. Try NSolve[a + f[x] == 0 && 0 <= x <= 1, x]. $\endgroup$ Commented Sep 24, 2018 at 11:43


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.