Array indices must be positive integers or logical values Error
1 view (last 30 days)
The question I am trying to answer is: Write a program to test the two finite difference approximations in section 1.6 (the forward and centered difference formulas). Test your program on the function arctan(x) at x=sqrt(2).Start with h = 1/2, and generate a sequence of approximations by reducing h by a factor of 2 each time, until h = 2−40. Observe how the error changes with h.
So far I have;
I keep getting the array indices must be positive integers or logical values error.
Ashutosh Prasad on 7 Sep 2018
The error that you are getting is because of the way you are defining the h vector. Your definition creates an empty vector with no elements. But I understand you want h to be halved on each iteration, that's when loops come handy.
Try execution the following script
f = @(x) atan(x);
x = sqrt(2) ;
h = 0.5;
while(h >= 2^(-40))
F = ((f(x+h)-f(x))/h);
h = h/2;
Let me know if this helps