Message Boards Message Boards

0
|
1127 Views
|
1 Reply
|
0 Total Likes
View groups...
Share
Share this post:

Using components of an Output as arguments of further calculation

Posted 1 year ago

In response of

Solve[x + y == 3 && x - y == -1, {x, y}];

I have found the output in the form

Out[1]:  {{x -> 1, y -> 2}}.

Now I have given a function

f[x_, y_] = x^2 + y^2 + 7

and want to find its value at the response point (x,y)=(1,2), how can one manage it without copying the values of x and y
The way
f[Out[1]] is not working ...

POSTED BY: Ghulam Farid

The easiest way:

Solve[x + y == 3 && x - y == -1, {x, y}]
x^2 + y^2 + 7 /. %

Another way:

sol = Solve[x + y == 3 && x - y == -1, {x, y}]
f[x_, y_] := x^2 + y^2 + 7
f[x, y] /. sol[[1]]

In general, Solve gives the solutions in the form of replacement rules, that can be used with the /. mechanism. Haave a look at the documentation for Solve in the Scope > Basic Uses section.

With SolveValues there are no replacement rules and the procedure is different:

sol2 = First@SolveValues[x + y == 3 && x - y == -1, {x, y}]
f[x_, y_] := x^2 + y^2 + 7
f @@ sol2
POSTED BY: Gianluca Gorni
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