Message Boards Message Boards

0
|
2997 Views
|
12 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to automatically assign two values to two variables x1 and x2 ?

Posted 1 year ago

Solving the equation yields two:

Solve[x^2 + a x + 1 == 0, x]

{{x -> 1/2 (-a - Sqrt[-4 + a^2])}, {x -> 1/2 (-a + Sqrt[-4 + a^2])}}

How to automatically assign two values to two variables x1 and x2 after solving the equation

It is convenient to directly use variables x1 and x2 for other operations in subsequent calculations.

POSTED BY: Lee Tao
12 Replies
Posted 1 year ago
Clear["`*"]

eqns1 = {x^2/2^2 + y^2/1^2 == 1, y == x + 1};    

SolveValues[eqns1, {x, y}]

{{x1, y1}, {x2, y2}} = Sort[%, #1[[1]] < #2[[1]] &]
POSTED BY: Lee Tao
Posted 1 year ago

Or

sol = SolveValues[eqns1, {x, y}]
{{x1, y1}, {x2, y2}} = SortBy[Last]
POSTED BY: Updating Name
Posted 1 year ago

enter image description here

there is something wrong with the result

POSTED BY: Lee Tao

Oops. I meant to write

{{x1, y1}, {x2, y2}} = sol // SortBy[Last]
POSTED BY: Rohit Namjoshi
Posted 1 year ago
ClearAll[Evaluate[Context[] <> "*"]]

eqns1 = {x^2/2^2 + y^2/1^2 == 1, y == x + 1};   

SolveValues[eqns1, {x, y}]

{{x1, y1}, {x2, y2}} = Sort[%, #1[[2]] < #2[[2]] &]
POSTED BY: Lee Tao
Posted 1 year ago

There is also such a problem: if the equation has two solutions, compare the size of the two, assign the small root to y1, and assign the big root to y2, how to operate it?

as this eqns:

ClearAll[Evaluate[Context[] <> "*"]]

eqns1 = {x^2 - y == 0, y == x + 1};

{{x1, y1}, {x2, y2}} = SolveValues[eqns1, {x, y}]
POSTED BY: Lee Tao
Posted 1 year ago

Further question: I want to assign a root greater than 0 to y1, and write the following code. Why is the result not what you want, and the calculated value of y1 less than 0? Why? How to rewrite the code?

ClearAll[Evaluate[Context[] <> "*"]]

eqns1 = {x^2/2^2 + y^2/1^2 == 1, y == x + 1};  

{{x1, y1}, {x2, y2}} = 
 SolveValues[eqns1, {x, y}, Assumptions -> y1 > 0] // FullSimplify

the result is

{{-(8/5), -(3/5)}, {0, 1}}

enter image description here

POSTED BY: Lee Tao

Hi Lee

Assumptions -> y1 > 0 does nothing since y1 does not appear anywhere in the equations to be solved.

Is this what you are looking for?

{{x1, y1}} = SolveValues[eqns1, {x, y}, Assumptions -> y > 0]
y1
(* 1 *)
POSTED BY: Rohit Namjoshi
Posted 1 year ago

I want to write all solution sets, but assign the value of y greater than 0 to y1, and other solutions will also be displayed.

eqns1 = {x^2/2^2 + y^2/1^2 == 1, y == x + 1};

{{x1, y1}} = SolveValues[eqns1, {x, y}, Assumptions -> y > 0]

{{x2, y2}} = SolveValues[eqns1, {x, y}, Assumptions -> y < 0]

This can solve the problem, but the code is awkward. Can you optimize it?

POSTED BY: Lee Tao
Posted 1 year ago

You could skip a step by using SolveValues:

{x1, x2} = SolveValues[x^2 + a x + 1 == 0, x]
POSTED BY: Eric Rimbey
Posted 1 year ago

the fuction solvevalues is in the new version of mathematica

POSTED BY: Lee Tao

Use ReplaceAll

sol = Solve[x^2 + a x + 1 == 0, x]
{x1, x2} = x /. sol

x1
(* 1/2 (-a - Sqrt[-4 + a^2]) *)

x2
(* 1/2 (-a + Sqrt[-4 + a^2]) *)
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract