1
$\begingroup$

Can anyone explain to me how can I use the roots of an equation? Let clarify my question with one example. Suppose that I have an equation which I want to find its root:

x^3=0.01 x

Now, I solve the equation in order to find the roots.

NSolve[x^3==0.01x, x]

We know that the answer is a list. I used the command 'Part' to pick the first root.

Part[NSolve[x^3==0.01x, x],1]

Now, I want to put this root in another function such as:

f[x_]:=x^2

This is the result of my code:

f[Part[NSolve[x^3==0.01x, x],1]]
{(x -> -0.1)^2}

As you see the answer is not a number. But, I need a number. How can I use the roots of equations in any other calculations?

Thanks a lot.


I used the code, and it pretty works well. But, unfortunately, it doesn't work for the following case:

v = NSolve[1 == E^(-(x/2))/x, x, Reals];
f[q_] := q;
f[q] /. v[[1]]

The output is:

{q}

It expect to get a number. What is happening?

$\endgroup$

closed as off-topic by Bob Hanlon, m_goldberg, Daniel Lichtblau, garej, MarcoB Jul 1 at 16:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, m_goldberg, Daniel Lichtblau, garej, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ If you really want to just get the first solution, without using substitution rules, you can get it with NSolve[x^3 == 0.01 x, x][[1, 1, 2]]. $\endgroup$ – MelaGo Jun 27 at 1:53
  • $\begingroup$ Mehrdad, about your additional question, see edit in my answer. $\endgroup$ – Kagaratsch Jun 27 at 13:08
3
$\begingroup$

The Solve routine gives you a substitution rule

solution = NSolve[x^3==0.01x, x]

{{x -> -0.1}, {x -> 0.}, {x -> 0.1}}

which you can substitute into other expressions like

f[x_]:=x^2

by using the substitution operator /.

f[x] /. solution

{0.01, 0., 0.01}

The above gives you a list of all three solutions. If you only want the first one, you can take the first part also by doing

f[x] /. solution[[1]]

0.01

In general, you can write any substitution rules you like, e.g.:

a/.a->b

b

EDIT

In your code

v = NSolve[1 == E^(-(x/2))/x, x, Reals];
f[q_] := q;
f[q] /. v[[1]]

you are essentially trying to do a substitution like q/.x->something. So Mathematica is looking for any x it can find in the expression q and substitutes them by something. q is not x though, so naturally nothing happens. To get what you want, you can try

f[x] /. v[[1]]
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.