Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There were a few issues with your code.

  1. sltns is a list containing the list of solutions. So, you need to access sltns[0] before accessing each piece of a solution.
  2. You cannot use the name c for the variable in the symbolic solution and in the loop.
  3. You need to substitute the c value within the loop into the solution.
  4. There is no solution when c=1.

    var('n u c') sltns=solve([n + u==0, n + c*u - 1==0], n, u) print sltns L = [0, 1/4, 1/2, 3/4, 5/4, 6/4, 7/4, 2] for c1 in L: print (c1,sltns[0][0].rhs().subs(c==c1),sltns[0][1].rhs().subs(c==c1) )

There were a few issues with your code.

  1. sltns is a list containing the list of solutions. So, you need to access sltns[0] before accessing each piece of a solution.
  2. You cannot use the name c for the variable in the symbolic solution and in the loop.
  3. You need to substitute the c value within the loop into the solution.
  4. There is no solution when c=1.

Here is the corrected code.

var('n u c')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
print sltns
L = [0, 1/4, 1/2, 3/4, 5/4, 6/4, 7/4, 2]
for c1 in L:
    print (c1,sltns[0][0].rhs().subs(c==c1),sltns[0][1].rhs().subs(c==c1) )

)

There were a few issues with your code.

  1. sltns is a list containing the list of solutions. So, you need to access sltns[0] before accessing each piece of a solution.
  2. You cannot use the name c for the variable in the symbolic solution and in the loop.
  3. You need to substitute the c value within the loop into the solution.
  4. There is no solution when c=1.

Here is the corrected code.

var('n u c')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
print sltns
L = [0, 1/4, 1/2, 3/4, 5/4, 6/4, 7/4, 2]
for c1 in L:
    print (c1,sltns[0][0].rhs().subs(c==c1),sltns[0][1].rhs().subs(c==c1) )

To answer your second question, you can collect the information into a list of the rows and then convert to a matrix.

var('n u c')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
print sltns
L = [0, 1/4, 1/2, 3/4, 5/4, 6/4, 7/4, 2]
rows=[]
for c1 in L:
    print (c1,sltns[0][0].rhs().subs(c==c1),sltns[0][1].rhs().subs(c==c1) )
    rows.append([c1,sltns[0][0].rhs().subs(c==c1),sltns[0][1].rhs().subs(c==c1)] )
m=matrix(rows)
m