integrating a function through a for loop with appending an array - python

I am trying to perform an integration through a for loop to generate values for each range but cannot figure out how to append the array at the end.
import numpy as np
from matplotlib import pyplot as plt
from scipy.integrate import quad
import scipy.integrate as integrate
import scipy.special as special
def func(m):
return m**-1.35
mass=np.zeros(49)
for i in range(1,50):
x=mass=integrate.quad(func,i,i+1)
mass.append(x,i)

Related

List of matrices: plot each element of matrix as a function of an index

I have a list of matrices. I would like to plot each element of those matrices in function of another list.
However I am struggling to do it without using a loop.
How can I do it in the simplest way ?
Below a code explaining a little bit more what I want to do.
import numpy as np
from numpy import *
from matplotlib.pyplot import *
import matplotlib.pyplot as plt
from mpmath import *
import mpmath as mpmath
import pylab
import numpy
import time
import math
from qutip.sparse import sp_eigs
import numpy, scipy.io
from random import *
randomMatrixList=[np.random.rand(2,2) for _ in range(10)]
index=np.arange(10)
# I want to plot on x axis: index, on y axis: randomMatrixList[ii][0] for ii
# corresponding to index[ii] for the "0" curve, then randomMatrixList[ii][1] for the first one, and so on
I don't think there is any way to do this completely without loops, but this way is somewhat compact. There is further cleverness to be done if you want, but the code below is a trade off in terms of explicitness and ease to understand.
import numpy as np
import matplotlib.pyplot as plt
randomMatrixList = [np.random.rand(2, 2) for _ in range(10)]
index = np.arange(10)
stacked_matrices = np.array(randomMatrixList)
print(stacked_matrices.shape)
for k in range(stacked_matrices.shape[1]):
for j in range(stacked_matrices.shape[2]):
plt.plot(index, stacked_matrices[:, j, k], label=f"mat[{j},{k}]")
plt.legend()
plt.xlabel("index")
plt.show()
The code produces the image below

How to use loop and range function in python to visualize plot?

Im trying to understand How to use loop and range function in python( matplotlib package ) to visualize the cdf of the Poisson distribution in a single density plot??
𝜆 = 3,4,5,6,7
import numpy as np
import matplotlib.pyplot as plt
plt.gcf().set_size_inches(12,6)
for l in range(3,8,1):
seq = np.arrange(0,20)
How can i build the rest of the code ?
import numpy as np
You define the range of values to calculate the cdf outside the loop, and for every iteration, you have the values as x, and the cdf as y:
import matplotlib.pyplot as plt
from scipy.stats import poisson
Seq = np.arange(0,20)
plt.gcf().set_size_inches(12,6)
for l in range(3,8,1):
plt.plot(Seq,poisson.cdf(Seq,l),label=l)
plt.legend(loc="lower right")

How to plot the orbit which spirals into centre of orbit by using loops?

Hi i have written some codes when i try to plot the orbit,b=beta=3,G=1, i cant get the orbit which spirals into the centre by using loops it shows an incomplete orbit.
below is the code with b=beta=3 and G=1 :
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def dbydt(t,v):
x,y,vx,vy=v
rsq=x**2+y**2
G=1
b=3
beta=3
return vx,vy,-G*x/rsq**((b+1)/2),-G*y/rsq**((beta+1)/2)
result=solve_ivp(dbydt,[0,2],[1,0,0,1],t_eval=np.linspace(0,2,100))
plt.scatter(result.y[0],result.y[1])
plt.scatter(0,0,color='yellow',s=200)
plt.show()
If you increase your final time to more than 2, eg. 25 in this case:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def dbydt(t,v):
x,y,vx,vy=v
rsq=x**2+y**2
G=1
b=3
beta=3
return vx,vy,-G*x/rsq**((b+1)/2),-G*y/rsq**((beta+1)/2)
t_final = 25
result=solve_ivp(dbydt,[0,t_final],[1,0,0,1],t_eval=np.linspace(0,t_final,512))
plt.scatter(result.y[0],result.y[1])
plt.scatter(0,0,color='yellow',s=200)
plt.show()

I am not able to create the graph for y=sin(x)

enter code here
import numpy as np
import math
import matplotlib.pylab as plt
a=np.linspace(3,6,10)
plt.plot(a,math.sin(a))
plt.show()
The output says ****TypeError: only size-1 arrays can be converted to Python scalars
Use np.sin or np.vectorize(math.sin).
import numpy as np
import math
import matplotlib.pylab as plt
a = np.linspace(3,6,10)
plt.plot(a, np.sin(a))
plt.show()
Note that np.sin, like math.sin, takes radians rather than degrees, so you may want to adjust your array (a) accordingly, or use np.rad2deg because at the moment the result is:
Whereas if you were to pass in floats between 0 and 2 * math.pi, you would get a nice sine wave:

Is there a way to get an r-squared value for nonlinear regression?

I managed to optimize a line in order to get a line of best fit using curve_fit, but I can't seem to get the R squared value the way I can for linear regression, this is my code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.optimize import *
from scipy.integrate import *
from scipy.interpolate import *
df=pd.read_csv('F:/Data32.csv')
df2=df['Temperature']
df3=df['CO2-Rh']
def f(x,a,b,c) :
return a*np.exp(b*x)+c
params, extras = curve_fit(f, df2, df3)
print('a=%g,b=%g, c=%g' %(params[0],df2[1],df3[2]))
plt.plot(df2,df3,'o')
plt.plot(df2,f(df2,params[0],params[1],params[2]))
plt.legend(['data','fit'],loc='best')
plt.show()

Categories