Find a function given two points and informaton about derivatives

Suppose a polynomial function passes through the points (x,y) = (1,6) and (3,2) and has a local maximum at the first point and a local minimum at the second point.

The problem is to find a, b, c, and d for a polynomial function of least degree that meets those conditions and where f(x) = ax^4 + bx^3 +c*x^2+ d

A <- c(1,1,1,1,81,27,9,1,4,3,2,0,108,27,6,0)
AM <- matrix(A, nrow=4, ncol=4, byrow=TRUE)
rhs = c(6,2,0,0)
AM
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]   81   27    9    1
## [3,]    4    3    2    0
## [4,]  108   27    6    0

AM, above represents the coefficient matrix for the the function evaluated at 1, and 3 and the derivative of the function evaluated at 1,3.

f(1) = a + b + c + d = 6
f(3) = 27a + 9b + 3c + d = 2
f’(1) = 3a + 2b + c + 0d = 0
f’(3) = 27a + 6b + c + 0d = 0

rhs (right hand side is the right vector)

rhs
## [1] 6 2 0 0

And Rhs is the right hand side of the matrix equation As = rhs, or the evaluations of the four functions whose coefficients are in AM.

s <-solve(AM,rhs)
s
## [1]  0.375 -2.000  2.250  5.375

And s, above, translated to a,b,c,d, below is ourproposed solution vector.

a=s[1]
b=s[2]
c=s[3]
d=s[4]
# Solution vector
c(a,b,c,d)
## [1]  0.375 -2.000  2.250  5.375

No to check our work, evaluate the function and its first and second derivatives at x = 1 and 3 to see if local minima anmaxima tests are met.

x=c(1,3)
#Function f(x)
fx= a*x^4 +b*x^3+c*x^2+d
x
## [1] 1 3
fx
## [1] 6 2
# Derivative f'(x)
fpx = 4*a*x^3 +3*b*x^2+2*c*x
x
## [1] 1 3
fpx
## [1] 0 0
# Second Derivative f''(x)
fppx = 3*4*a*x^2 +2*3*b*x+2*c
x
## [1] 1 3
fppx
## [1] -3  9

Thus, from above, it can be seen that at 1 and 3 f(x) is 6 and 2 respectively, the first derivative, f’(x)=fpx, is 0 in both cases and the second derivative, f’’(x) = fppx, is negative and positive respectively.

So the function, f(x) = (3/8)x^4 + (-2)x^3 + (9/4)*x^2+ (43/8) meets our conditions.

Note that the x term is missing from the polynomial but it didn’t seem to matter and also that the matrix inverse calculation could have been calculated by augmenting rhs to AM and then redcing the augmented matrix to row echelon form.

Question: Have we shown that this function is a function of MINIMAL degree that meets our conditions. Let’s consider a cubic function and repeat the work. The problem is to find a, b, c, and d for a polynomial function of least degree that meets those conditions and where f(x) = ax^4 + bx^3 +c*x^2+ d

A <- c(1,1,1,1,27,9,3,1,3,2,1,0,27,6,1,0)
AM <- matrix(A, nrow=4, ncol=4, byrow=TRUE)
rhs = c(6,2,0,0)
AM
##      [,1] [,2] [,3] [,4]
## [1,]    1    1    1    1
## [2,]   27    9    3    1
## [3,]    3    2    1    0
## [4,]   27    6    1    0

AM, above represents the coefficiemt matrix for the the function evaluated at 1, and 3 and the derivative of the function evaluated at 1,3.

rhs
## [1] 6 2 0 0

And Rhs is the right hand side of the matrix equation As = rhs, or the evaluations of the four functions whose coefficients are in AM.

s <-solve(AM,rhs)
s
## [1]  1 -6  9  2

And s, above, translated to a,b,c,d, below is ourproposed solution vector.

a=s[1]
b=s[2]
c=s[3]
d=s[4]
# Solution vector
c(a,b,c,d)
## [1]  1 -6  9  2

No to check our work, evaluate the function and its first and second derivatives at x = 1 and 3 to see if local minima anmaxima tests are met.

x=c(1,3)
#Function f(x)
fx= a*x^3 +b*x^2+c*x+d
x
## [1] 1 3
fx
## [1] 6 2
# Derivative f'(x)
fpx = 3*a*x^2 +2*b*x+c
x
## [1] 1 3
fpx
## [1] -1.776357e-15  3.552714e-15
# Second Derivative f''(x)
fppx = 2*3*a*x +2*b
x
## [1] 1 3
fppx
## [1] -6  6

Thus, from above, it can be seen that at 1 and 3 f(x) is 6 and 2 respectively, the first derivative, f’(x)=fpx, is 0 in both cases and the second derivative, f’’(x) = fppx, is negative and positive respectively.

So the function, f(x) = x^3 -6x^2 + 9x + 2 meets our conditions.

A quadratic function has at most 2-1 extrema; so we are DONE

Question: Did we get lucky about the second derivatives coming our negative and positive–which is what we wanted. No we didn’t get lucky. We were given two points, both satisfying f’(x)=0 . One was larger than the the other. If there are a maximum of two extrema in a cubic polynomial function, then the two points cannot both be minima or maxima or else there would be more extrema in between. So the problem reduces to “Find a cubic polynomial function that has extrema at (1,6) and (3,2)” since it can have only two extrema at most, (3,2) is the min and (1,6) is the max. Easy; I mean I got it in two seconds but I thought I was lucky with cubic and f’’ test!

Finally let’s see what our function looks like on a graph:

Generate a bunch (51) of points on the function between x = 0 and 5

x = seq(0, 5, by= .1 )
y= x^3 -6*x^2 + 9*x + 2

Plot the graph to visually check our results.

plot(x,y, type = "l",main = ("f(x) = x^3 -6x^2 + 9x + 2"))