0

I am trying a solve a question. When I run this code I achieved what did I want.(.8372e-03 1.8372e-06 1.8372e-09)

truncation.error <- function(f, x) {
  steps <- c(0.1, 0.01, 0.001)
  n <- length(steps)
  fdx1 <- vector(length = n)
  for (h in 1:n) {
    fdx1[h] <- exp(2.4)*steps[h]^3/6
  }
  return(fdx1)
}
for (i in 2.4) {
  print(truncation.error(f, x))}

However when I write in data.frame format like it the output for truncation.error is 0-0-0. why the output changed? Any idea? and how can I fixed this problem. I want to see true answer which I achieved in above. ;

colnames(approx.df) <- c('Actual Values', 'Central Difference Approx', 'Central Differences Error', "truncation error")

approx.df```



Original question is [ [1]: https://i.stack.imgur.com/hmO3I.png][1]


How can I solve this problem or where am I making mistake? Thanks a lot.


 [1]: https://i.stack.imgur.com/hmO3I.png

sessen
  • 3
  • 3
  • change f <- expression(exp(x)) it to f <- as.expression(exp(x)) . your error will be gone – Hunaidkhan May 18 '20 at 23:21
  • Thank you so much. But now the outputs are rounded to zero. How can I take real results. Which are 0.00183720-0.0000084 and 0.00000002 – sessen May 18 '20 at 23:37
  • can you tell me why you are using fdx1<-vector(length=n) – Hunaidkhan May 19 '20 at 04:31
  • I added the original question in above. The problem in the 3rd part. (Truncation error part). I tried to write it according to how did I find central differences. Which is ` f <- function(x) { return(exp(x))} central.difference <- function(f, x) { steps <- c(0.1, 0.01, 0.001) n <- length(steps) fdx <- vector(length = n) for (h in 1:n) { fdx[h] <- (f(x+ steps[h]) - f(x - steps[h])) / (steps[h]*2) } return(fdx) } for (i in x) { print(central.difference(f, 2.3)) }`. So actually I don't know why did I use fdx1<-vector(length=n) . Just trying to solve it.Shouldn't I use? – sessen May 19 '20 at 04:49
  • i tried to replicate the complete code and fdx1 is giving logical anser true and false because of that your final answer comes out as 0 – Hunaidkhan May 19 '20 at 05:41
  • I fixed it but I don't know-how. It works now.When I print it answer come out as true. I really don't know how to use R sorry for that. Thanks a lot for your help. And here my last question. When I print this code, the answer comes out what should suppose to be so it is true. (0.00183720...) however when I define something like that approx.df <- data.frame(cbind(actual, central.approx, actual - central.approx,round(truncation.error(f,5)))) the output shows again as just 0. Do you have any idea for that ? – sessen May 19 '20 at 06:50
  • can you provide me reproducible code with dput() i will try to help you after that . Also in the above code you need to provide decimel places in round – Hunaidkhan May 19 '20 at 07:22
  • I would like to but I don't know how to use dput(). But here is the actual code "truncation.error <- function(f, x) { steps <- c(0.1, 0.01, 0.001) n <- length(steps) fdx1 <- vector(length = n) for (h in 1:n) { fdx1[h] <- exp(2.4)*steps[h]^3/6 } return(fdx1) } for (i in 2.4) { print(truncation.error(f, x))}approx.df <- data.frame(cbind(actual, central.approx, actual - central.approx,round(truncation.error(f,5)))) colnames(approx.df) <- c('Actual Values', 'Central Difference Approx', 'Central Differences Error', "truncation error") approx.df. " – sessen May 19 '20 at 07:46
  • Mistake you are doing is you are not defining these things actual, central.approx, actual - central.approx where are these variabes saved or defined in your code? – Hunaidkhan May 19 '20 at 23:09
  • f <- function(x) { return(exp(x)) } central.difference <- function(f, x) { steps <- c(0.1, 0.01, 0.001) n <- length(steps) fdx <- vector(length = n) for (h in 1:n) { fdx[h] <- (f(x+ steps[h]) - f(x - steps[h])) / (steps[h]*2) } return(fdx) } for (i in x) { print(central.difference(f, 2.3)) }fdx <- function(x) { return(exp(x) ) } actual <- vector(length = length(x)) central.approx <- c(9.99081, 9.97435, 9.97418) for (i in 1:length(x)) { actual[i] <- fdx(x[i]) } it is first part of my code . I defined here. After that I passed to truncation.error part – sessen May 20 '20 at 00:14
  • Oooo you are right. I defined truncation error in where did I defined central.difference and actual. It worked Now my output is fixed. Thank you so much four your kind help. – sessen May 20 '20 at 00:32
  • your welcome and please marked it as closed or answered one i add a comment – Hunaidkhan May 20 '20 at 04:35

1 Answers1

0

change f <- expression(exp(x)) it to f <- as.expression(exp(x)) . your error will be gone and Mistake you are doing is you are not defining these things actual, central.approx, actual - central.approx where are these variabes saved or defined in your code?

Hunaidkhan
  • 1,411
  • 2
  • 11
  • 21