MATLAB: How do i make the current code plug h back into the central finite difference equation after it is divided? Currently it runs the h reduction but not recalculating anything else.

central difference program

During my while loop my h value decreases like it is suppose to but the value is not being plugged back into my equation so I am not getting the values I should have. Plus I need this values put into a table. What can I do to my current code to have the new h value put back into my equation and the error calculated again? I also need to have all previous values plotted into a table. Also I need the original h plotted as well.
Here is my code:
%x-value%
x=0.5;
%define function%
f=inline('-0.1.*x.^4-0.15.*x.^3-0.5.*x.^2-.25.*x+1.2','x');
%calculated true value%
truevalue=-.9125;
h=1;
Central_Finite_Difference_Approximation=(f(x+h)-f(x-h))/2*h;
Relative_Error=abs((truevalue-Central_Finite_Difference_Approximation)/truevalue);
while 3.64*10^-10 < Relative_Error
h=h/10;
x=0.5;
Step_Size=h;
t=table(Step_Size,Central_Finite_Difference_Approximation,Relative_Error);
disp(t)
end
This is my result:
Step_Size Central_Finite_Difference_Approximation Relative_Error
_________ _______________________________________ ______________
0.1 -1.2625 0.38356
Step_Size Central_Finite_Difference_Approximation Relative_Error
_________ _______________________________________ ______________
0.01 -1.2625 0.38356
Step_Size Central_Finite_Difference_Approximation Relative_Error
_________ _______________________________________ ______________
0.001 -1.2625 0.38356
Step_Size Central_Finite_Difference_Approximation Relative_Error
_________ _______________________________________ ______________
0.0001 -1.2625 0.38356
Step_Size Central_Finite_Difference_Approximation Relative_Error
_________ _______________________________________ ______________
1e-05 -1.2625 0.38356

Best Answer

Recalculate inside your while loop to get new values. For example:
%x-value%
x = 0.5;
%define function%
%Using an anonymous function instead of inline, see doc inline.
f = @(x) -0.1.*x.^4-0.15.*x.^3-0.5.*x.^2-.25.*x+1.2;
%calculated true value%
truevalue = -.9125;
%Set Intial Values of parameters
h = 1;
Step_Size=h;
Central_Finite_Difference_Approximation = (f(x+h)-f(x-h))/(2*h);
Relative_Error = abs((truevalue-Central_Finite_Difference_Approximation)/truevalue);
while 1e-5 < Relative_Error
h = h/10;
%Vertically Append to save calculated values
Step_Size=[Step_Size;h];
Central_Finite_Difference_Approximation = [Central_Finite_Difference_Approximation; (f(x+h)-f(x-h))/(2*h)];
%Careful only to use the last value of the vector for new error
Relative_Error = [Relative_Error; abs((truevalue-Central_Finite_Difference_Approximation(end))/truevalue)];
end
%Make one table out of all calculated values.
t=table(Step_Size,Central_Finite_Difference_Approximation,Relative_Error);
%display our final table.
disp(t)