Advertisement
wh1plash

Untitled

Nov 14th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.94 KB | None | 0 0
  1. x = seq(-2*pi, 2*pi, length.out = 250)
  2. y = sin(x)
  3.  
  4. plot(x,y,
  5.      type = "l",
  6.      col = "grey",
  7.      lwd = 2,
  8.      bty = "n")
  9. x0 = pi/2
  10.  
  11. pol_sin = function(x, x0, n){
  12.   y = sin(x0);
  13.   for(i in 1:n){
  14.     if(i%%4 == 0){
  15.       y = y + sin(x0) * (x-x0)^i/factorial(i)
  16.     } else if(i%%4 == 1) {
  17.       y = y + cos(x0) * (x-x0)^i/factorial(i)
  18.     } else if(i%%4 == 2) {
  19.       y = y - sin(x0) * (x-x0)^i/factorial(i)
  20.      
  21.     } else if(i%%4 == 3) {
  22.       y = y - cos(x0) * (x-x0)^i/factorial(i)
  23.     }
  24.   }
  25.   return(y)
  26. }
  27. pol_sin = Vectorize(pol_sin, vectorize.args = "x")
  28. lines(x,pol_sin(x,x0,2),
  29.       col = "red",
  30.       lwd = 2, lty = 2)
  31. lines(x,pol_sin(x,x0,3),
  32.       col = "blue",
  33.       lwd = 2, lty = 2)
  34. lines(x,pol_sin(x,x0,6),
  35.       col = "pink",
  36.       lwd = 2, lty = 2)
  37. lines(x,pol_sin(x,x0,12),
  38.       col = "magenta",
  39.       lwd = 2, lty = 2)
  40. lines(x,pol_sin(x,x0,20),
  41.       col = "forestgreen",
  42.       lwd = 2, lty = 2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement