-1

I am iteratively solving this implicit equation

(here)

using fsolve within a for loop over a range of values of the independent variable, V.

I also want to vary I_L and run the for loop over each value and generate an individual text files.

I know how to use the open and write text files, what I'm struggling with is setting loops up correctly to output what I want.

I have coded a simpler example below to allow for ease of understanding since it's just the loops I'm stuck on.

import numpy as np
from scipy.optimize import fsolve
import scipy.constants as sc

x = np.linspace(-1, 1, 1001)

C_vary = [0, 1, 2, 3]

def equation(y, x, C):
    return C - np.exp(x+y) - y

for C in C_vary:
    
    y = []
    
   Solve equation at each value of C_vary and output y values to new list of
   results
martineau
  • 119,623
  • 25
  • 170
  • 301
  • This is confusing; what is the "y" argument entering your equation(y,x,C)? – Zarathustra Feb 18 '22 at 16:17
  • The original equation is y = C - exp(x+y). Writing it in the function 'equation' like this means that it is assumed C - exp(x+y) - y = 0, which it does. Please see the answer to another user's question here https://stackoverflow.com/questions/64400639/solve-an-implicit-equation-python. My definition of 'equation' should make sense once you've read. I use the same method as used there to solve the equation. Just confused about the looping to get out individual text files for each C_vary – spudulike97 Feb 18 '22 at 16:23
  • The change of notation makes the question hard to follow. –  Mar 02 '22 at 14:22

2 Answers2

0

I have introduced an initial guess for the function y(x), but you can check the details further. The output y is a list, with each element corresponding to a value of the C_vary parameters, for each x.

import numpy as np
from scipy.optimize import fsolve
import scipy.constants as sc

x = np.linspace(-1, 1, 1001)
C_vary = [0, 1, 2, 3]

def equation(y, x, C):
    return C - np.exp(x+y) - y

y0 = np.exp( 0.5*x ) #initial guess 
y  = [ fsolve( equation, y0, (x,ci) ) for ci in C_vary ]
  
Zarathustra
  • 391
  • 1
  • 12
0

If you are after I as a function of V and other parameters, you can solve the equation by means of the Lambert W function.

It has the form

z = e^(a z + b)

where z is linear in I, and this is

- a z e^(- a z) = - a e^b

or

z = - W(-a e^b) / a.