# Finding numerical solution to a nonlinear equation

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I'm trying to find a solution for a nonlinear equation using cloud.sagemath but I have been unsuccessful. The command "solve" could not find a result, and I haven't been able to input the range over which the solution should be looked. Here is my code:

phi = var('phi')
a = .25
beta = 1
g = .2
ce = .5
alpha = .1
gamma = 2
V = .5
rho = .8
sigma = 4
sigmaeps = 1
K = 2
R = 2
K2 = 2
avginv = K+R+K2
Sinv = phi*avginv
Swinv = K2+R-Sinv
psinv = (-1+(1+4*rho^(-2)*sigma^(-2)*K)^(1/2))/(2*rho^(-2)*sigma^(-2))
phipriv = K/avginv
phipub = Swinv/avginv
prob = a-a*e^(-Swinv)
kappa = gamma*prob*(1-prob)*g^2
N = beta^(-1)*(1+alpha)*phi - beta^(-1)*alpha
L = beta^(-2)*(V+(1-phipriv)*avginv^(-1)+sigmaeps - 2*sigmaeps*phi + rho^2*sigma^2*psinv^(-2)*phipriv^2)
q = (beta*ce^(-1)-alpha*kappa^(-1)*N)*(gamma*L + ce^(-1)+kappa^(-1)*N^2)
y = q*N
phi1 = alpha/(1-alpha)
eq = beta*ce^(-1)-alpha*kappa^(-1)*N == 0
phi2 = solve(eq,phi)
print phi2


I get

[
phi == -1/11*e^(12*phi - 8) - 2/11*e^(6*phi - 4) + 4/11
]


Can someone help? And how can I ask for a solution on $[0,1]$?

edit retag close merge delete

Sort by » oldest newest most voted

Hello,

Something like that ?

sage: eq.find_root(0,1)
0.6666666666676859


or rather

sage: eq.find_root(0,1/2)
0.3367803592444174

more

By plotting the function, I noticed that there would be 2 roots. Unfortunately, find_root cannot obtain them at once. But following your suggestion did the trick.

( 2014-08-03 23:31:14 -0600 )edit