Questions tagged [piecewise]

"piecewise" may refer to a piecewise function, a piecewise interpolation or a piecewise regression / smoothing.

Defining a piecewise function normally requires writing if ... else ... clause.

Interpolation is inherently piecewise.

Piecewise regression often refers to piecewise linear regression, or segmented regression.

367 questions
5
votes
1 answer

Solving and plotting a piecewise ODE

I have a function dφ/dt = γ - F(φ) (where F(φ) -- a is 2π-periodic function) and the graph of the function F(φ). I need to create a program that outputs 6 plots of φ(t) for different values of γ (γ = 0.1, 0.5, 0.95, 1.05, 2, 5), and t∈[0,100]. Here…
james
  • 109
  • 6
5
votes
2 answers

How to convert a spline fit into a piecewise function?

Let's say I have import numpy as np from scipy.interpolate import UnivariateSpline # "true" data; I don't know this function x = np.linspace(0, 100, 1000) d = np.sin(x * 0.5) + 2 + np.cos(x * 0.1) # sample data; that's what I actually…
Cleb
  • 25,102
  • 20
  • 116
  • 151
5
votes
3 answers

Piecewise regression with a quadratic polynomial and a straight line joining smoothly at a break point

I want to fit a piecewise linear regression with one break point xt, such that for x < xt we have a quadratic polynomial and for x >= xt we have a straight line. Two pieces should join smoothly, with continuity up to 1st derivative at xt. Here's…
tehm0n
  • 79
  • 1
  • 7
5
votes
2 answers

Piecewise regression with a straight line and a horizontal line joining at a break point

I want to do a piecewise linear regression with one break point, where the 2nd half of the regression line has slope = 0. There are examples of how to do a piecewise linear regression, such as here. The problem I'm having is I'm not clear how to fix…
5
votes
1 answer

Piecewise functions on Numpy Arrays

What is efficient (speed) way to apply Piecewise functions on Numpy Array? Say, for example, Piecewise functions are like For (1) : x<=2 f(x) = 2*x + x^2 (2) : x>2 f(x) = -(x^2 + 2) Here's what I did. data = np.random.random_integers(5,…
user2517372
5
votes
2 answers

Fitting piecewise function in Python

I'm trying to fit a piecewise defined function to a data set in Python. I've searched for quite a while now, but I haven't found an answer whether it is possible or not. To get an impression of what I am trying to do, look at the following example…
cass
  • 309
  • 1
  • 5
  • 14
4
votes
3 answers

How to make a piecewise linear fit in Python with some constant pieces?

I'm trying to make a piecewise linear fit consisting of 3 pieces whereof the first and last pieces are constant. As you can see in this figure don't get the expected fit, since the fit doesn't capture the 3 linear pieces clearly visual from the…
afd
  • 65
  • 6
4
votes
3 answers

Difficulty fitting piecewise linear data in R

I have the following data (cost of a product vs. time) that looks like the following: annum <- c(1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919) cost <- c(0.0000, 18.6140, 92.1278,…
4
votes
0 answers

python numpy piecewise linear fit not robust?

I have applied the elegant solution to piecewise linear fitting as given in How to apply piecewise linear fit in Python?. As shown in the figure (source code is given below), I do get as a result the orange line, whereas I expect something like the…
itmatters
  • 665
  • 6
  • 8
4
votes
1 answer

how to write piecewise linear objective function in Pyomo

I want to create a linear model in Pyomo that has piecewise linear function in its objective function. I managed to create the following code: model = AbstractModel() breakpoints = [-5,0,5] values = [10,0, 10] model.X = Var(bounds=(-5,5)) model.Y=…
kfurmanska
  • 51
  • 4
4
votes
1 answer

Create and plot a piecewise function in Octave

So I want to plot this function for -1 First I created the piecewise function function x = pieceWise(t) if t >= 0 & t <3 x = exp(-t); else x = 0; endif then called and plot it here x = linspace(-1,5,1000); y =…
Tanaka
  • 53
  • 1
  • 4
4
votes
1 answer

Error with sympy.lambdify for piecewise functions and numpy module

In sympy 0.7.6, I had no troubles with the following code for both the modules='sympy' and the modules='numpy' options. Now with sympy v0.1, the evaluation with modules='numpy' raise a ZeroDivisionError: import sympy x, y = sympy.symbols(['x',…
Toht
  • 53
  • 6
4
votes
2 answers

Piecewise regresion Python

Hi I'm trying to figure out how to fit those values with a piecewise linear function. I have read this question but I can't get forward (How to apply piecewise linear fit in Python? ). In this example is show how to implement a piecewise function…
4
votes
0 answers

Shifted piecewise

I want to have a series of piecewise functions with each being a shifted copy of the base function, but I do not know how to do that in sympy. Here is an example: from sympy import Symbol from sympy import Piecewise from sympy import And x =…
Student4K
  • 916
  • 2
  • 9
  • 20
4
votes
0 answers

piecewise linear regression python: arbitrary amount of knots

I have an experimental data, which is piecewise continuous, and each part should fit linearly. However, I would like to fit it without knowing where exactly are the knots (so the points where the slope is changing), since its not easy to determine…
Antonio
  • 325
  • 1
  • 3
  • 9
1
2
3
24 25