Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica - Working with simplification

  1. Dec 7, 2008 #1
    Hi everybody,

    do anyone know how to simplify the maximum as possible an expression in Mathematica?

    for example:

    I have the following inputs:

    F[x_, y_] = Sin[Pi*x]*Sin[Pi*y]
    FullSimplify[(TIME - F[0.125, 0.125])/
    0.25 - (1/(
    2 Pi^2))[(0 - 2*F[0.125, 0.125] + u2)/(0.125)^2 + (
    u8 - 2*F[0.125, 0.125] + 0)/(0.125)^2] == 0] // N

    then Mathematica returns the following output:

    1. TIME == 0.146447+ 0.25 0.0506606[-37.4903 + 64. u2 + 64. u8]

    the problem is that I needed Mathematica to multiply this "0.0506606" to the other terms. Actually I tried to use the Distributive[] function, but had no success.

    Any suggestions?

    Thanks in advance.
     
  2. jcsd
  3. Dec 7, 2008 #2

    alphysicist

    User Avatar
    Homework Helper

    Hi Lanot,

    I'm not really sure what you are doing, but I think what you need to do is change the following square brackets in red to parenthesis:

    F[x_, y_] = Sin[Pi*x]*Sin[Pi*y]
    FullSimplify[(TIME - F[0.125, 0.125])/
    0.25 - (1/(
    2 Pi^2))[(0 - 2*F[0.125, 0.125] + u2)/(0.125)^2 + (
    u8 - 2*F[0.125, 0.125] + 0)/(0.125)^2] == 0] // N


    so that it is:

    F[x_, y_] = Sin[Pi*x]*Sin[Pi*y]
    FullSimplify[(TIME - F[0.125, 0.125])/
    0.25 - (1/(
    2 Pi^2)) ( (0 - 2*F[0.125, 0.125] + u2)/(0.125)^2 + (
    u8 - 2*F[0.125, 0.125] + 0)/(0.125)^2 ) == 0] // N


    Is that what you were wanting?
     
  4. Dec 7, 2008 #3
    Yes, that's exactly what I'm looking for. Thank you so much.
    Strangely, I haven't found anything related in the Mathematica Documentation.

    One more question though.

    Lets say that I have several outputs from mathematica like:

    0 == 1 u1 + 2 u2 + 3 u3
    2 == 2 u1 + 2 u2 + 2 u3
    3 == 3 u1 + 1 u2 + 2 u3

    Is it possible to evaluate the set and find u1, u2 and u3 in a matrix? The only way that I know is to pick the coefficients and use the LinearSolve command...
     
  5. Dec 7, 2008 #4

    alphysicist

    User Avatar
    Homework Helper

    I don't know how you are getting these outputs, so there might be some details to work through, but here is what I would do.

    Right now you run something that works like this, right?

    input: <your mathematica expression>

    output: 0 == 1 u1 + 2 u2 + 3 u3


    Now change this to:

    input: a = <your mathematica expression>

    output: 0 == 1 u1 + 2 u2 + 3 u3

    This is different because now when you enter just a, you get:

    input: a

    output: 0 == 1 u1 + 2 u2 + 3 u3

    So now do the same for the other two outputs, putting them in variables b and c, and then use:


    Solve[{a,b,c},{u1,u2,u3}]



    (If you are generating these three outputs automatically (instead of manually running it three times) you can do the above automatically, but I don't know how you are getting those outputs.)
     
  6. Dec 7, 2008 #5
    Yes. It works. Thanks.


    And how could I do to assign a value from the output directly to some variable of the output that I'm working on? For example:

    input: Simplify[(UP9 - u9)/0.25 -
    1/(2*Pi^2) ((u8 - 2 u9 + 0)/0.25^2 + (0 - 2 u9 + u6)/0.25^2) ==
    0] // N
    (i know the values of u8, u9 and u6... I'm just evaluating for UP9)
    so the output is that:

    output: 1. UP9 == 0.381295

    I just wanted that the value 0.381295 would be assigned to UP9 instead of seeing the equality. If i change UP9 to other variable and rewrite the expression to UP9 = <expression>, then the equality would be assigned to UP9, not the value.

    Any ideas?

    Thanks
     
  7. Dec 7, 2008 #6

    alphysicist

    User Avatar
    Homework Helper

    Here is a quick way. Instead of simplify I would use solve:

    input: Simplify[(UP9 - u9)/0.25 -
    1/(2*Pi^2) ((u8 - 2 u9 + 0)/0.25^2 + (0 - 2 u9 + u6)/0.25^2) ==
    0,UP9]

    and if your other variables are already assigned this would give something like:

    output: {{UP9 [itex]\to[/itex] 0.381295}}

    The double curly brackets means this is a nested list, so to actually use this to assign a value to UP9, I would modify the above input I and actually use:

    input: UP9 /. First[ Solve[(UP9 - u9)/0.25 -
    1/(2*Pi^2) ((u8 - 2 u9 + 0)/0.25^2 + (0 - 2 u9 + u6)/0.25^2) ==
    0,UP9] ]

    output: 0.381295

    and now UP9 should hold 0.381295; in this expression the First function pulls the first element of the outer list which is {UP9 [itex]\to[/itex] 0.381295}, and then that is used to replace the value of UP9 using the /. function (which is a replacement operator).

    This idea will work because there is only one solution from using Solve. If there were more than one, you would need to tell it which solution to use (or else the above would always only pick the first solution returned).
     
  8. Dec 7, 2008 #7
    Strangely, sometimes it works, sometimes it doesn't.

    If I type something like:

    UP8 /. First[Solve[ <expression> == 0, UP8]]

    the value is not assigned to UP8, but, if I type

    UP8 = UP8 /. First[Solve[ <expression> == 0, UP8]]

    then sometimes it works, and sometimes (most of the time) this error appears:

    General::ivar: 0.11030304322627314` is not a valid variable. >>
    ReplaceAll::reps: {True} is neither a list of replacement rules nor a \
    valid dispatch table, and so cannot be used for replacing. >>

    Output: 0.203813 /.True
     
    Last edited: Dec 7, 2008
  9. Dec 7, 2008 #8

    alphysicist

    User Avatar
    Homework Helper

    Sorry; yes this last form is what I meant to type in. UP8 is evaluated and then set to that value.

    Once you have set UP8 equal to a number, you cannot then use it as a variable (in the math sense) in the Solve function (because then the condition in your Solve function is automatically evaluated to True or False). To run it again, you first have to clear UP8 by using

    Clear[UP8]

    and then you can run it again. So if you put the Clear[UP8] right before your Solve (in the same cell) I think you could run it again and again. So your cell might be:


    Code (Text):

    Clear[UP8]

    UP8 =  UP8  /. First[Solve[ <expression> == 0, UP8]]

    Print["UP8 = ",UP8]
     
    Does that work?
     
  10. Dec 7, 2008 #9
    Yes, It works perfectly now.

    Thank you very much.
     
  11. Dec 7, 2008 #10

    alphysicist

    User Avatar
    Homework Helper

    Glad to help!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Mathematica - Working with simplification
  1. In mathematica (Replies: 4)

Loading...