Mathematica: Evaluate Value of Alpha & Output to Text File

  • Mathematica
  • Thread starter Saladsamurai
  • Start date
  • Tags
    Mathematica
In summary: ListPlot[data] is for plotting data in a table. My fault. Change your above code to this: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}]) // TableFormListPlot[data]
  • #1
Saladsamurai
3,020
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
  • #2
[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]​
 
  • #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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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]
 
  • #7
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?
 
  • #8
I think you forgot to Print the calculation? (or to store it in something)
 
  • #9
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.
 

Related to Mathematica: Evaluate Value of Alpha & Output to Text File

1. How do I evaluate the value of alpha in Mathematica?

To evaluate the value of alpha in Mathematica, you can use the function "Evaluate" followed by the symbol for alpha (α). This will calculate the value of alpha based on any assigned variables or defined equations in your code.

2. Can I save the output of my alpha value to a text file in Mathematica?

Yes, you can use the function "Export" to save the output of your alpha value to a text file in Mathematica. Simply specify the file path and name, followed by the value you want to export in quotation marks. For example: Export["alpha_value.txt", alpha].

3. How can I assign a specific value to alpha in Mathematica?

To assign a specific value to alpha in Mathematica, you can use the assignment operator (:=) followed by the desired value. For example: alpha := 0.5. This will assign the value of 0.5 to alpha for use in your calculations.

4. Is there a built-in function for calculating the value of alpha in Mathematica?

Yes, there are several built-in functions in Mathematica that can be used to calculate the value of alpha, depending on your specific needs. Some useful functions include "Solve", "Reduce", and "FindRoot". These functions can help you solve equations and find the value of alpha that satisfies certain conditions.

5. Can I use Mathematica to find the optimal value of alpha for my data?

Yes, Mathematica has powerful optimization functions that can be used to find the optimal value of alpha for your data. These functions include "NMinimize", "FindMinimum", and "Minimize". These functions use numerical methods to find the value of alpha that minimizes a certain objective function or error metric.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Math
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
898
Replies
0
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
21
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top