# How can I find the current for a nonlinear electrical circuit using the Shockley equation, in Octave?

For this electrical circuit:

The voltage $$V_D$$ can be found by solving a nonlinear equation:

$$\frac{V_{DD}}{R} - \frac{V_D}{R} - I_se^{V_D/V_T} = 0$$

In this example, let $$R=1000$$, $$V_T = 0.025$$, and $$I_s = 10^{-13}$$.

I want to solve this equation using Octave in the range $$0 with a 0.01 step. Thus far, I have found a way to use fsolve to solve it time with $$V_{DD}=9$$ using the following code:

function y=f(x)
y=(9/1000) - (x/1000) - ( (10^(-13))*exp(x/0.025) );
endfunction

x=fsolve("f",1)


This correctly yields the following:

x = 0.6316


I'm happy that fsolve works quickly to solve this equation. However, I can't figure out how to get Octave to solve this same nonlinear equation 20,000 times, for input voltage $$V_{DD}$$ that varies from 0 to 20 with a 0.01 step. I'm not sure if fsolve is the best, or if Octave has another iterative solver to work with.

How can I do this with Octave?

## 1 Answer

The easiest way is to create a lambda function at call time which lets you set "extra" arguments. You can then call this in a for loop and vary the extra argument.

function y=f(Vd,Vdd)
y=(Vdd/1000)-(Vd/1000) - ((10^(-13))*exp(Vd/0.025));
endfunction

Vdd=linspace(0,20,20001)
sols = zeros(length(Vdd))
for i = 1:length(Vdd)
sols(i) = fsolve(@(x) f(x,Vdd(i)), 1);
endfor
$$$$

• It is worth noting that the convergence of the individual solves will likely be much more stable and marginally faster if you set the initial guess for each fsolve call to be the previous entry in sols`, assuming you are working in serial and that the difference between nearby voltages are small. This will ensure that your initial guess is close to the true solution. Oct 16, 2023 at 17:06
• True, doubly important because no analytical Jacobian is specified. Oct 16, 2023 at 21:25