ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Oct 2022 11:25:01 +0200Solving a system of linear equations with complex numbers yields false solutionhttps://ask.sagemath.org/question/64344/solving-a-system-of-linear-equations-with-complex-numbers-yields-false-solution/I have equations for the analysis of an electric circuit. They contain at several places the term w*I (I is the imaginary unit). I get a solution for the currents I1, I2, I3, I4. Then I substitute the solution into the equations, but I see that two of the four equations are not fulfilled:
var('L1 L2 L3 L4 C1 C2 C3 C4 M23 I1 I2 I3 I4 U w p RL')
lsg = solve([I1*(w*I*L1 + 1/(w*I*C1)) + I2*(-1/(w*I*C1) ) == U, \
I2*(w*I*L2 + 1/(w*I*C1) + 1/(w*I*C2)) + I3*(w*I*M23 - 1/(w*I*C2)) + I1*(- 1/(w*I*C1)) == 0, \
I3*(1/(w*I*C3) + w*I*L3 + 1/(w*I*C2)) + I2*(w*I*M23 - 1/(w*I*C2)) - I4/(w*I*C3) == 0, \
I4/(w*I*C3) + I4*RL - I3/(w*I*C3) == 0], \
[I1, I2, I3, I4])
param = [w==1, U==1, C1==1, C2==1, C3==1, C4==1, L1==1, L2==1, L3==1, L4==1, RL==1, M23==0]
I1 = I1.subs(lsg).subs(param)
I2 = I2.subs(lsg).subs(param)
I3 = I3.subs(lsg).subs(param)
I4 = I4.subs(lsg).subs(param)
eqn = [I1*(w*I*L1 + 1/(w*I*C1)) + I2*(-1/(w*I*C1) ) == U, \
I2*(w*I*L2 + 1/(w*I*C1) + 1/(w*I*C2)) + I3*(w*I*M23 - 1/(w*I*C2)) + I1*(- 1/(w*I*C1)) == 0, \
I3*(1/(w*I*C3) + w*I*L3 + 1/(w*I*C2)) + I2*(w*I*M23 - 1/(w*I*C2)) - I4/(w*I*C3) == 0, \
I4/(w*I*C3) + I4*RL - I3/(w*I*C3) == 0]
print("I1=", I1)
print("I2=", I2)
print("I3=", I3)
print("I4=", I4)
[eq.subs(param) for eq in eqn]
Output (unexpected):
I1= 1
I2= -I
I3= -2
I4= I - 1
[1 == 1, (-I - 1) == 0, I == 0, 0 == 0]
I have copied and pasted the equations from within the solve() command into the eqn = ... statement. So they are guaranteed to be equal.
Because I have set M23=0 in the parameters, I could remove the terms with M23 in the equations. Then I get the correct solution. I don't understand why not when M23 is present.
Another way to get the correct solution is replacing w*I with p in the equations and setting p=I in the parameters:
var('L1 L2 L3 L4 C1 C2 C3 C4 M23 I1 I2 I3 I4 U w p RL')
lsg = solve([I1*(p*L1 + 1/(p*C1)) + I2*(-1/(p*C1) ) == U, \
I2*(p*L2 + 1/(p*C1) + 1/(p*C2)) + I3*(p*M23 - 1/(p*C2)) + I1*(- 1/(p*C1)) == 0, \
I3*(1/(p*C3) + p*L3 + 1/(p*C2)) + I2*(p*M23 - 1/(p*C2)) - I4/(p*C3) == 0, \
I4/(p*C3) + I4*RL - I3/(p*C3) == 0], \
[I1, I2, I3, I4])
param = [p==I, U==1, C1==1, C2==1, C3==1, C4==1, L1==1, L2==1, L3==1, L4==1, RL==1, M23==0]
I1 = I1.subs(lsg).subs(param)
I2 = I2.subs(lsg).subs(param)
I3 = I3.subs(lsg).subs(param)
I4 = I4.subs(lsg).subs(param)
eqn = [I1*(p*L1 + 1/(p*C1)) + I2*(-1/(p*C1) ) == U, \
I2*(p*L2 + 1/(p*C1) + 1/(p*C2)) + I3*(p*M23 - 1/(p*C2)) + I1*(- 1/(p*C1)) == 0, \
I3*(1/(p*C3) + p*L3 + 1/(p*C2)) + I2*(p*M23 - 1/(p*C2)) - I4/(p*C3) == 0, \
I4/(p*C3) + I4*RL - I3/(p*C3) == 0]
print("I1=", I1)
print("I2=", I2)
print("I3=", I3)
print("I4=", I4)
[eq.subs(param) for eq in eqn]
Output as expected:
I1= 1
I2= -I
I3= -I - 1
I4= -1
[1 == 1, 0 == 0, 0 == 0, 0 == 0]
Does anyone know why it yields false results in the first case (taking w*I instead of p) ?
Albert_ZweisteinFri, 07 Oct 2022 11:25:01 +0200https://ask.sagemath.org/question/64344/Getting all (complex) solutions of a non polynomial equationhttps://ask.sagemath.org/question/40302/getting-all-complex-solutions-of-a-non-polynomial-equation/Hi !
I was used to solve the following equation with Mathematica.
\begin{equation}
\alpha_1 + \alpha_2x + \alpha_3x^2 + x^4 + \frac{\alpha_4}{x^2-\alpha_0} + \frac{\alpha_5 x^2}{x^2-\alpha_0}=0
\end{equation}
where $\alpha_i$ are constants.
The Mathematica function "Solve" gives me all the numerical roots of this non polynomial equation very easily. These roots can be real or complex.
I'm a very beginner at Sage. I have tried several methods to solve this equation automatically but it seems that all methods I've used work only for polynomial equations. Here they are :
alpha0 = 0.25
alpha1 = -2.5
alpha2 = 6.9282
alpha3 = -5.5
alpha4 = 0.5
alpha5 = -0.5
x = var('x')
eq = alpha1 + alpha2*x + alpha3*x**2 + x**4 + alpha4/(x**2 - alpha0) + alpha5*x**2/(x**2 - alpha0) == 0.
# test 1
# solve(eq, x, ring=CC)
# ==> [0 == 20000*x^6 - 115000*x^4 + 138564*x^3 - 32500*x^2 - 34641*x + 22500]
# test 2
# solve(eq, x, ring=CC, solution_dict=True)
# ==> [{0: 20000*x^6 - 115000*x^4 + 138564*x^3 - 32500*x^2 - 34641*x + 22500}]
# test 3
# eq.roots(x, ring=CC,multiplicities=False)
# ==> TypeError: fraction must have unit denominator
Do you have any idea of a method to get the approximated roots of the equation ?
Thanks in advance
EDIT : correction of an error in the equation ; add few testsyoda789Tue, 26 Dec 2017 15:05:07 +0100https://ask.sagemath.org/question/40302/simplify equationshttps://ask.sagemath.org/question/9690/simplify-equations/Hello,
i'm trying to simplify equations "eq" (http://pastie.org/5624251) under two assumptions. The result is supposed to be (1+a*x+b*y+c*z) / 2
sage: var('x,y,z,a,b,c')
(x, y, z, a, b, c)
sage: (x*x+y*y+z*z == 1).assume()
sage: (a*a+b*b+c*c == 1).assume()
sage: eq = abs( sqrt((1+c)*(1+z)) / 2 + (1-c)*(1-z)*sqrt((1+c)*(1+z)) / (2*(a-i*b)*(x+i*y)) ) ^ 2
sage: eq.simplify_full()
(I*((-8.0*I*a*b - 8.0*b^2)*c - 8.0*I*a*b - 8.0*b^2)*x*y*z + 4*(c^3 - c^2 - c + 1)*z^3 + 8.0*I*b*c^2*x + 8.0*I*b*x*z^2 + I*((-8.0*I*a*b - 8.0*b^2)*c - 8.0*I*a*b - 8.0*b^2)*x*y + (-(4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c - 4*a^2 + 8.0*I*a*b + 4.0*b^2)*y^2 + ((4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c + 4*a^2 - 8*I*a*b - 4.0*b^2)*x^2 - (4*c^3 - ((8*a - 8*I*b)*c^2 - 8*a)*x - ((8*I*a + 8.0*b)*c^2 - 8*I*a - 8.0*b)*y - 4*c^2 - 4*c + 4)*z^2 + 4*c^3 + (-8.0*a*c^2 + 8*a - 8*I*b)*x + ((-8*I*a - 8.0*b)*c^2 + ((8*I*a^2 + 8.0*a*b)*c + 8*I*a^2 + 8.0*a*b)*x + 8*I*a + 8.0*b)*y + (((8*I*a^2 + 8.0*a*b)*c + 8*I*a^2 + 8.0*a*b)*x*y + (-(4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c - 4*a^2 + 8.0*I*a*b + 4.0*b^2)*y^2 + ((4.0*a^2 - 8.0*I*a*b - 4.0*b^2)*c + 4*a^2 - 8*I*a*b - 4.0*b^2)*x^2 - 4*c^3 + 4*c^2 + 4*c - 4)*z - 4*c^2 - 4*c + 4)/(-32.0*I*a*b*x^2 + 32.0*I*a*b*y^2 - 32.0*I*b^2*x*y + 16*(a^2 - b^2)*x^2 + (32*I*a^2 + 64.0*a*b)*x*y - 16*(a^2 - b^2)*y^2)
What am I doing wrong?rdmadss1Fri, 04 Jan 2013 20:06:15 +0100https://ask.sagemath.org/question/9690/