source("http://macosa.dima.unige.it/r.R") # If I have not already loaded the library ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------S12Regression. Splines. Curve fitting. Moving average (or mean).Linear, quadratic and cubicregression: the linear function with graph for a given point (p,q) and the generic linear, quadratic and cubic functions that "best" approximate some experimental data (x,y). Commands:regression(x,y,p,q),regression1(x,y),regression2(x,y),regression3(x,y)If the points are 2, 3 or 4 regression1, regression2 and regression3 find exactly the curve. In these cases we have aninterpolation. We also see howsplines(approximating function graphs or curves) can be calculated (typical instrument to obtain a smooth curve in agreement with a finite number of digital information). Also the splines are examples ofinterpolation.For a more complete method of studynglinear regression(and the command LR) see here.If the "x" are too large [for example x=c(2007,2012,…)] an error message may appear. In this case you can modify x (x-2000) and find the regression rB (of x-2000), and then modify it [take r(x) = rB(x-2000)] x = c(-0.2, 1.3, 1.7, 3.2, 4.1, 4.6) y = c(9.1, 17.5, 20.7, 25.4, 35.7, 51.2) Plane(-1,6,-10,70); POINT(x,y,1)regression(x,y, 0,0); POINT(0,0,"blue")#9.829 * x f = function(x) 9.829 * x; graph(f,-1,6, "red")regression1(x,y)#7.628 * x + 7.911 g = function(x) 7.628 * x + 7.911; graph(g,-1,6, "blue")regression2(x,y)#1.37 * x^2 + 1.38 * x + 11.2 h = function(x) 1.37 * x^2 + 1.38 * x + 11.2; graph(h,-1,6, "violet")regression3(x,y)#1.21 * x^3 + -6.3 * x^2 + 11.9 * x + 11.6 k = function(x) 1.21 * x^3 + -6.3 * x^2 + 11.9 * x + 11.6; graph(k,-1,6, "green") POINT(0,0,"blue"); POINT(x,y,1)# On the right, the curve that goesexactlyfor (-1,-10),(0,-1),(1,-2),(2,5)Plane(-2,3,-20,20) POINT(-1,-10, 1); POINT(0,-1,"red"); POINT(1,-2,"green"); POINT(2,5,"blue") regression3( c(-1,0,1,2), c(-10,-1,-2,5) )#3 * x^3 + -5 * x^2 + 1 * x + -1 f = function(x) 3 * x^3 + -5 * x^2 + 1 * x + -1 graph(f,-2,3, 6) POINT(-1,-10, 1); POINT(0,-1,"red"); POINT(1,-2,"green"); POINT(2,5,"blue")# It is the same function that we obtain withxy_4(c(-1,0,1,2), c(-10,-1,-2,5)) [see]# Thecubicsplinethat interpolates the points x,y (see):x = c(-0.2, 1.3, 1.7, 3.2, 4.1, 4.6) y = c(9.1, 17.5, 20.7, 25.4, 35.7, 51.2) BF=3; HF=2.5; Plane(-1,5,-5,65); POINT(x,y,1) SF =splinefun(x,y); graph2(SF,-0.2,4.6, "red") POINT(x,y-10,1); SF1=splinefun(x,y-10); graph2(SF1,-1,5,"blue") # The "extended" (blue) curve that passes by the same points, lowered by 10 (the inter- # polation used to find the curve outside the known points is called extrapolation)# Obviously if I want to find a value of y I can use splinefun(x,y). For example: splinefun(x,y-10)(0.5) # or: SF1(0.5) # 1.383585 POINT(0.5, splinefun(x,y-10)(0.5), "red")#splinefunD(…, …, N, col) plots the graph of the Nth derivative: BF=4.5; HF=3; Plane(-1,5, -30,65) graph2(SF,-1,5, "red"); POINT(x,y,1) splinefunD(x,y,1,"blue"); splinefunD(x,y,2,"seagreen"); splinefunD(x,y,3,"orange") text(3.8,5,"D1"); text(2.7,-5,"D2"); text(0.5,-14,"D3")# D1, D2, D3: branches of parabolas, straight lines, horizontal lines. # If you have some points of a closed curve you can approximate it with the command #splineC(x,y,ColBorder,ColInside), orsplinCif you want a thin-stroke curve. If you # choose NULL as internal color the figure is transparent.BF=3; HF=3; PLANE(-2,8, -2,8) u=c( -1, 4, 7,6,3,0) v=c(-1.5,0,-1,6,7,5) splineC(u,v,"red",NULL); POINT(u,v,"seagreen") PLANE(-2,8, -2,8); splinC(u,v,"blue","yellow") # The points used to trace the curve are in splineCx(x,y) and splineCy(x,y) length(splineCx(u,v))#104 c( splineCx(u,v)[1],splineCy(u,v)[1] )#-1.0 -1.5 c( splineCx(u,v)[2],splineCy(u,v)[2] )#-0.8442124 -1.6377163 # If you want to have approximately theareaof the closed curve you can useareaPol:areaPol(splineCx(u,v),splineCy(u,v))#53.88609#splineL(x,y,ColBorder), andsplinL, do not close the curve. BF=1.8; HF=BF*340/270 BOXW(0,270,0,340) x=c(22,34,38, 35, 7, 2, 15, 52,114,194,221,233,231,228,227,243,259, 255,233,239,235,238,229,226,236,217,201,154,157) y=c( 4,55,92,129,187,224,278,315,331,307,271,230,216,211,205,174.5,144, 131,130,119,113,109,102, 92, 68, 59, 55, 57, 4) splineL(x,y,"pink"); splinL(x,y,"brown") # Point(x,y, "red"); gridVC(seq(0,270,50),"blue"); gridHC(seq(0,340,50),"blue") # If we want to approximate points with a curve y = A·exp(B·x) we can use regression1 # after transforming y-coordinates by the logarithmic function. If we want approximate # them with a polynomial curve we can transform x-coordinates too: see here # # See here to see other methods ofcurve fitting(with the commandsF_RUN1,F_RUN2, # ...F_RUN6).##An exampleof use of regression.

# A ball is allowed to fall freely under gravity. The image on the # right was captured with a stroboscopic flash at 30 flashes per # second. I look for the curve y=c2*x^2+c1*x+c0 that passes through # the points and I take g = 2*c2 (s = s0+v0*t+a/2*t^2, a = g; the first # flash does not correspond exactly to the release of the ball; so the # low is not s = a/2*t^2). How much is g? # The distance (in meters) of the ball from the initial position is # approximately: k = c(0,0.03,0.06,0.09,0.14,0.20,0.27,0.35,0.44, 0.55,0.67,0.80,0.94,1.08,1.24,1.41,1.60,1.77) length(k) # 18 number of distances t = 1/30*(0:17) # the times (in seconds) regression2(t,k) # the regression of 2nd order # 4.92 * x^2 + 0.345 * x + 0.00675 # I can take 2*4.9 = 9.8 as an approximation of g. |