Write a program to compute the derivative of \(f\left( x \right) ={ x }^{ 3 }\quad +\quad 2\cdot { x }^{ 2 }\) at any value of x.
To find the derivative, we will use the following:
\(f^{ ' }\left( x \right) ={ limit }_{ h\rightarrow 0 }\frac { f\left( x+h \right) -f\left( x \right) }{ h }\)
To simulate the limit we will take a suitable small amount for \({h= 10 }^{ -10 }\).
First we will write the function f(x) as indicated above.
fx <- function(x){x^3 +2*x^2}
We will now write program that will take an x value and calculate the value of the derivative of the function f(x) at this value.
derivative_x <-function(x){
h <- 10^(-10)
return((fx(x+h)-fx(x))/h)
}
We will now test this function by systematically calcuated the derivatives using this function and comparing the results with evaluating the function \(f^{ ' }\left( x \right) =3\cdot { x }^{ 2 }+4\cdot x\).
d_fx <- function(x){3*x^2 + 4*x} #derivative of function f(x)
allowed_e <- 10^(-3) #acceptable error
x1 <- runif(50, -20.05, 20.05) #random value of x between -20.05 and 20.05
l_dx <-c()
d_dx <-c()
diff <-c()
d_log <-c()
for (x_i in x1){
l_dx <- c(l_dx,derivative_x(x_i))
d_dx <- c(d_dx, d_fx(x_i))
diff <- c(diff, derivative_x(x_i)-d_fx(x_i))
if(abs(derivative_x(x_i)-d_fx(x_i)) <= allowed_e){
d_log<-c(d_log,TRUE)
}else{
d_log<-c(d_log,FALSE)
}
}
df <- cbind.data.frame(x1, l_dx, d_dx, diff, d_log)
df
## x1 l_dx d_dx diff d_log
## 1 11.61980443 451.5391083 451.53878249 3.258319e-04 TRUE
## 2 -17.92405614 892.1051631 892.11914119 -1.397811e-02 FALSE
## 3 -13.37831210 483.4259926 483.42445556 1.537010e-03 FALSE
## 4 6.04030517 133.6167088 133.61708014 -3.713337e-04 TRUE
## 5 -1.91697480 3.3564795 3.35647795 1.592760e-06 TRUE
## 6 -4.41437101 40.8025613 40.80253013 3.120358e-05 TRUE
## 7 -11.42870317 346.1286724 346.13095566 -2.283271e-03 FALSE
## 8 1.54552406 13.3480249 13.34803004 -5.135093e-06 TRUE
## 9 -9.76519989 247.0176241 247.01658699 1.037145e-03 FALSE
## 10 -3.89599817 29.9524316 29.95241252 1.912770e-05 TRUE
## 11 8.65707611 259.4629223 259.46320461 -2.823529e-04 TRUE
## 12 -6.10399000 87.3600925 87.36012164 -2.914763e-05 TRUE
## 13 -4.37155121 39.8452471 39.84517510 7.200374e-05 TRUE
## 14 0.12722464 0.5574569 0.55745688 5.246703e-08 TRUE
## 15 3.04184566 39.9258226 39.92585775 -3.509962e-05 TRUE
## 16 13.24231391 579.0434443 579.04588856 -2.444251e-03 FALSE
## 17 -4.21378979 36.4128283 36.41291408 -8.577491e-05 TRUE
## 18 14.38240856 678.0919648 678.09066180 1.303000e-03 FALSE
## 19 17.63617290 1003.6274034 1003.64847568 -2.107227e-02 FALSE
## 20 -4.08877299 33.7990969 33.79910168 -4.825863e-06 TRUE
## 21 -5.34364822 64.2893383 64.28913598 2.023136e-04 TRUE
## 22 2.40237029 26.9236367 26.92363027 6.381746e-06 TRUE
## 23 -10.80669780 307.1272658 307.12736080 -9.496068e-05 TRUE
## 24 13.45638033 597.0514394 597.04803583 3.403569e-03 FALSE
## 25 14.96435970 731.6566553 731.65362286 3.032397e-03 FALSE
## 26 14.53144904 691.6161510 691.61482961 1.321403e-03 FALSE
## 27 -5.99859420 83.9551717 83.95502034 1.513612e-04 TRUE
## 28 -18.60142975 963.6096365 963.63384756 -2.421103e-02 FALSE
## 29 -15.75712149 681.8299880 681.83214705 -2.159027e-03 FALSE
## 30 10.65256460 383.0427886 383.04165586 1.132743e-03 FALSE
## 31 5.28669467 104.9943421 104.99420045 1.416574e-04 TRUE
## 32 4.25630195 71.3734494 71.37352672 -7.734910e-05 TRUE
## 33 13.37429521 590.1119948 590.11249772 -5.028941e-04 TRUE
## 34 11.28214808 426.9895726 426.98918843 3.841580e-04 TRUE
## 35 -10.00699252 260.3917437 260.39172762 1.609961e-05 TRUE
## 36 -9.42221106 228.6446943 228.64533963 -6.453390e-04 TRUE
## 37 8.61832910 257.3005986 257.30010588 4.927276e-04 TRUE
## 38 10.64919765 382.8131412 382.81302212 1.190682e-04 TRUE
## 39 -1.10639780 -0.7532397 -0.75324293 3.242108e-06 TRUE
## 40 17.94108528 1037.3878467 1037.41196426 -2.411753e-02 FALSE
## 41 2.04424366 20.7137418 20.71377113 -2.929413e-05 TRUE
## 42 3.83179016 59.3750826 59.37500806 7.456748e-05 TRUE
## 43 -6.40663183 97.5086323 97.50826682 3.654624e-04 TRUE
## 44 -0.01770582 -0.0698828 -0.06988279 -8.978574e-10 TRUE
## 45 -17.09758060 808.5771697 808.59146520 -1.429554e-02 FALSE
## 46 -15.47263351 656.3186616 656.31662925 2.032388e-03 FALSE
## 47 19.71204345 1244.5161701 1244.54214467 -2.597456e-02 FALSE
## 48 -12.78470897 439.2063602 439.20751448 -1.154313e-03 FALSE
## 49 -16.90010122 789.2231224 789.23985906 -1.673665e-02 FALSE
## 50 13.10890181 567.9703463 567.96552702 4.819288e-03 FALSE
We will now write a program to calculate the area under the curve for the function defined as follow:
\(f\left( x \right) =3\cdot { x }^{ 2 }+4\cdot x\)
To evaluate the area, we will simulate the integral of the function by calcuating areas of rectangle for a very small increment of x.
This is actually represented by the formula denoted below:
\(\int _{ a }^{ b }{ f\left( x \right) } =\underset { n\rightarrow \infty }{ lim } \sum _{ i=1 }^{ n }{ f({ x }_{ i })\Delta x }\)
We have already coded the function in the problem above and denoted d_fx. We will now write a program to calculate the area under the curve. The smalles value of \(\Delta x\) that we could use is \({ 10 }^{ -5 }\), smaller value proved to give performance issue.
area_fx <- function(x1,x2){
s <- c()
delta_x <-10^(-5)
x_seq <- seq(x1,x2,delta_x)
x_seq <- x_seq[ x_seq != x2 ]
for (x_i in x_seq){
s_i <- delta_x*d_fx(x_i)
s <-c (s, s_i)
}
return(sum(s))
}
area = area_fx(1,3)
area
## [1] 41.99984
To validate the results, we will now compute the value under the curve by evaluating the integral of the function between the values [1,3]. We will do this by comptuting the value of F(3)-F(1). Please note that this function is given by the function in the first problem f(x).
area_test <- fx(3)-fx(1)
area_test
## [1] 42
We will solve this using integration by part which gives us:
\(\int { f\left( x \right) g^{ ' }\left( x \right) } dx=f\left( x \right) g\left( x \right) \quad -\quad \int { f^{ ' }\left( x \right) } g\left( x \right)dx\)
We will set \(f\left( x \right)\) and \(g^{ ' }\left( x \right)\) as follows: \(f\left( x \right) =\sin { x }\) and \(g^{ ' }\left( x \right) =\cos { x }\),
this will give us:
\(f^{ ' }\left( x \right) =\cos { x }\) and \(g\left( x \right) =\sin { x }\)
Substituing we get:
(1)\(\int { \sin { x } \cos { x } } dx\quad =\quad \sin { x } \sin { x } -\quad \int { \cos { x } } \sin { x } dx\)
Since \(\int { \cos { x } } \sin { x } dx=\int { \sin { x } } \cos { x } dx\), we can rewrite (1) as follows
\(\int { \sin { x } \cos { x } } dx\quad =\quad \sin { x } \sin { x } -\quad \int { \sin { x } } \cos { x } dx\), which leads to
\(2\int { \sin { x } \cos { x } } dx\quad =\quad { sin }^{ 2 }x\), hence we have:
\(\int { \sin { x } \cos { x } } dx\quad =\quad \frac { 1 }{ 2 } { sin }^{ 2 }x\quad +\quad C\)
Again, we will solve this using integration by part. We will set \(f\left( x \right) ={ x }^{ 2 }\) and \(g^{ ' }\left( x \right) ={ e }^{ x }\)
This give us the following:
(2) \(\int { { x }^{ 2 } } { e }^{ x }dx\quad =\quad { x }^{ 2 }{ e }^{ x }\quad -\quad \int { 2x } { e }^{ x }dx\quad\)
Again, we will using integration by part to solve \(\int { 2x } { e }^{ x }dx\quad\).
In this case, \(f\left( x \right)=x\) and \(g^{ ' }\left( x \right) ={ e }^{ x }\), hence we get:
\(\int { 2x } { e }^{ x }dx\quad =\quad 2\int { x } { e }^{ x }dx\quad =\quad 2(x{ e }^{ x }\quad -\quad \int { 1 } { e }^{ x }dx)\quad =\quad 2x{ e }^{ x }\quad -\quad 2{ e }^{ x }\),
substituing back into (2) we obtain, \(\int { { x }^{ 2 } } { e }^{ x }dx\quad =\quad { x }^{ 2 }{ e }^{ x }\quad -\quad 2(x{ e }^{ x }-{ e }^{ x })\quad =\quad { e }^{ x }({ x }^{ 2 }-2x+2)\quad +\quad C\)
Let us set \(f\left( x \right)=x\) and \(g\left( x \right) =\cos { x }\), substituing in formula above, we have:
\(\frac { d }{ dx } (x\cos { x) } =1\cos { x } +\quad x(-\sin { x } )\quad =\quad \cos { x } -\quad x\sin { x }\)
Let us set \(f\left( x \right) ={ e }^{ x }\) and \(g\left( x \right) ={ x }^{ 4 }\), Hence we get by applying the chain rule:
\(\frac { d }{ dx } ({ e }^{ { x }^{ 4 } })={ e }^{ { x }^{ 4 } }\cdot 4{ x }^{ 3 }=4{ x }^{ 3 }{ e }^{ { x }^{ 4 } }\)