Curve_fit of exponential function using FEM solution as input

Right, so now your dolfin function T is defined, and you want to pass it into your exponenial_func.

If I understand correctly, your situation is that you have some parametric function f(t) (your exponential function), and some other function T(x) (your dolfin function). What you’re trying to do is generate a third function in x,

g(x) = f( T(x) )

The title in your initial question was confusing, it sounded like you want to apply curve fitting to your calculated solution T(x).

Probably what you want to do is set up your transform function as an Expression, not a python function. Then you can project it like you did with your T function. But the Expression needs to take T as a parameter. That’s no longer simple with a simple in-line Expression, instead try a UserExpression

class exponenial_expr(UserExpression):
    def __init__(self, t, a, b, **kwargs):
        super().__init__(**kwargs)
        self.t = t
        self.a = a
        self.b = b
    def eval(self, values, x):
        values[0] = exponenial_func( self.t(x), self.a, self.b )
    def value_shape(self):
        return ()

cf. Using functions in expressions

Then you can project,

vp = project( exponenial_expr( T,*popt1 , degree=2), Space)

or you might prefer to interpolate

vp_interp = interpolate( exponenial_expr( T,*popt1 , degree=2), Space)
1 Like