Mathematica: Evaluate Value of Alpha & Output to Text File

  • Context: Mathematica 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around using Mathematica to evaluate a variable, alpha, at different values of another variable, i, and outputting the results to a text file. Participants explore methods for substitution, data organization, and plotting within Mathematica, while comparing it to MATLAB.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant expresses a desire to evaluate alpha at each value of i and output the results in a two-column format.
  • Another participant explains the use of substitution rules in Mathematica and suggests using the substitution operator for evaluating alpha.
  • Hints are provided on using the Table function instead of For loops to store results in a variable.
  • Participants discuss the advantages of returning rules for symbolic manipulation, allowing for further calculations before substituting values.
  • One participant mentions their familiarity with MATLAB and finds Mathematica's approach more complex, seeking help with plotting data.
  • Another participant points out the need to print calculations or store them to see outputs when using For loops.
  • Clarifications are made regarding the syntax of For loops and the importance of using Print to display results.
  • Participants share their experiences with learning Mathematica and express a desire for additional resources.

Areas of Agreement / Disagreement

Participants generally agree on the methods for substitution and data output, but there is no consensus on the ease of use of Mathematica compared to MATLAB, as opinions vary on the learning curve and functionality.

Contextual Notes

Some participants mention limitations in their understanding of substitution rules and the syntax of Mathematica, indicating a need for further exploration of these concepts.

Who May Find This Useful

This discussion may be useful for users transitioning from MATLAB to Mathematica, those interested in numerical evaluations and data output in Mathematica, and individuals seeking to understand substitution rules and plotting in the software.

Saladsamurai
Messages
3,009
Reaction score
7
Hey folks :smile:

Clearly there is a better way to do this.

Picture3-20.png


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?
 
Physics news on Phys.org
[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]​
 
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.
 
To put together what the last two posters told you:

Try something like

Code:
Clear[k]
k=0.0454;
(data = Table[{i,alpha/.Solve[k==(alpha/(1-alpha))(alpha/(2(alpha/2+1+i)))^0.5,alpha]},{i,0,2,0.1}])//TableForm
Export["filename",data];

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.
 
NeoDevin said:
To put together what the last two posters told you:

Try something like

Code:
Clear[k]
k=0.0454;
(data = Table[{i,alpha/.Solve[k==(alpha/(1-alpha))(alpha/(2(alpha/2+1+i)))^0.5,alpha]},{i,0,2,0.1}])//TableForm
Export["filename",data];

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.

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.
 
Saladsamurai said:
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.

Code:
ListPlot[data]
 
Ok folks. Thanks for the help so far!

NeoDevin: When I tack
Code:
ListPlot[data]
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:
For[i == 0, i <= 10, i++, x^2 /. x -> i]

But I get no output at all?
 
I think you forgot to Print the calculation? (or to store it in something)
 
Saladsamurai said:
Ok folks. Thanks for the help so far!

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

My fault. Change your above code to this:

Code:
Clear[k]
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
ListPlot[data]

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.

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

Code:
For[i == 0, i <= 10, i++, x^2 /. x -> i]

But I get no output at all?

Two problems with your code:

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

Code:
For[i=0,i<=10,i++,x^2/.x->i]

But even that will produce no output, because "For" suppresses most outputs by default. Try

Code:
For[i=0,i<=10,i++,Print[x^2/.x->i]]
 
  • #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.

~Casey
 
  • #11
Saladsamurai said:
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.

~Casey

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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 0 ·
Replies
0
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K