1

I was doing numerical method in R and Python I have applied leapfrog method in python and it worked perfectly but I want to do the slimier thing in R. Here you can see my code

Possibly I have tried doing u[2,2]=beta*(u[1,1]-2*u[2,1]+u[3,1]) this works, here I can see that the error is due to the bold statement means due to u[2,0] does not exist. But the same code worked in python, Please help to resolve the error while executing the loop

u[i, j - 1] evaluates to u[2, 0], or numeric(0). This is what produces the error. Is there any solution.

1 Answers1

0

Keep in mind that R indexes are 1-based, while in Python they are 0-based. In your code, the first time through the for loop, u[i, j - 1] evaluates to u[2, 0], or numeric(0). This is what produces the error.

BigFinger
  • 1,033
  • 6
  • 7
  • I have seen that thing thats why my loop is being started from 1 – Hamza Afzal Dec 20 '19 at 20:39
  • Yes that is the error. But how it will be resolved. The same thing u[1,-1] works in python – Hamza Afzal Dec 20 '19 at 20:40
  • My requirement is to bring same results for u[2,2] in r that was u[1,1] in python. But R is causing this error – Hamza Afzal Dec 20 '19 at 20:43
  • I don't understand the algorithm, so my suggestion may not be correct, but you can avoid index errors by modifying your index ranges like this: for(j in 2:(Nt - 1)){ for(i in 2:(Nx - 1)){ – BigFinger Dec 20 '19 at 21:53
  • Your suggstion is correct but I need those values also. I dont want to mis calculate any value. Thats why I am here asking for help ne to have each value. – Hamza Afzal Dec 21 '19 at 03:08