piecewise

Matplotlib- plot piecewise Linear Function with three parts

空扰寡人 提交于 2020-01-15 03:27:49
问题 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 like this Anyone know what's wrong? Thanks! def piecewise_linear2(x, x0, y0, k1, k2, k3): return np.piecewise(x, [x < x0], [lambda x:k1*x + y0-k1*x0, lambda x:k2*x + y0-k2*x0, lambda x:k3*x + y0-k3*x0]) fit_days = np.array([1786,1852,2067,2142,2143

Broken stick (or piecewise) regression with 2 breakpoints

孤街醉人 提交于 2020-01-04 05:47:08
问题 I want to estimate two breakpoints of a function with the next data: df = data.frame (x = 1:180, y = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 2, 2, 4, 2, 2, 3, 2, 1, 2,0, 1, 0, 1, 4, 0, 1, 2, 3, 1, 1, 1, 0, 2, 0, 3, 2, 1, 1, 1, 1, 5, 4, 2, 1, 0, 2, 1, 1, 2, 0, 0, 2, 2, 1, 1, 1, 0, 0, 0, 0, 2, 3, 0, 3, 2, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

piecewise function fitting with nls() in R

大憨熊 提交于 2019-12-19 02:46:11
问题 I am trying to fit a two-part line to data. Here's some sample data: x<-c(0.00101959664756622, 0.001929220749155, 0.00165657261751726, 0.00182514724375389, 0.00161532360585458, 0.00126991061099209, 0.00149545009309177, 0.000816386510029308, 0.00164402569283353, 0.00128029006251656, 0.00206892841921455, 0.00132378793976235, 0.000953143467154676, 0.00272964503695939, 0.00169743839571702, 0.00286411493120396, 0.0016464862337286, 0.00155672067449593, 0.000878271561566836, 0.00195872573138819, 0

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

南楼画角 提交于 2019-12-18 16:59:38
问题 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 picture of what it may look like: I have parametrize my piecewise regression function as: where a , b , c and xt are parameters to be estimated. I want to compare this model with a quadratic polynomial regression over the whole range in terms of

Constructing piecewise symbolic function in Matlab

大城市里の小女人 提交于 2019-12-18 03:31:08
问题 I am trying to generate a piecewise symbolic function in Matlab. The reason it has to be symbolic is I want to be able to integrate/differentiate the function afterwards and/or insert actual values. I have the following function: x^3/6 -> 0 < x <= 1 (1/6)*(-3*x^3+12*x^2-12x+4) -> 1 < x <= 2 (1/6)*(3*x^3-24*x^2+60x-44) -> 2 < x <= 3 (1/6)*(4-x)^3 -> 3 < x <= 4 0 -> otherwise For example, I want to put this function in a variable (let's say f) and then call int(diff(f, 1)^2, x, 0, 4) % numbers

Fitting piecewise function in Python

霸气de小男生 提交于 2019-12-17 20:23:43
问题 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 (which is not working for me). Here I'm trying to fit a shifted absolute value function (f(x) = |x-p|) to a dataset with p as the fit parameter. import scipy.optimize as so import numpy as np def fitfunc(x,p): if x>p: return x-p else: return -(x-p)

How to draw graph of Gauss function?

风流意气都作罢 提交于 2019-12-12 15:08:28
问题 Gauss function has an infinite number of jump discontinuities at x = 1/n, for positive integers. I want to draw diagram of Gauss function. Using Maxima cas I can draw it with simple command : f(x):= 1/x - floor(1/x); plot2d(f(x),[x,0,1]); but the result is not good ( near x=0 it should be like here) Also Maxima claims: plot2d: expression evaluates to non-numeric value somewhere in plotting range. I can define picewise function ( jump discontinuities at x = 1/n, for positive integers ) so I

How do I perform statements on the dependent variable of a graph in MATLAB?

Deadly 提交于 2019-12-12 06:35:46
问题 I would like to know how to grab a specific number from an interval to test it and later be able to built a different functions under one graph. For example (in this case the 'x' variable), x 0:.5:5; Ids=ones(x); figure;hold on; for n = 1:5 if(x < 3.0) %problem here Ids(n) = plot(x,x.^x); else if (x > 4.0) %and here Ids(n) = plot(x,-x.^x); end end end EDIT What I really want to do in MATLAB is to be able to do the following piecewise function: y(x) = { 0 (t - 5) < 0 { (t - 5)*(t - x) x < (t -

piecewise function with 3d plot

送分小仙女□ 提交于 2019-12-11 19:58:48
问题 I am having trouble getting np.piecewise to work for multiple dimensional plotting due to broadcast errors. Does anyone have any manner to get around this? Here is what I have in a simplified executable script: import numpy as np from pylab import * import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d, Axes3D num_steps = 100 x_arr = np.linspace(0,100, num_steps) y_arr = np.linspace(0,20, num_steps) def zfunc(x, y): return np.piecewise(x, [x>=500, x<500], [x, -x]) x,y = np

Matlab drawing points and show values

喜欢而已 提交于 2019-12-11 18:36:16
问题 I have a simple plot question. On x axis, the values are K, say from 2 to 12, discrete. On y axis, the values are C, say from 1 to 10, discrete. My function is piecewise: K if K<2C; K+2C if K>=2C; I want to show the values at points (K,C): (1,1) Show as 1 (1,2) Show as 1 (2,1) Show as 4 (2,2) Show as 2 ect. How would I do that? Many thanks, Casper 回答1: You can use ndgrid to create K and C: [K C] = ndgrid(2:12,1:10); then use logical indexing to calculate the separate parts: z=zeros(11,10);