Ok, I'm having a problem (probably a newbie problem) but here it goes:
for i=1:1:16; h=10^(-i); yDFC2(i)=(1/(2*1*h(i)))*(-1*valX+valX); end err2=abs(fDX(x1)-yDFC2); loglog(h,err2,'b-');
Every time I run this code, it gives me the following error:
Attempted to access h(2); index out of bounds because numel(h)=1
valX is a variable calculated by determining the image of a function that has been inputted by the user;
fDX is the derivative of that function (both are working well).
This is meant to plot the spacing (
h) vs the error in central finite differences (CFD). The formula inside the for cycle is the 1st order derivative calculated by CFD for
The spacing needs to be in increments of
If anyone could help me figure this out, I'd be very thankful.
If you do
h = 10.^(-(1:1:16));
outside of the for loop, you can accress h(i) correctly inside. The problem is that you create a scalar h for each pass.