An example of a free-form problem
Han Le - Copyrighted

0. Consideration - following the guideline

1-    Find an electrical engineering problem that should be solved with materials covered in this course Numerical Methods. Think of the follow:
•    What EE courses have you taken?
•     What were some problems that you encountered in those courses?
•    What new, creative “twists” can you introduce into one of those problems?
•    Do you think you can solve that problem (with a new twist preferably), by applying what you learned in this class?

outcome: an op-amp circuit

A problem from a course in circuit: how to make a square wave oscillator with op-amp.
Consider the op-amp circuit below:

OpAmp_oscillator_example_1.gif

We want to solve for OpAmp_oscillator_example_2.png
According to op-amp theory: OpAmp_oscillator_example_3.png
where OpAmp_oscillator_example_4.pngis the open-loop gain. But OpAmp_oscillator_example_5.png is extremely large, hence, unless
                         OpAmp_oscillator_example_6.png
we have:         OpAmp_oscillator_example_7.png   
We can find OpAmp_oscillator_example_8.png as follow.

We see that:  OpAmp_oscillator_example_9.png   where we define:  OpAmp_oscillator_example_10.png
                       OpAmp_oscillator_example_11.png
                       OpAmp_oscillator_example_12.png ;   OpAmp_oscillator_example_13.png
                       OpAmp_oscillator_example_14.png
Now, we are ready to use NDSolve to solve a nonlinear system of differential equations.

1. Numerical Method with NDSolve

Setup

1. Write code for equations

vp, vm, and vout are OpAmp_oscillator_example_15.png,  OpAmp_oscillator_example_16.png, and  OpAmp_oscillator_example_17.png. Let: OpAmp_oscillator_example_18.png
the equations are:

OpAmp_oscillator_example_19.png

We notice that the relationship  OpAmp_oscillator_example_20.png is just too simple to make it an equation. Hence, we eliminate OpAmp_oscillator_example_21.png and substitute it with OpAmp_oscillator_example_22.png to have only 2 equations: (this save a little computation time)

OpAmp_oscillator_example_23.png

2. Write code for inititial conditions

We need initial conditions:
Let just set vm[t=0]=0. At that point, vp can be positive or negative depending on vout. Let’s assume vout=1.

OpAmp_oscillator_example_24.png

Let’s choose:

3. Define parameters

OpAmp_oscillator_example_25.png

4. Put all together according to correct order

Now we use NDSolve

OpAmp_oscillator_example_26.png

5. Plot to look at solutions

We will plot OpAmp_oscillator_example_27.png, OpAmp_oscillator_example_28.png which is  OpAmp_oscillator_example_29.png and OpAmp_oscillator_example_30.png

OpAmp_oscillator_example_31.png

OpAmp_oscillator_example_32.gif

It works. It shows that the circuit is indeed a bistability system with output that flipflops between 2 states. This is just an illustration of the very high-gain behavior of an open-loop op-amp: the voltage output is saturate at either max positive or max negative OpAmp_oscillator_example_33.png and hence generates a square wave.
What we do here is to demonstrate it with brute-force numerical differential equation solution rather than circuit analysis. In fact, if we didn’t do circuit analysis, this method helps us gain insight so that we can go back to do circuit analysis and understand what happens.

Manipulatable parameters

As a final step, we now put the whole thing in a dynamic loop so that we can vary the circuit parameters τ and γ.

OpAmp_oscillator_example_34.png

Put into an app structure

OpAmp_oscillator_example_35.gif

2. Time-step (Finite-difference) simulation of op amp square-wave generator

Discussion

We see the above NDSolve does the job, but it is slow as expected. We can have a different appraoch, based on the insight we gain above that is faster. In this approach, we solve the problem as time-domain difference equation: we make the step in time and update the discrete values of the voltages at the sampling time point.

But we can go even further: we can solve the simple and straight forward equation:
                        OpAmp_oscillator_example_37.png
whenever OpAmp_oscillator_example_38.png is not at a transition point, because it is always a constant otherwise, either OpAmp_oscillator_example_39.png or OpAmp_oscillator_example_40.png.
Thus, treating  OpAmp_oscillator_example_41.png as a constant:

OpAmp_oscillator_example_42.png

OpAmp_oscillator_example_43.png

OpAmp_oscillator_example_44.png is just a constant to be determined by initial condition. The tricky thing here, however is that we should choose the initial condition to be the starting point of a cycle, which is where OpAmp_oscillator_example_45.png just flips and starts its constant value for 1/2 a cycle.
That point is where OpAmp_oscillator_example_46.png. Thus, we can choose that as t=0:
OpAmp_oscillator_example_47.png
Now we can write the code

Code development

Equation for time step

OpAmp_oscillator_example_48.gif

Test with a Do loop

OpAmp_oscillator_example_49.gif

Plot the result:

OpAmp_oscillator_example_50.png

OpAmp_oscillator_example_51.gif

It works as expected.

Manipulate loop for control of parameters

OpAmp_oscillator_example_52.png

Put into an app structure

OpAmp_oscillator_example_53.gif

3. Op amp square-wave sound generator

We can put the waveform into an amplifier and speaker to listen to it.

OpAmp_oscillator_example_55.gif

Created with the Wolfram Language