scipy.optimize.curve_fit一个具有scipy.integrate.quad的确定积分函数 [英] scipy.optimize.curve_fit a definite integral function with scipy.integrate.quad

查看:122
本文介绍了scipy.optimize.curve_fit一个具有scipy.integrate.quad的确定积分函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个函数,即自变量是数学模型的确定积分的上限.这个数学模型具有我要进行回归的参数. 这个数学模型是非线性的,可能会很复杂.

If I have a function that the independent variable is the upper limit of an definite integral of a mathematical model. This mathematical model has the parameters I want to do regression. This mathematical model is nonlinear and can be complicated.

  1. 我该如何解决?

  1. How can I solve this?

如果随后处理了我函数的输出,是否可以是curve_fit?

if the output of my function is then be processed, can it be curve_fit?

有一个简化的案例

import scipy.optimize as sp
from scipy.integrate import quad
import numpy as np
number = 100

def f(x,a,b,c):
    return 500*a*x+b*c

def curvefit(d,a,b,c):
    return quad(f,0,d,args=(a,b,c))[0]

x_linear = np.linspace(0.001,0.006,number)
y_linear = 23.33*x_linear + 0.02*(np.random.random(number)-0.5)
parameter = sp.curve_fit(curvefit,x_linear,y_linear)

x和y _linear是我组成的数字.

x and y _linear are number I made up.

d是x_linear这是一个列表,并且是quad()中的上限.

d in curvefit() is now x_linear that is a list, and is the upper limit in quad().

错误显示ValueError:具有多个元素的数组的真值不明确.使用a.any()或a.all()

The error shows ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我知道quad()要求上限为浮点型.

I know quad() requires upper limit to be float.

推荐答案

在函数scipy.integrate.quad中会引发错误,因为d是numpy.array而不是标量.赋予scipy.optimize.curve_fit的函数将自变量(在您的情况下为x_linear)作为第一个参数.

The error is raised inside the function scipy.integrate.quad because d is a numpy.array and not a scalar. The function given to scipy.optimize.curve_fit take the independent variable (x_linear in your case) as first argument.

快速而肮脏的解决方法是修改curvefit以计算d中每个值的定积分:

The quick and dirty fix is to modify curvefit to compute the definite integral for each value in d:

def curvefit(xs,a,b,c):
    return [quad(f,0,x,args=(a,b,c))[0] for x in xs]

这篇关于scipy.optimize.curve_fit一个具有scipy.integrate.quad的确定积分函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆