1
$\begingroup$

This code works:

x=2
y/.Solve[y==x^2]
Out[138]= 2
Out[139]= {4}

So above I put number in variable x and getting the result of corresponding y

Now I want to give list with several values in list in variable x and get evaluated values of corresponding y's. How can I do that?

I tried this but it does not works:

x={1,2,3,4}
y/.Solve[y==x^2]
Out[140]= {1,2,3,4}
Out[141]= y

Expected output is:

{1,4,9,16}
$\endgroup$
5
  • $\begingroup$ Solve needs the variable you're solving for in the second argument, and you want to solve for the array - Array[y, 4]/.First[Solve[Array[y, 4] == x^2, Array[y, 4]]]. But obviously you can just do {1,2,3,4}^2 $\endgroup$
    – flinty
    Commented Jul 13, 2020 at 0:16
  • $\begingroup$ @flinty This does not works: x = {1, 2, 3, 4} y /. Solve[Array[y, 4] == x^2, Array[y, 4]]. How can I fix it? $\endgroup$
    – vasili111
    Commented Jul 13, 2020 at 0:22
  • $\begingroup$ What doesn't work? It works perfectly fine for me - you need a semicolon after your first list. $\endgroup$
    – flinty
    Commented Jul 13, 2020 at 0:25
  • $\begingroup$ @flinty You are right. Thank you very much. My mistake! $\endgroup$
    – vasili111
    Commented Jul 13, 2020 at 0:25
  • $\begingroup$ You also need Array[y, 4]/. not y/. $\endgroup$
    – flinty
    Commented Jul 13, 2020 at 0:26

2 Answers 2

2
$\begingroup$

Maybe you can use the following codes:

x = {1, 2, 3, 4};Solve[y == z^2] /. z -> x

The final result is:

{{y -> {1, 4, 9, 16}}}
$\endgroup$
1
$\begingroup$

Why not bypass Solve, and apply the square directly:

x={1, 2, 3, 4};

y = x^2

{1, 4, 9, 16}

More complicated operations:

y= somethingComplicated[#]&/@x
$\endgroup$
2
  • $\begingroup$ Thank you. I need a way to use x={1, 2, 3, 4} in y/.Solve[y==x^2] . $\endgroup$
    – vasili111
    Commented Jul 13, 2020 at 0:23
  • 1
    $\begingroup$ If bypassing Solve, you only need x = {1, 2, 3, 4}; y = x^2 since Power is Listable $\endgroup$
    – Bob Hanlon
    Commented Jul 13, 2020 at 0:30

Your Answer

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

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