# Mathematica questions

• Mathematica
Hey folks 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?

## Answers and Replies

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
$\alpha \to 0.14761$ is a substitution rule. You can use it with the substitution operator:
$$In := \alpha /. \alpha \to 0.14761$$
$$Out := 0.14761$$​
Substitution works with a list too:
$$In := \alpha /. \{ \alpha \to 0.14761 \}$$
$$Out := 0.14761$$​
${{ \alpha \to 0.14761 }}$ 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 []:
$$In := \alpha /. \{ \{ \alpha \to 0.14761 \} \} []$$
$$Out := 0.14761$$​

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.

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.

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?

Hurkyl
Staff Emeritus
Science Advisor
Gold Member
I think you forgot to Print the calculation? (or to store it in something)

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][]}, {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.

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]]

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

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.