Search code examples
wolfram-mathematica

Solve function not solving simultaneous equations


How do I solve a problem with 3 simultaneous equations? My code shown below is not giving the correct results.

I am attempting to find the maximum area (A) whilst both lengths x and y follow the following property: 2x + y = 960.

I have already looked at the documentation, and it seems that the format of my arguments is correct.

Solve[{2 x + y == 960, A == x*y, D[A] == 0}, {x, y}]

I am unsure of this, however it might be too complex for the Solve function to work, as it is getting the derivative of one of the variables (D[A]).

However I am able to do this question by hand:

  1. Rearrange 1st equation so that y = 960 - 2x

  2. Substitute y into 2nd equation so that A = x(960 - 2x) = 2x^2 + 960x

  3. Get the derivative: 4x + 960 and solve for 4x + 960 = 0

  4. x = 240

  5. Substitute x = 240 into y = 960 - 2x

  6. y = 960 - 2(240) = 960 - 480 = 480

  7. Therefore dimensions are 240 x 480.

I expect the output to be {240, 480}. Thanks :)

EDIT: Here is what I have typed into mathematica:

Clear[x, y, A]

Solve[{2 x + y == 960, A == x*y, D[A, x] == 0}, {x, y}]

OUT: {{x -> 1/2 (480 - Sqrt[2] Sqrt[115200 - A]), 
  y -> 480 + Sqrt[2] Sqrt[115200 - A]}, {x -> 
   1/2 (480 + Sqrt[2] Sqrt[115200 - A]), 
  y -> 480 - Sqrt[2] Sqrt[115200 - A]}

NMaximize[{x*y, 2 x + y == 960}, {x, y}]

OUT: {115200., {x -> 240., y -> 480.}}

Solution

  • Try this

    NMaximize[{x*y,2x+y==960},{x,y}]
    

    which is maximizing the area with your constraint expression and that instantly returns x->240, y->480

    The difficulty you were having was with the use of D[A] when Mathematica needs to know what variable you are differentiating with respect to.

    Perhaps something in this will help you understand what is happening with your derivative.

    EDIT

    Look at what Solve is going to be given:

    Clear[x,y,A];
    A == x*y;
    D[A, x]
    

    which gives 0. Why is that? You are taking the derivative of A with respect to x, but A has never been assigned any value, you have only declared that A and x*y are equal. Thus

    Clear[x,y,A];
    {2 x + y == 960, A == x*y, D[A, x] == 0}
    

    is handing

    {2*x + y == 960, A == x*y, True}
    

    to Solve and that is perhaps less puzzling when Solve returns something with A in it.

    When some function in Mathematica isn't giving you the result that you expect or that makes sense then checking exactly what is being given to that function as arguments is always a good first step.

    There are always several ways of doing anything in Mathematica and some of those seem to make no sense at all