Ask Your Question

gottfried's profile - activity

2015-04-28 16:59:24 +0100 commented answer numerical solutions from a for-loop solve()

I need to some more karma myself to be able to upvote your answer (I'll upvote after using the site some more and getting more karma). BTW, to clarify, if there were three variables solved for, I would need to select the third one by writing sltns[0][2], yes?

2015-04-28 16:49:52 +0100 received badge  Scholar (source)
2015-04-28 15:26:48 +0100 received badge  Student (source)
2015-04-28 04:46:33 +0100 received badge  Editor (source)
2015-04-28 04:45:24 +0100 asked a question numerical solutions from a for-loop solve()

I am trying to extract a list of 3-tuples which solve a system of equations. For the test I am using a simply equation whose solutions go from negative to positive around one, so

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

does not work.

What I am trying to get is, for example, if c = 1/2, then (1/2, 2, -2] because if c = 1/2 then n==2 and u==-2. And so on for all the elements of the list.


If I omit defining sltns, but use the solve() directly, I get out of range errors when for some c, u goes from positive to negative or vice versa for n, and I use [0].rhs(). Isn't sltns[2] the way to select, for example, [d==4] in [a==2, b==3, d==4], while sltns[0] and sltns[1] select the first two? Is it limited to some range positive or negative?

My question is: 1. What is the correct way to code the calculation above? 2. How would I alternatively code the loop if I needed the () s to be tuples in a matrix [(),(), ..., ()] for further calculations or plotting?