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
2
votes
1 answer

How can I make a 3D plot of a piecewise function of two variables?

I am trying to plot the following function in MATLAB: The goal is to stitch all of the different conditions into one graph to make an overall CDF graph. What I have tried so far is the…
James
  • 127
  • 2
  • 12
2
votes
1 answer

Piecewise-Linear Function in Gurobi, understanding the example

I am trying to implement a piecewise-linear objective in a gurobi optimization problem in python. When trying to get familiar with this method using the example from the gurobi web-site (http://examples.gurobi.com/production-scheduling/) I got…
James
  • 23
  • 5
2
votes
1 answer

lambdification of sympy piecewise function evaluates every expression

I'm lambdifying a sympy piecewise function trying to do something like this: f = Piecewise((1,(p > -1e-10) & (p < 1e-10)), (1/p, True)) g = lambdify(p,f,"numpy") While >>> f.subs(p,0) 1 I get >>>…
pyrogen
  • 209
  • 1
  • 7
2
votes
1 answer

Using scipy.optimize.curve_fit to fit a piecewise function

I am attempting to write a program that reads two sets data from a .csv file into arrays, and then fits it to a piecewise function. What's most important to me is that these fits are done simultaneously because they have the same parameters. This…
2
votes
1 answer

Integrating Piecewise with irrational exponent gives error

When trying to integrate a Piecewise function with irrational exponent, I stumbled upon the following problem: import sympy as sym x, y = sym.symbols(['x', 'y']) pdf = sym.Piecewise((0, x < 1), (x ** -2.5, x < 2), …
Georgy
  • 12,464
  • 7
  • 65
  • 73
2
votes
2 answers

Piecewise interpolation for entire data.frame R

I have a dataset from a sources that uses a special compression algorithm. Simply put, new measurements are recorded only when the change in slope (rate of change) is greater than a certain percentage (say 5%). However, for the analysis I'm…
Gautam
  • 2,597
  • 1
  • 28
  • 51
2
votes
1 answer

Matplotlib- plot piecewise Linear Function with three parts

I have some data that I want to fit with a piecewise linear function that has three parts. So something like this if there were two inflection points: Unfortunately, when I use the following code, I do not get the correct data, it instead looks…
kb3hts
  • 165
  • 1
  • 4
  • 9
2
votes
1 answer

Piecewise linear constraint in CPLEX API for MATLAB

I am a (almost) beginner with CPLEX and optimization. I am trying to set up an optimization problem in Matlab using the newly added feature of CPLEX (12.7.1), which enables the definition of piecewise linear (PWL) constraints. However, it is not…
2
votes
0 answers

Python - Most efficient way to define a piecewise function

I have a piecewise mathematical function that needs to be evaluated a lot of times, as a subfunction in quad and curve_fit routines. In trying to improve the performance of my program, I found that a lot of time was spent in evaluating conditions in…
SimonLR
  • 37
  • 1
  • 5
2
votes
1 answer

Tensorflow: Can't use tf.case with input argument

I need to create a variable epsilon_n that changes definition (and value) based on the current step. Since I have more than two cases, it seems that I can't use tf.cond . I am trying to use tf.case as follows: import tensorflow as…
D.Badawi
  • 175
  • 1
  • 13
2
votes
2 answers

Curvefitting optimization error when fitting piecewise linear function

I have some data in two arrays, which appears to have a break in it. I want my code to figure out where the break is with using piecewise in scipy. Here is what I have: from scipy import optimize import matplotlib.pyplot as plt import numpy as…
kb3hts
  • 165
  • 1
  • 4
  • 9
2
votes
1 answer

How do I code a piecewise mixed-model in lme in R?

I followed this example for running a piecewise mixed model using lmer, and it works very well. However, I am having trouble translating the model to lme because I need to deal with heteroscedasticity, and lmer doesn’t have that ability. Code to…
2
votes
2 answers

Piecewise list comprehensions in python

What is the easiest/most elegant way to do the following in python: def piecewiseProperty(aList): result = [] valueTrue = 50 valueFalse = 10 for x in aList: if hasProperty(x): result.append(valueTrue) …
user
  • 7,123
  • 7
  • 48
  • 90
2
votes
0 answers

MATLAB inline function if statement

I'm trying to define a function as follows: if nn == 0 tau(nn) = 0 else tau(nn) = crazy_function(nn) end However, I want to do it in a single line. I tried tau = @(nn) crazy_function(nn).*(nn~=0) + 0.*(nn==0); But this doesn't work because…
user1936752
  • 756
  • 1
  • 9
  • 25
2
votes
1 answer

Python: Piecewise function integration error: "TypeError: cannot determine truth value of ..."

This code runs correctly: import sympy as sp def xon (ton, t): return (t-ton)/5 xonInt = sp.integrate (xon(ton, t),t) print xonInt But when the function becomes piecewise, e.g.: import sympy as sp def xon (ton, t): if ton <= t: …
user_185051
  • 426
  • 5
  • 19