Problem 8.8.18 (p496)

Using Taylor Series Verify sin(-x)=sin(x)

library(pracma)
## Warning: package 'pracma' was built under R version 3.6.3
f1 <- function(x)  sin(-x)

p1 <- round(taylor(f1, 0, 4),2)
p1
## [1]  0.17  0.00 -1.00  0.00
s<- seq(-1.0, 1.0,0.1)
f1_s <- f1(s)

plt_taylor1 <- polyval(p1, s)
plot(s, plt_taylor1,type="l",col="red")

f2 <- function(x)  -sin(x)

p2 <- round(taylor(f1, 0, 4),2)
p2
## [1]  0.17  0.00 -1.00  0.00
s<- seq(-1.0, 1.0,0.1)
f2_s <- f2(s)

plt_taylor2 <- polyval(p2, s)
plot(s, plt_taylor2,type="l",col="red")

match=0
err=0
for (i in 1:4)
{
  if(p1[i]==p2[i])
  {
    match=match+1
  }
  else
  {
    err=i
    break
  }
}

if(err==1)
{
  print("Functions do not match")
} else 
{
  print("Functions do  match")
}
## [1] "Functions do  match"