Dave
Dave

Reputation: 123

How to take a derivative of Function, then evaluate using real numbers?

I have a massive, ugly expression. I need to take its derivative, then plug in some numbers and evaluate it.

This is to calculate the total error in the output of an electrical circuit.

The actual formula looks like this:

Ugly Formula

Almost all those variables on the right are temperature dependent, and I am concerned with the overall temperature sensitivity, dReff/dT.

But let's make things simpler for the sake of explanation. Let's say I have a much simpler formula:

R(T) = 2*R_0(T)

I would like to get the derivative in terms of the variables:

dR/dT = 2*dR_0/dT

And then, knowing dR_0/dT from a datasheet, I would like to be able to plug it in:

dR_0/dT = 2ohms/°C

dR/dT = 2*2ohms/°C = 4ohms/°C

Now, I've used python and sympy to get me pretty far but now I'm stuck. It looks like this:

from sympy import *

T = Symbol('T')
R_0 = Function('R_0')(T)
R = 2*R_0

diffR = Symbol('diffR')
diffR = R.diff(T)

At this point if you print diffR, you get the following

2*Derivative(R_0(T),T)

How to I get the rest of the way? Is there a way to plug in values for the derivative term?

Upvotes: 1

Views: 1118

Answers (1)

Jared Goguen
Jared Goguen

Reputation: 9010

One solution is to make another symbol for the derivative:

dR0 = Function('R_0')(T).diff(T)

Then, you can substitute in for this in your larger derivative expression.

expr.subs(dR0, value)

This works in the few examples I've tried. Their might be an easier way to do this, but I only deal with sympy when strictly needed.

Upvotes: 3

Related Questions