par(mfrow=c(2,2)) #set up graphs in 2x2 matrix
fx=function(x){3*(x+1)*(x-4)*(x+5)}
s=seq(-10,10,by=.01)
a=fx(s)
plot(a~s,type="l")
fy=function(y){y^2}
a=fy(s)
plot(a~s,type="l")
fz=function(z){log(z)}
s=seq(0,10,by=.01)
a=fz(s)
plot(a~s,type="l")
fqplus=function(qplus){sqrt(qplus)}
fqneg=function(qneg){-sqrt(qneg)}
s=seq(0,10,by=.01)
a=fqplus(s)
b=fqneg(s)
fin=c(a,b)
s2=c(s,s)
plot(fin~s2,type="p")
# Numerical Derivatives
library(rootSolve)
fx=function(x){3*(x+1)*(x-4)*(x+5)}
s=seq(-10,10,by=.01)
a=fx(s)
plot(a~s,type="l")
#find the y intercept
yint=fx(0)
#find the x intercept
xint=uniroot(fx,c(-100,100))
points(xint$root,yint,col="red",pch=21)
xint$root
## [1] -5
yint
## [1] -60
#take the symbolic derivative of log(x)
library(Deriv) #references add-in
## Warning: package 'Deriv' was built under R version 4.3.3
myf1=function(x)sin(x)
Deriv(myf1)
## function (x)
## cos(x)
#take the symbolic derivative of 1/x
myf2=function(x)1/x
ans=Deriv(myf2)
ans
## function (x)
## -(1/x^2)
#evaluate the derivative of 1/x at x=3
ans(3)
## [1] -0.1111111
#Power Rule, px^(p-1)
myf1=function(x)x**n
Deriv(myf1)
## function (x)
## n * x^(n - 1)
#Addition rule, dx(f)+dx(g)
myf2=function(x)3*x+(3*x^2+1)
Deriv(myf2)
## function (x)
## 3 + 6 * x
#Exponential Function, Identity
myf3=function(x)exp(x)
Deriv(myf3)
## function (x)
## exp(x)
#Logarithm Function, 1/x
myf4=function(x)log(x)
Deriv(myf4)
## function (x)
## 1/x
#Product Rule, f'g+g'f
myf5=function(x)3*x*(3*x^2+1)
Deriv(myf5)
## function (x)
## 3 * (1 + 9 * x^2)
#Quotient rule f/g, (f'g-g'f)/g^2
myf6=function(x)3*x/(3*x^2+1)
Deriv(myf6)
## function (x)
## {
## .e1 <- x^2
## .e2 <- 1 + 3 * .e1
## 3 * ((1 - 6 * (.e1/.e2))/.e2)
## }
#Chain rule, f'u*g'x
myf7=function(x)3*(x^2+2)^2
Deriv(myf7)
## function (x)
## 12 * (x * (2 + x^2))
#f(x) = x^3 – 3*x^2 – 9*x + 5 for –2 ≤ x ≤ 6
library(mosaic)
## Warning: package 'mosaic' was built under R version 4.3.3
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
library(Deriv)
library(rootSolve)
fx=function(x){x^3-3*x^2-9*x + 5}
s=seq(-2,4,by=.1)
a=fx(s)
data=cbind(s,a)
plot(data, type="l", main="Solve for Max between -2 and 4")
min(a) #-22
## [1] -22
max(a) #10
## [1] 10
first=Deriv(fx)
a=uniroot.all(first,c(-2,4))
a #-1 and 3
## [1] -1.000171 3.000171
c(first(-1), first(3))
## [1] 0 0
#R Base Function, integrate
fx=function(x)-2
integrate(Vectorize(fx),1,4) #Some functions puke w/o vectorize
## -6 with absolute error < 6.7e-14
fx=function(x)exp(x^2+x)
integrate(fx,1,2)
## 86.83404 with absolute error < 9.6e-13
#Constant Multiple Rule, int(k*f)=k*int(f)+C
library(mosaic)
library(mosaicCalc)
## Warning: package 'mosaicCalc' was built under R version 4.3.3
## Loading required package: mosaicCore
## Warning: package 'mosaicCore' was built under R version 4.3.3
##
## Attaching package: 'mosaicCore'
## The following objects are masked from 'package:dplyr':
##
## count, tally
##
## Attaching package: 'mosaicCalc'
## The following object is masked from 'package:stats':
##
## D
antiD(5~x)
## function (x, C = 0)
## 5 * x + C
#Sum Rule (separable)
antiD(x-2+x^2~x)
## function (x, C = 0)
## (2 * x^3 + 3 * x^2 - 12 * x)/6 + C
#Power Rule, x^n -> (x^(n+1)/(n+1)
antiD(x^3~x)
## function (x, C = 0)
## x^4/4 + C
#Exp(x)=>exp(x)+C
antiD(exp(x)~x)
## function (x, C = 0)
## exp(x) + C
#1/x=>ln|x|+C
antiD(1/x~x)
## function (x, C = 0)
## log(x) + C
library(rootSolve) #for actual roots
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.3.3
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
myf1=function(x) x-(x^2+x-2)/(2*x+1) #Problem 5, page 173 xo-equation/d(equation)
myf2=function(x) x-(x^2-2)/(2*x) #Problem 6, page 173
myf3=function(x) x-(x^3-x^2+x-1)/(3*x^2-2*x+1) #Problem 8, page 173
temp1=temp2=temp3=rep(0,6) #initialize
temp1[1]=0; temp2[1]=1.5; temp3[1]=1 #initialize start
#Loop
for (i in 2:6){temp1[i]=myf1(temp1[i-1]);temp2[i]=myf2(temp2[i-1]);temp3[i]=myf3(temp3[i-1])}
#Evaluate actual
act1=round(uniroot.all(function(x) x^2+x-2, c(-1000,1000)),2)
act2=round(uniroot.all(function(x) x^2-2, c(-1000,1000)),2)
act3=round(uniroot.all(function(x) x^3-x^2+x-1, c(-1000,1000)),2)
#Combine estimated and actual
temp1=c(temp1, act1); temp2=c(temp2, act2); temp3=c(temp3,act3, NA)
tab=rbind(temp1,temp2,temp3)
rownames(tab)=c('#5','#6', '#8')
colnames(tab)=c('x0', 'x1', 'x2', 'x3','x4', 'x5', 'Actual Root1', 'Actual Root2')
tab%>%kbl()%>%kable_classic(html_font='Cambria')
| x0 | x1 | x2 | x3 | x4 | x5 | Actual Root1 | Actual Root2 | |
|---|---|---|---|---|---|---|---|---|
| #5 | 0.0 | 2.000000 | 1.200000 | 1.011765 | 1.000046 | 1.000000 | -2.00 | 1.00 |
| #6 | 1.5 | 1.416667 | 1.414216 | 1.414214 | 1.414214 | 1.414214 | -1.41 | 1.41 |
| #8 | 1.0 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.000000 | 1.00 | NA |