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

Mathematica questions

  1. Jun 6, 2010 #1
    Hey folks :smile:

    Clearly there is a better way to do this.


    It should be clear that I want n to take on the value of alpha and not the expression "alpha --> value"

    What I really want to do is this: Evaluate the value of alpha at each value of i and then output the value of i in one column and the corresponding value of alpha in the next column.

    Then when I would like to output those to columns to a text file.

    Any ideas?
  2. jcsd
  3. Jun 6, 2010 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    [itex]\alpha \to 0.14761[/itex] is a substitution rule. You can use it with the substitution operator:
    [tex]In[1] := \alpha /. \alpha \to 0.14761[/tex]
    [tex]Out[1] := 0.14761[/tex]​
    Substitution works with a list too:
    [tex]In[2] := \alpha /. \{ \alpha \to 0.14761 \}[/tex]
    [tex]Out[2] := 0.14761[/tex]​
    [itex]{{ \alpha \to 0.14761 }}[/itex] is a list of substitution rules, corresponding to all solutions to your equation. You probably want to iterate through it (look up While, Do, Table and similar things) rather than just assuming it has one element. But if you wanted to do that, you could index it with [[1]]:
    [tex]In[2] := \alpha /. \{ \{ \alpha \to 0.14761 \} \} [[1]][/tex]
    [tex]Out[2] := 0.14761[/tex]​
  4. Jun 6, 2010 #3
    A few hints:

    Use Table rather than For. That can get your results into a variable where you can use them instead of just having them printed.

    Study and understand the previous hint about using substitution.

    Since you want i AND n for each row think {i, n} instead of just n as each result.

    Think Export and the various formats it can export into to get it into a file.
  5. Jun 6, 2010 #4
    To put together what the last two posters told you:

    Try something like

    Code (Text):
    (data = Table[{i,alpha/.Solve[k==(alpha/(1-alpha))(alpha/(2(alpha/2+1+i)))^0.5,alpha]},{i,0,2,0.1}])//TableForm
    The advantage of returning a rule, rather than a value, is that you can continue symbolic manipulation (including things such as integrating over a variable, differentiating, etc.) and substitute the value in at the last step. You could instead take a value and assign it to the variable at the last step, but this has the disadvantage that if you had any other manipulations to do with other functions with the same variables, they would no longer be variables.
  6. Jun 6, 2010 #5
    Interesting stuff. For now though I really only need to do numerical stuff. I am better versed in MATLAB and I know that this would be simple to do; the Mathematica switch seems to be a pain for now. Your table function above seems to do the trick; now I just need to figure out how to plot the data in the table.
  7. Jun 6, 2010 #6
    Code (Text):
  8. Jun 6, 2010 #7
    Ok folks. Thanks for the help so far!

    NeoDevin: When I tack
    Code (Text):
    at the end of my code, it does not do anything. What am I supposed to be doing with that command? Thanks!

    Also: I am trying to learn how to use these 'substitution rules.' I figured this would work as a quick test:

    Code (Text):

    For[i == 0, i <= 10, i++, x^2 /. x -> i]
    But I get no output at all?
  9. Jun 6, 2010 #8


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    I think you forgot to Print the calculation? (or to store it in something)
  10. Jun 6, 2010 #9
    My fault. Change your above code to this:

    Code (Text):
    k = 0.0454;
    (data = Table[{i, alpha /. Solve[k == (alpha/(1 - alpha)) (alpha/(2 (alpha/2 + 1 + i)))^0.5, alpha][[1]]}, {i, 0, 2, 0.1}]) // TableForm
    The reason is that Solve does not return a list of replacement rules, but rather a list of lists of replacement rules. This makes sense if you think of the cases where you are solving for multiple variables, and there are multiple sets of solutions.

    That's what I get for not testing the code in Mathematica before I post it.

    Two problems with your code:

    First, and most important, "i==0" is a comparison, not an assignment. It should read

    Code (Text):
    But even that will produce no output, because "For" suppresses most outputs by default. Try

    Code (Text):
  11. Jun 7, 2010 #10
    Most excellent! Thanks so much guys! I am trying to resist the easy way out, which would be to just do this all in MATLAB. I've see what Mathematica can do and I am eager to learn, but it is quite different from MATLAB. MATLAB is basically a really fancy 'calculator' and the functionality is really quite intuitive, whereas Mathematica is not--at least for now.

    If anyone knows of any good Mathematica reading, by all means share.

  12. Jun 7, 2010 #11
    The tutorials included in the Mathematica help are pretty good. I learned by myself and from asking online, so I can't recommend any particular books, but I imagine if you hit up amazon and read the reviews, you should be able to find something.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook