0
\$\begingroup\$

I tried to simulate a negative feedback non-inverting op-amp, and got incorrect results.

I ran the following time-stepping code in python:

A = 100000                      #open-loop gain  
B = 0.75                        #the feedback factor  
V_out = 0                       #initial output of op-amp is zero  
V_in = 2                        #input voltage of 5V (DC)  
for t in range(1000):  
    feedback = B*V_out  
    V_out = A*(V_in - feedback)  
    if V_out > 10:                    #saturation
        V_out = 10                    #hits rails at 10V
    elif V_out < -10:                   #saturation
        V_out = -10                   #hits rails at -10V

But this model doesn't behave like a practical op-amp. V_out saturates at -10V, and feedback goes to 7.5V, and not 2V, thus violating the second summing point constraint.

Why is this happening?

NOTE: I've used the standard general feedback model for reference.

\$\endgroup\$
  • 1
    \$\begingroup\$ You may be trying to simulate an op-amp but this is really a question about debugging python code and not about EE. \$\endgroup\$ – Andy aka May 17 '15 at 11:43
  • 1
    \$\begingroup\$ Hence, the closed-loop gain should be +4/3, correct? Another problem with your model (code) could be that - in contrast to reality - your opamp model does not contain any delay (frequency-dependent gain). Hence, the loop contains no delay and this could be a problem. Other simulators using node voltages - and work even for ideal VCCS. \$\endgroup\$ – LvW May 17 '15 at 12:03
  • 1
    \$\begingroup\$ OK - I have assumed this. Again - I suppose, a loop without any delay cannot be calculated correctly. Why not including a simple RC lowpass? It should be sufficient to replace the constant A or the constant B value by a first order lowpass function. \$\endgroup\$ – LvW May 17 '15 at 12:24
  • 1
    \$\begingroup\$ Why do you want a CHANGE of A? I do not know if your simulator/calculator can translate a lowpass function (frequency domain) into a corresponding time delay (time domain). But such a delay is important for proper loop calculation. \$\endgroup\$ – LvW May 17 '15 at 12:53
  • 2
    \$\begingroup\$ In general to solve a differential equation numerically (what you are trying to do) the change in variables between time steps must be 'small'. \$\endgroup\$ – Spehro Pefhany May 17 '15 at 14:12
2
\$\begingroup\$

Your first tick of time "t" produces an end-stopped output voltage at plus or minus 10V. It doesn't matter which because your next iteration will also produce an end-stopped output voltage of opposite polarity then you are back to the start again and you'll just produce end-stopped voltages each tick of time "t".

If you don't get alternate end-stops of plus then minus 10V then your code is wrong.

But, of course alternate end stops of 10V isn't how an op-amp works so you then have to look at what has gone wrong and build a small integrator into the code so that it more closely represents an op-amp. You have no mechanism for producing a value that can converge on 0V and that is your basic problem.

\$\endgroup\$
  • \$\begingroup\$ The output indeed oscillates between the power rails. Should I conclude that the model I am using is too general to simulate a real life op-amp? \$\endgroup\$ – Sidd May 17 '15 at 12:43
  • 1
    \$\begingroup\$ @Sidd Not too general- too simplistic. Andy's suggestion of an integrator simulates to a first order the real behavior of an op amp. \$\endgroup\$ – Spehro Pefhany May 17 '15 at 14:03
2
\$\begingroup\$

This obvious answer is that your code has a bug. I haven't looked at in detail to find it, but real opamps are a clear existance proof that feedback does really work.

That said, stability is a issue even with real opamps. This is probably easier to see by imagining the opamp working in discrete steps, much like a computer simulation. If the output is low and our step size too large, then the output will slam high that iteration. Now the output is too high, and will slam low next iteration, etc.

Something similar happens in the continuous analog domain too. It turns out that stability is less at lower overall loop gains. This is why you sometimes see opamp datasheets say the amp is "unity gain stable". That means it won't oscillate down to a gain of one. Among other things, such opamp can be safely used as a voltage follower.

Internally compensating a opamp to be stable at unity gain lowers its gain*bandwidth product. Some opamps are therefore compenstated for stability only at a higher gain, which is useful in circuits that require more bandwidth and want the higher gain anyway.

\$\endgroup\$
  • \$\begingroup\$ I checked the code output, and it is giving me an oscillating V_out and feedback. The feedback oscillates between +7.5V and -7.5V, and the output between the rails. So does this mean that a real amplifier with the characteristics of the one I am simulating might also oscillate in a similar manner? \$\endgroup\$ – Sidd May 17 '15 at 12:08
  • \$\begingroup\$ @Sidd: That would require your simulation to be accurate, with the right step size, etc. Without looking in detail at your simulation, I can't say that is true. You need to carefully test and debug your code before you can claim anything about it simulating a real opamp. \$\endgroup\$ – Olin Lathrop May 17 '15 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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