Skip to main content
added 18 characters in body
Source Link
nicoguaro
  • 8.6k
  • 6
  • 26
  • 51

Trying to study the error of FDM for a second order derivative versus step size. I I calculated the coefficients and validated them, but the output has errors for small step sizes.
The

The function in question is: $ f(x) = e^{sin(x)} $
With

$$ f(x) = e^{\sin(x)} $$

With a derivative of $ f''(x) = \left(cos^2(x)-sin(x)\right)e^{sin(x)} $
The$$ f''(x) = \left(\cos^2(x) - \sin(x)\right)e^{\sin(x)} $$

The calculated FDM: $$ \frac{\partial^{(2)}f}{\partial x^{(2)}}\approx -\frac{5}{2h^2}f(x)-\frac{1}{12h^2}f(x-2h)+\frac{4}{3h^2}f(x-h)+\frac{4}{3h^2}f(x+h)-\frac{1}{12h^2}f(x+2h) $$

$$\frac{\partial^{(2)}f}{\partial x^{(2)}}\approx -\frac{5}{2h^2}f(x) - \frac{1}{12h^2}f(x-2h) + \frac{4}{3h^2}f(x-h) + \frac{4}{3h^2}f(x+h) - \frac{1}{12h^2}f(x+2h)$$

And my octaveOctave implementation:

stps = 1e-7:1e-6:3e-5;
outs = [];
x = pi/2;
for h = stps
  coefficients = [-5/2 -1/12 4/3 4/3 -1/12]./h^2;
  steps = [0 -2 -1 1 2].*h;

  outs(end+1) = sum(coefficients .* exp(sin(x + steps))); 
end

plot(stps,outs, "linewidth",1.5 , stps, ones(size(stps)).* (cos(x)^2 - sin(x))*exp(sin(x)) , "linewidth",1.5)

Which produces the following plot: FDM output plot

Are those rounding errors caused by the nature of floating point numbers and operations or something else?

Trying to study the error of FDM for a second order derivative versus step size. I calculated the coefficients and validated them, but the output has errors for small step sizes.
The function in question is: $ f(x) = e^{sin(x)} $
With a derivative of $ f''(x) = \left(cos^2(x)-sin(x)\right)e^{sin(x)} $
The calculated FDM: $$ \frac{\partial^{(2)}f}{\partial x^{(2)}}\approx -\frac{5}{2h^2}f(x)-\frac{1}{12h^2}f(x-2h)+\frac{4}{3h^2}f(x-h)+\frac{4}{3h^2}f(x+h)-\frac{1}{12h^2}f(x+2h) $$

And my octave implementation:

stps = 1e-7:1e-6:3e-5;
outs = [];
x = pi/2;
for h = stps
  coefficients = [-5/2 -1/12 4/3 4/3 -1/12]./h^2;
  steps = [0 -2 -1 1 2].*h;

  outs(end+1) = sum(coefficients .* exp(sin(x + steps))); 
end

plot(stps,outs, "linewidth",1.5 , stps, ones(size(stps)).* (cos(x)^2 - sin(x))*exp(sin(x)) , "linewidth",1.5)

Which produces the following plot: FDM output plot

Are those rounding errors caused by the nature of floating point numbers and operations or something else?

Trying to study the error of FDM for a second order derivative versus step size I calculated the coefficients and validated them, but the output has errors for small step sizes.

The function in question is

$$ f(x) = e^{\sin(x)} $$

With a derivative of $$ f''(x) = \left(\cos^2(x) - \sin(x)\right)e^{\sin(x)} $$

The calculated FDM

$$\frac{\partial^{(2)}f}{\partial x^{(2)}}\approx -\frac{5}{2h^2}f(x) - \frac{1}{12h^2}f(x-2h) + \frac{4}{3h^2}f(x-h) + \frac{4}{3h^2}f(x+h) - \frac{1}{12h^2}f(x+2h)$$

And my Octave implementation:

stps = 1e-7:1e-6:3e-5;
outs = [];
x = pi/2;
for h = stps
  coefficients = [-5/2 -1/12 4/3 4/3 -1/12]./h^2;
  steps = [0 -2 -1 1 2].*h;

  outs(end+1) = sum(coefficients .* exp(sin(x + steps))); 
end

plot(stps,outs, "linewidth",1.5 , stps, ones(size(stps)).* (cos(x)^2 - sin(x))*exp(sin(x)) , "linewidth",1.5)

Which produces the following plot: FDM output plot

Are those rounding errors caused by the nature of floating point numbers and operations or something else?

Source Link

Finite difference method basic implementation on Octave

Trying to study the error of FDM for a second order derivative versus step size. I calculated the coefficients and validated them, but the output has errors for small step sizes.
The function in question is: $ f(x) = e^{sin(x)} $
With a derivative of $ f''(x) = \left(cos^2(x)-sin(x)\right)e^{sin(x)} $
The calculated FDM: $$ \frac{\partial^{(2)}f}{\partial x^{(2)}}\approx -\frac{5}{2h^2}f(x)-\frac{1}{12h^2}f(x-2h)+\frac{4}{3h^2}f(x-h)+\frac{4}{3h^2}f(x+h)-\frac{1}{12h^2}f(x+2h) $$

And my octave implementation:

stps = 1e-7:1e-6:3e-5;
outs = [];
x = pi/2;
for h = stps
  coefficients = [-5/2 -1/12 4/3 4/3 -1/12]./h^2;
  steps = [0 -2 -1 1 2].*h;

  outs(end+1) = sum(coefficients .* exp(sin(x + steps))); 
end

plot(stps,outs, "linewidth",1.5 , stps, ones(size(stps)).* (cos(x)^2 - sin(x))*exp(sin(x)) , "linewidth",1.5)

Which produces the following plot: FDM output plot

Are those rounding errors caused by the nature of floating point numbers and operations or something else?