0
$\begingroup$

Suppose I wanted to solve the following for $a[1], a[2], a[3]$ in the Mathematica code:

(a[1]+a[3])*t+(a[1]+2*a[2])*t^2=0

with $a[1] = 1$ where $t$ is just a variable and the equation above is identically 0. How would I automate this in Mathematica to get $a[3] = -1, a[2] = -1/2$?

2 Answers 2

2

In general, you can do it this way:

a[1] = 1; 
Solve[# == 0] & /@ 
 CoefficientList[(a[1] + a[3])*t + (a[1] + 2*a[2])*t^2, t]

Out[1] = {{{}}, {{a[3] -> -1}}, {{a[2] -> -(1/2)}}}

What the above does is it first collects the coefficients of the polynomial using CoefficientList and passes them to Solve which solves each of them.

0

This will do it:

a[1] = 1; Solve[{a[1] + a[3] == 0, a[1] + 2*a[2] == 0}, {a[2], a[3]}]
  • 0
    Ah, thanks! But what if instead of 2 relations, I had 100 of them? Is there a way to extract this using a for loop?2011-06-08
  • 0
    @Shayla: How would you enter the 100 relations into Mathematica? Are they somehow being generated automatically?2011-06-08
  • 2
    @Shayla: Incidentally, Mathematica is a functional programming language (see http://en.wikipedia.org/wiki/Functional_programming). Though it does have loop constructions, commands like Table, Map, and Apply are considerably more useful.2011-06-08
  • 0
    Yes, so let's say I have a differential equation $f''+f=0$ whose solution I'm going to write as $f(t)=\sum_{n = 1}^{100}a_{n}t^{n-1}$. It would be nice to compute the values of $a_{n}$.2011-06-08