0
$\begingroup$

For this electrical circuit:

enter image description here

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<V_D<20$ 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?

$\endgroup$

1 Answer 1

3
$\begingroup$

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
```
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$
    – whpowell96
    Oct 16, 2023 at 17:06
  • 1
    $\begingroup$ True, doubly important because no analytical Jacobian is specified. $\endgroup$ Oct 16, 2023 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.