I have been looking for an answer now and I can't find a solution. I am trying to use sympy.solve in Python 3 and I only want to see the real roots and all positive answers.
Sympy is able to solve what equation I want, but It shows a bunch of nonsense as the other answers. I have tried some of the other methods I found like solveset but they seem to still show the complex solutions
Image of Sympy solution
This is my code if that helps:
h, rho_c_s, T_c_s, k, pi, M, M_sun, m_H, m_e, mu_e = symbols(
r"h,\rho_\odot,T_\odot,k,\pi,M,M_\odot,m_H,m_e,\mu_e",positive=True,real=True)
rho_c = rho_c_s * (M / M_sun)**(-2 / 7)
T_c = T_c_s * (M / M_sun)**(4 / 7)
expr1 = (h**2) / (20 * m_e) * (3 / pi)**(2 / 3) * (rho_c /
(mu_e * m_H))**(5 / 3)
expr2 = (rho_c * k * T_c) / (mu_e * m_H)
expr = expr1 - expr2
eqn = Eq(expr, 0)
soln = solve(eqn, M, simplify=True,real=True,positive=True,)
init_printing()
soln
I appreciate your help.
Thanks
Well I figured it out.
You want to do:
soln[0]
And it will only give you:
Solution
Complex solutions are normal and intended. To hide the complex portions look here:
https://stackoverflow.com/a/73669399/11468876
Related
For multiplication and division, we can use the left and right shifts.
x>>2 // it will right shift by 2. ---> 2^2=4. (Multiply by 4 or divide by 4, depends on MSB/LSB)
However, if we want to divide by a number that isn't the power of 2, how can we achieve the required purpose?
Booth's algorithm is an additive one and can take a comparatively longer time than the multiplicative algorithms, like the Newton-Raphson algorithms found in this educational PDF.
Each next approximation is calculated using the previous approximation.
X(N+1) = X(N)(2 - b * X(N)), where x(0)=1
So, to find the inverse of b, i.e. 1/b, where b=0.6 (error=e(x)), it takes about 5 iterations.
X(000) = 1.000
X(001) = 1.000 * (2 - (1.000 * 0.6)) = 1.400
X(002) = 1.400 * (2 - (1.400 * 0.6)) = 1.624
X(003) = 1.624 * (2 - (1.624 * 0.6)) = 1.6655744
X(004) = X(003) * (2 - (X(003) * 0.6)) = 1.666665951
X(005) = X(004) * (2 - (X(004) * 0.6)) = 1.666666668
which approximates the answer, which is 1.6666666667.
I included this example in case the referenced PDF disappears. See the referenced PDF or look up the Newton-Raphson algorithm for more information.
By using Booth's restoring division algorithm
I have a standard problem in economics, which I would like to solve using sympy's nonlinsolve.
I want to symbolically find the steady state of a model and to that end solve a nonlinear system of equations.
I was happy to see sympy has nonlinsolve which is supposed to be able to solve such problems but I am so far unable to do so.
In the following you find the part of the problem which does not solve on it's own:
from sympy.solvers.solveset import nonlinsolve
from sympy import var
var('c k mc n r_k eta w alpha', Rational = True, positive=True)
eq1 = -r_k + alpha * mc * k**(-1 + alpha) * n**(1 - alpha)
eq2 = -w + mc * (1 - alpha) * k**alpha * n**(-alpha)
eq3 = c**-1 * w - n**eta
eq_system = [eq1,eq2,eq3]
nonlinsolve(eq_system , [n,k,w])
alpha and eta are parameters whereas the other symbols are variables. I know they are rational positive numbers but for a reason I do not understand yet, the code throws:
ValueError: alpha: Exponent must be a positive Integer
Neither do I see why alpha must be an integer nor does it help if I declare alpha as such.
Any hint as to how I can get a meaningful solution using sympy?
Many thanks,
Thore
I want to multiply two matrix like this(scalar multiplication).
But this method looks dumb. How can I make this much simpler?
A.size # B * S * V
B.size # B
A * B.unsqueeze(1).unsqueeze(2).expand_as(A)
I am trying to write a short trig function wanting to use sine and cosine functions in VB and I am very new to this as it's my first time coding in VB.
See what I'm trying to accomplish below:
Function XXX(HR, HR_RL, abc)
Const PI As Double = "3.141593"
AC = Sqr(HR ^ 2 + HR_RL ^ 2 - 2 * HR * HR_RL * Cos(abc * PI / 180))
VB is complaining about two things Cos and Sqr.
It seems they are both built-in VBA functions, but somehow I can't get them evaluated and as a result, AC variable = empty.
In fact, it doesn't seem that it even goes past "Cos(abc * PI / 180)". If I substitute "Cos(abc * PI / 180)" with a numeric value (just to see where else is another problem), surely enough, now VB complains about "Sqr".
Go into your VBA Editor menu and select Tools -> References..., then check the System library. It will give you access to all the math functions.
I am relatively new to SAS with limited programming experience. I need to write code that searches for the value of a specific variable that will form an equality. For example, I need to find the value of k that makes the following algebraic equation hold:
A = B + {[(C - k(B)] / (1+k)} + {[(D - k(E)] / (1+k)^2}, etc.
In this equation, I know the values of A, B, C, D, etc. and need to search for a value of k (the discount rate) that fits the equality.
Here's the proc model code I'm trying to use:
proc model data = test noprint;
p = bv0 + ((e1 - (k * bv0)) / (1+k)) + ((e2 - (k * bv1)) / ((1+k)**2)) + ((e3 - (k * bv2)) / ((1+k)**3)) + ((e3 - k *(bv2)) * (1+g)) / (((1+k)**3) * (k - g));
ENDOGENOUS k;
solve k / out = est;
run;
When I run this code, I receive the following error message:
WARNING: No equations are defined in the model. (Check for missing VAR or ENDOGENOUS statement.)
ERROR: The following solve variables do not appear in any of the equations to be solved: k
Any help anyone can provide would be great! Thanks!
If p is supposed to be the name of an equation, try adding eq. prefix before p. If p is a variable that the expression on the right should be equal to, then replace p with eq.equation1 and put -p on the right side.