scipy.optimize.curve_fit a definite integral function with scipy.integrate.quad

2024/11/20 9:46:15

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. How can I solve this?

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

There is a simplified case

import scipy.optimize as sp
from scipy.integrate import quad
import numpy as np
number = 100def f(x,a,b,c):return 500*a*x+b*cdef 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 and y _linear are number I made up.

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

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

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

Answer

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.

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]
https://en.xdnf.cn/q/119847.html

Related Q&A

MAC OS - os.system(command) display nothing

When I run IDLE (python 3.8) :>>> import os >>> os.system("ls") 0 >>> os.system(echo "test") 0 >>> os.system("users") 0 >>> Bu…

Flask App will not load app.py (The file/path provided (app) does not appear to exist)

My flask app is outputting no content for the for() block and i dont know why.I tested my query in app.py , here is app.py:# mysql config app.config[MYSQL_DATABASE_USER] = user app.config[MYSQL_DATABAS…

how to create from month Gtk.Calendar a week calendar and display the notes in each day in python

I have created a calendar app with month and week view in python. In month view, I can write notes in each day, store them in a dictionary and save the dictionary in to disk so I can read it any time.…

How to access inner attribute class from outer class?

As title. the class set a attribute value inside inner class. then, access that inner attribute class from outer function. In below, attribute sets with inner function set_error. then, use outer functi…

Summing up the total based on the random number of inputs of a column

I need to sum up the "value" column amount for each value of col1 of the File1 and export it to an output file. Im new in python and need to do it for thousands of records.File1col1 col2 …

What is wrong with this Binomial Tree Backwards Induction European Call Option Pricing Function?

The function below works perfectly and only needs one thing: Removal of the for loop that creates the 1000 element array arr. Can you help me get rid of that for loop? Code is below #Test with europea…

Regex behaving weird when finding floating point strings [duplicate]

This question already has answers here:re.findall behaves weird(3 answers)Closed 4 years ago.So doing this (in python 3.7.3):>>> from re import findall >>> s = 7.95 + 10 pieces >&g…

how do i get this code to tell me what position a word in the sentence is

varSentence = ("The fat cat sat on the mat")print (varSentence)varWord = input("Enter word ")varSplit = varSentence.split()if varWord in varSplit:print ("Found word") else…

Reverse geocoding with Python/GoogleMaps API: How to parse response

Im attempting to use the Google Maps Geocoding API to find the State associated with a latitude and longitude with a Python script. I have no trouble getting back a response from the server, but when …

How to constantly generate random numbers inside for i in range loop

import random world_size=8 for i in range(world_size)chunk_type=random.randint(1,2)print(chunk_type)import randomclass chunk_type():chunk_type=random.randint(1,2)world_size=8for i in range(world_size):…