0
$\begingroup$

How can get roots only saved in the list?

Input

g[x_] := x^2 + x - 6;
g /@ Table[i, {i, -10, 10}]
If[g[x] == 0, i, Nothing]

Output

{-3,2}

How can I achieved that kind of output?

$\endgroup$
1
$\begingroup$
xs = Table[i, {i, -10, 10}];
Pick[xs, g /@ xs, 0]
(* {-3, 2} *)
|improve this answer|||||
$\endgroup$
1
$\begingroup$
 g[x_] := x^2 + x - 6;

Table[If[g[i] == 0, i, Nothing], {i, -10, 10}]

{-3, 2}

Or

 val = Table[i, {i, -10, 10}];
    Pick[val, Thread[g@val == 0]]

{-3, 2}

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Can you solve this with function If Or Nothing? Where did I get wrong? Can you point out the mistakes I made here? I would appreciate it if you could do these for me. $\endgroup$ – kile Oct 26 '19 at 16:24
  • $\begingroup$ @kile Try If[g[#]==0,#,Nothing]&/@Table[i,{i,-10,10}] and try testx[x_]:=If[g[x]==0,x,Nothing];testx/@Table[i,{i,-10,10}] Search and study the help system documentation until you can figure these out so you will be able to use these ideas next time. $\endgroup$ – Bill Oct 26 '19 at 17:20
  • $\begingroup$ testx[x_]:=If[g[x]==0,x,Nothing];testx/@Table[i,{i,-10,10}] It failed to give the right answer. $\endgroup$ – kile Oct 27 '19 at 11:40
1
$\begingroup$

An alternative approach would be to use Solve:

sol = Solve[g[x] == 0, x];
x /. sol
{-3, 2}
|improve this answer|||||
$\endgroup$
  • $\begingroup$ This is not what I want. Just try each number from -10 to 10. $\endgroup$ – kile Oct 27 '19 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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