Reiterative mapping with the previous result - python

I'm trying to reiterate calculation using the previous result via using map function. I have a code work, but looks ugly. If you have insights, so that a code can be written elegantly, please, teach me. Any help will be very appreciable.
The reiterating process is described as you see in the figure below.
I have put my ugly code and also my trial with map function. I appreciate your help in advance.
The ugly one
import numpy as np
ys=np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
xs=ys
from scipy.interpolate import interp1d
g = interp1d(xs, ys, fill_value='extrapolate')
x0=ys[0]
s1=-4
def func(x1):
return -g(x1)/(x0-x1)-s1
from scipy.optimize import fsolve
initial_guess = 5
x1=fsolve(func, initial_guess)[0]
print(x1)
s2=-2
def func(x2):
return -g(x2)/(x1-x2)-s2
from scipy.optimize import fsolve
initial_guess = 5
x2=fsolve(func, initial_guess)[0]
print(x2)
s3=-0.67
def func(x3):
return -g(x3)/(x2-x3)-s3
from scipy.optimize import fsolve
initial_guess = 5
x3=fsolve(func, initial_guess)[0]
print(x3)
My trial with map function
import numpy as np
ys=np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
xs=ys
from scipy.interpolate import interp1d
g = interp1d(xs, ys, fill_value='extrapolate')
x0=ys[0]
s=[-4,-2,-0.67]
def func(x):
return -g(x)/(x0-x)-s
xall=list(map(func, s))
from scipy.optimize import fsolve
initial_guess = 5*np.ones(s.size)
xi=fsolve(xall, initial_guess)[0]
print(xi)

Maybe you want to use a lambda function as input to fsolve. Something like this:
import numpy as np
from scipy.optimize import fsolve
from scipy.interpolate import interp1d
ys = np.array([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
xs = ys
g = interp1d(xs, ys, fill_value='extrapolate')
x0 = ys[0]
s = [-4, -2, -0.67]
initial_guess = 5
for si in s:
x0 = fsolve(lambda x1: -g(x1)/(x0 - x1) - si, initial_guess)[0]
print(x0)

Related

"int 'object is not subscriptable"

i'm starting to learn GEKKO. Now, I am solving a knapsak problem to learn, but this time I get the error "int 'object is not subscriptable". can you look at this code? what is the source of the problem How should I define the 1.10 matrices?
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Var((10),lb=0,ub=1,integer=True)
#x = m.Array(m.Var,(1,10),lb=0,ub=1,integer=True)
v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10])
w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3])
capacity=16
for j in range(10):
m.Maximize(v[j]*x[j])
for i in range(10):
m.Equation(m.sum(x[i]*w[i])<=capacity)
m.options.solver = 1
m.solve()
#print('Objective Function: ' + str(m.options.objfcnval))
print(x)
My second question is that there is a function called "showproblem ()" in MATLAB. Does GEKKO have this function?
thanks for help.
new question that according to answer.
can i write here this style(that doesnt work, if i can do it, please write working style)(i want to write this style, because i think this style is easier to understand.),
for i in range(10):
xw = x[i]*w[i]
m.Equation(m.sum(xw)<=capacity)
instead of this.
xw = [x[i]*w[i] for i in range(10)]
m.Equation(m.sum(xw)<=capacity)
Here is a modified version that solves the mixed integer problem in gekko.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
x = m.Array(m.Var,10,lb=0,ub=1,integer=True)
v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10])
w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3])
capacity=16
for j in range(10):
m.Maximize(v[j]*x[j])
xw = [x[i]*w[i] for i in range(10)]
m.Equation(m.sum(xw)<=capacity)
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(-m.options.objfcnval))
print(x)
Your problem formulation was close. You just needed to define a list xw that you use to form the capacity constraint.
If you want to use a loop instead of a list comprehension then I recommend the following instead of xw = [x[i]*w[i] for i in range(10)].
xw = []
for i in range(10):
xw.append(x[i]*w[i])

What's meaning of plt.plot(x[0:-1],y/y[0])?

I am plotting an exponential distribution using the information provided by the tutor.
plt.plot(x[:-1],y/y[0])
plt.plot(tvals,pvals)
plt.show()
But, I do not know what's meaning of x[:-1] and y/y[0]?
x[:-1] means all the elements except the last one
y/y[0] is simply dividing the array y by the first value i.e y[0] of the array.
Code Example
import numpy as np
import matplotlib.pyplot as plt
x = np.array([1, 3, 5, 7])
y = np.array([2, 4, 6])
a = x[:-1] # [1, 3, 5]
b = y/y[0] # [1, 2, 3]
plt.plot(a, b)
Output

Combination of Map and Integration

This is the equation that I'm trying to plot, but have not been successful for hours. XA is variable between 0 to 1. I'd like to plot it while I'm varying eA and n constants. I'm still learning Python and this is being too complicated for me. Any help will be very appreciable.
XA = np.linspace(1e-2, 1-1e-2, 20)
from scipy.integrate import quad
def integrand(XA):
return ((1+eA*XA)/(1-XA))**n
p = lambda XA: quad(integrand, 1e-2, XA)[0]
xs = 1-XA
def func(n, eA):
return (XA*((1+eA*XA)/(1-XA))**n)/(p)
n = [1, 1, 2, 2]
eA = [1, 2, 1, 2]
ys = list(map(func, alps, e))
plt.plot(xs, ys)
plt.show()
You need to evaluate the functions in order to use them in further calculations. Also make sure to supply the needed arguments to the functions.
Here would be an example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
bracket = lambda eA,XA,n: ((1+eA*XA)/(1-XA))**n
p = lambda eA,XA,n: quad(lambda x: bracket(eA,x,n), 1e-2, XA)[0]
func = lambda eA,XA,n: XA*bracket(eA,XA,n)/p(eA,XA,n)
ns = [1, 1, 2, 2]
eAs = [1, 2, 1, 2]
XAs = np.linspace(3e-2, 1-1e-2, 50)
for e,n in zip(eAs,ns):
ys = list(map(lambda x: func(e,x,n), XAs))
plt.plot(XAs, ys, label="n={}, $e_A$={}".format(n,e))
plt.xlabel("$X_A$")
plt.legend()
plt.show()

Piecewise regression python

I am trying to do a piecewise linear regression in Python and the data looks like this,
I need to fit 3 lines for each section. Any idea how? I am having the following code, but the result is shown below. Any help would be appreciated.
import numpy as np
import matplotlib
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from scipy import optimize
def piecewise(x,x0,x1,y0,y1,k0,k1,k2):
return np.piecewise(x , [x <= x0, np.logical_and(x0<x, x< x1),x>x1] , [lambda x:k0*x + y0, lambda x:k1*(x-x0)+y1+k0*x0 lambda x:k2*(x-x1) y0+y1+k0*x0+k1*(x1-x0)])
x1 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15,16,17,18,19,20,21], dtype=float)
y1 = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 98.36, 112.25, 126.14, 140.03,145,147,149,151,153,155])
y1 = np.flip(y1,0)
x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11, 12, 13, 14, 15,16,17,18,19,20,21], dtype=float)
y = np.array([5, 7, 9, 11, 13, 15, 28.92, 42.81, 56.7, 70.59, 84.47, 98.36, 112.25, 126.14, 140.03,145,147,149,151,153,155])
y = np.flip(y,0)
perr_min = np.inf
p_best = None
for n in range(100):
k = np.random.rand(7)*20
p , e = optimize.curve_fit(piecewise, x1, y1,p0=k)
perr = np.sum(np.abs(y1-piecewise(x1, *p)))
if(perr < perr_min):
perr_min = perr
p_best = p
xd = np.linspace(0, 21, 100)
plt.figure()
plt.plot(x1, y1, "o")
y_out = piecewise(xd, *p_best)
plt.plot(xd, y_out)
plt.show()
data with fit
Thanks.
A very simple method (without iteration, without initial guess) can solve this problem.
The method of calculus comes from page 30 of this paper : https://fr.scribd.com/document/380941024/Regression-par-morceaux-Piecewise-Regression-pdf (copy below).
The next figure shows the result :
The equation of the fitted function is :
Or equivalently :
H is the Heaviside function.
In addition, the details of the numerical calculus are given below :

Interpolate Table from txt

I'm fairly new to python programming and I'm trying to write a program that plots a graph from a txt file and interpolate the data later.
To get the data, I know that I can use:
precos = np.genfromtxt('Precos.txt', delimiter=',')
or
precos = sp.loadtxt("Precos.txt", delimiter=",")
And the data is something simple like:
1, 69.00
2, 69.00
3, 69.00
4, 69.00
5, 69.00
6, 69.00
7, 69.00
8, 79.00
9, 56.51
10, 56.51
I also know that I can use
plt.plot(precos)
To plot the graph but I don't how to interporlate. I saw that sp.interpolate.interp1d can help, but I am still unable to get my head around it.
----EDIT----
Ok, I tried a new approach, and now my code is almost done, but I still getting one error.
import scipy as sp
import numpy as np
import matplotlib.pyplot as plt
## Importando os dados numa matriz Nx2
M = sp.loadtxt('Precos.txt', delimiter=',')
## Construindo os vetores X e Y
x=np.zeros(len(M))
y=np.zeros(len(M))
for i in range(len(M)):
x[i] = M[i][0]
y[i] = M[i][1]
##Grahp Plot
plt.plot(x,y)
plt.title("Fone de Ouvido JBL com Microfone T100A - Fevereiro 2017")
plt.xlabel("Dia")
plt.ylabel("Preco em R$")
##Interpolation
F = sp.interpolate.interp1d(x,y)
xn = sp.arange(0,9,0.1)
yn = F(xn)
plt.plot(x, y, 'o', xn, yn, '-')
plt.show()
But now I getting: ValueError: A value in x_new is below the interpolation range.
Any ideas?
sp.interpolate.interp1d generates a function that you can reuse to interpolate the original data at intermediate points. Here's some specific code to breathe some life into it:
import numpy as np
from scipy import interpolate
data = np.array([[1, 69.00],
[2, 69.00],
[3, 69.00],
[4, 69.00],
[5, 69.00],
[6, 69.00],
[7, 69.00],
[8, 79.00],
[9, 56.51],
[10, 56.51]])
x = data[:,0]
y = data[:,1]
# Define an interpolation function
interpolation_function = interpolate.interp1d(x,y,kind='linear')
# Define intermediate points to interpolate at, and print the result
xi = [1, 1.5, 2.5, 9.5]
print(interpolation_function(xi))
gives the result:
[ 69. 69. 69. 56.51]

Categories