Message Boards Message Boards

2 Replies
0 Total Likes
View groups...
Share this post:

what am i missing to make this work?

Anonymous User
Anonymous User
Posted 9 years ago

hi everybody! Need your answer which helps me understand why this don't work? solve = NSolve[{x - y == 1, 2 x + y == 1}, {x, y}]; x = Extract[solve, {1}]; y = Extract[solve, {2}]; x + y

POSTED BY: Anonymous User
2 Replies


In your code you were extracting the solution to solve, which turns out to be a row vector with dimensions 1,2:

solve = NSolve[{x - y == 1, 2 x + y == 1}];

Your extract function was trying to pull the second row of solve, which doesn't exist. I utilize the two bracket position notation instead of the Extract function so I can keep things straight.

Second, don't assign a value to a variable that you are trying to keep a variable. Mathematica's Solve function outputs a list of replacement rules and doesn't assign a value to the variables assigned in the Solve function. This way, those variables remain variables and the output of your Solve function won't change in the future, even if you rerun it multiple times.

POSTED BY: Robbie Haffner
Posted 9 years ago


In[1]:= solve = NSolve[{x - y == 1, 2 x + y == 1}, {x, y}]

Out[1]= {{x -> 0.666667, y -> -0.333333}}

In[2]:= Extract[solve, {1}]

Out[2]= {x -> 0.666667, y -> -0.333333}

In[3]:= Extract[solve, {2}]

During evaluation of In[3]:= Extract::partw: Part 2 of {{x->0.666667,y->-0.333333}} does not exist. >>

Out[3]= Extract[{{x -> 0.666667, y -> -0.333333}}, {2}]

In[4]:= x + y /. First[solve]

Out[4]= 0.333333
POSTED BY: Bill Simpson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract