2

I'm trying to write a program in matlab that checks how consistent the definition of the derivative becomes:

(f(x+h)-f(x))/h ~= f'(x)

when h is small enough. Thus far i have this:

function [errList] = diffConsistency(f,df,x,iMax,h0)
h=h0;
for i=1:iMax
    leftSide = (f(x+h) - f(x)) / h;
    rightSide = df(x);
    errList = abs(leftSide - rightSide);
    h = h*10^(-1);
end

I then use f=@(x)sin(x) and df=@(x)cosx, I'm new to using function handles so this might be wrong completely. iMax is set to 10 and h0 = 1, x=rand(10)

Could anyone check if this is even remotely correct. Especially the use of the function handles inside the diffConsistency function and use of the rand.

Should i define x differently, leftside rightside are correct? etc

Any feedback would help. Thanks in advance

il_raffa
  • 5,090
  • 129
  • 31
  • 36
p.late
  • 31
  • 6
  • How have you tested this so far? Does it work for sin(x) and cos(x)? Does it work for cases where the function don't match (try it for f = sin(x) and df = sin(x), say)? – EdR May 01 '18 at 12:38
  • It does not work as i hoped, since the error fluctuates too much and does not converge to something – p.late May 01 '18 at 12:46
  • Could you edit the question then to give an example of the output for various values of `h`? – EdR May 01 '18 at 12:49

1 Answers1

2

You use some specific data that obscures the result. You input 10x10 random numbers, and output a 10x10 matrix of errors, but this is only for the last i, as you overwrite errList every iteration!

change the function to:

function [errList] = diffConsistency(f,df,x,iMax,h0)
h=h0;
for i=1:iMax
    leftSide = (f(x+h) - f(x)) / h;
    rightSide = df(x);
    errList(i) = abs(leftSide - rightSide);
    h = h*10^(-1);
end

and if you call it as :

err=diffConsistency(@sin,@cos,rand,10,1)

and plot(err), you can clearly see how the error gets reduced each smaller h.

Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
  • Done. I do however have one more question. When I set iMax to 100 my error decreases until a certain value (around 10^-8), but then it starts increasing again. I do not know how to explain this. Is it a flaw in the code, number loss or something else entirely? – p.late May 01 '18 at 13:45
  • 2
    @p.late dangerous place you are getting into, careful careful! This has to do with computers, and floating point arithmetic. You can not store arbitrarily small numbers in a computer, as it has limited memory. When you reach 1e-12 you are starting to get into dangerous area where accuracy of the computation is not reliable anymore. You are saying you are trying to compute 1e-100 (`h`). Read more about why this happens [here](https://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab) – Ander Biguri May 01 '18 at 13:55
  • Ah I see. What do you think would be an appropriate value to give iMax in this case? By the way I adapted my code with the following break statement: – p.late May 01 '18 at 16:14
  • if h < 2^(-52) break end – p.late May 01 '18 at 16:15