0
$\begingroup$

I'm new to Mathematica from R, since R is not quite good at dealing with the following problem. Say, i want to solve the function a x - 10 = 0. Each time I change the value of a, say, from 1 to 10 by 1, I get x as output.

Take a =2, i learnt the command

a=2
Solve[a * x - 10 ==0, x]

Then i got x value. However, i have to manually change a=3, a=4, a=5... which is annoying.

I went online and found the following ways. Either

Manipulate[{a, Solve[a*x - 10 == 0, x]}, {a, 1, 5, 1}]

or

Solve[#*x - 10 == 0, x] & /@ Range[1, 5, 1]

However, the output is like

{{{x -> 10}}, {{x -> 5}}, {{x -> 10/3}}, {{x -> 5/2}}, {{x -> 2}}}

which i don't know how to save it as a csv for further use in R. I want it to look like

a x
1 10
2 5
3 10/3
4 5/2
5 2

Any suggestions? thanks in advance

$\endgroup$
3
  • 2
    $\begingroup$ See here $\endgroup$
    – Sektor
    May 14, 2015 at 8:37
  • 1
    $\begingroup$ See also here. $\endgroup$ May 14, 2015 at 11:26
  • $\begingroup$ For the CSV export part see here $\endgroup$ May 14, 2015 at 12:34

3 Answers 3

5
$\begingroup$

How about Table[{a, x /. Solve[a*x - 10 == 0, x][[1, 1]]} // N, {a, 1, 10}] ?

You can then export to .csv using Export["ans.csv", %].

$\endgroup$
1
  • 1
    $\begingroup$ You probably want to do Export["ans.csv", Catenate[{{{"a", "x"}}, %}]] to give column headers as requested. $\endgroup$ May 14, 2015 at 10:25
3
$\begingroup$

I believe this question should eventually be closed as its elements have been addressed before. However since you are new I hope to give a better welcome than merely "go read this" etc. To that end:

  1. If you can solve your equation Symbolically you can use Solve once, then populate values of a as desired.

  2. The output format of Solve is a List of Lists of Rule expressions. Each sublist is a solution. They are in this format for convenience, not to make things difficult. See Assign the results from a Solve to variable(s) for a few ideas about handling these.

As an example I will perform a symbolic Solve, then extract the first (only) x solution and name it x1, then use Table to create your table of results. You can Export this or display it with TableForm.

sol = Solve[a*x - 10 == 0, x]

x1 = x /. sol[[1]]     (* see documentation for ReplaceAll and Part *)

Table[{a, x1}, {a, 1, 10}]
{{x -> 10/a}}

10/a

{{1, 10}, {2, 5}, {3, 10/3}, {4, 5/2}, {5, 2}, {6, 5/3},
 {7, 10/7}, {8, 5/4}, {9, 10/9}, {10, 1}}

To differentiate this answer from Chen Stats Yu's here is another formulation that in time may interest you:

fx1 = {#, x} & /. First @ Solve[#*x - 10 == 0, x]

Array[fx1, 10]
{{1, 10}, {2, 5}, {3, 10/3}, {4, 5/2}, {5, 2}, {6, 5/3},
 {7, 10/7}, {8, 5/4}, {9, 10/9}, {10, 1}}
$\endgroup$
1
  • $\begingroup$ thanks for the kind help. I would like to learn more, to burden you less :) $\endgroup$ May 14, 2015 at 9:40
2
$\begingroup$

If you wanted to do it with a function :

solveme[a_] := Flatten[{a, Values[Solve[a*x - 10 == 0, x]]}]

Note that the Flatten in my answer and the [[1,1]] are both ways to remove nested {}'s. Chen's does it by selecting Part mine does it by extracting the Values from Solve then using Flatten to remove nested lists.

Pop in a range of values for a

result = solveme[#] & /@ Range[1, 5, 1]

Export["result.csv", result]

Alternatively you can use the nice columnar formatting of Dataset via Association

solveme2[n_] := Association[{"a" -> n, "x" -> x /. Solve[n*x - 10 == 0, x][[1, 1]]}]

Dataset formats with a nice columnar layout and could support further querying.

result2 = solveme2[#] & /@ Range[1, 5, 1] // Dataset

Dataset

Then your csv export is nicely WYSIWIG with column headings in 10.1,

Export["result2.csv", result2]

for 10.0.2 and earlier providing you can work around the inability of Dataset to be exported cleanly.

Export["result2.csv", Catenate[{DeleteDuplicates[Keys[#]], Values[#]}]] &[Normal[result2]]

Basically we split the Keys and the Values of the Dataset up after converting it to an Association using Normal. We use Slot (#) and Function (&) to create a pure function in the Export expression that lets us pass result2 in twice, once to create the column headers using Keys - which we de-duplicate, and again to create the column values using Values. Then we merge it into a big list using Cantenate so the columns export correctly.

$\endgroup$
3
  • $\begingroup$ thanks to the useful answer! that really works fine for me, and I am really grateful. $\endgroup$ May 21, 2015 at 10:13
  • $\begingroup$ Dear Mr. Gordon, now i have one more problem and may need your kind help. Now there are not only a, but also b a = Range[1, 5, 1]; b = {3.4, 5.7, 6.9, 20, -1}; solveme[a_, b_] := Flatten[ {a, b, Values[ NSolve[{a x - b == 0}, x]] }] solvemeresult = solveme[#] & /@ {a, b} I'm afraid the last code is wrong and i get the answer {solveme[{1, 2, 3, 4, 5}], solveme[{3.4, 5.7, 6.9, 20, -1}]} which is not what i want. Would you mind giving me some advice about how to correct it please? $\endgroup$ May 21, 2015 at 15:18
  • $\begingroup$ Thanks! I find this thread that helps to my question [link]mathematica.stackexchange.com/questions/24939/… $\endgroup$ May 22, 2015 at 2:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.