MATLAB: Decimal places for value stored

decimal placevariable value

Hello, im wondering if there any way i could perform calculation using values up to 4 decimal places only.
For example,i write this in command windows:
x=4.21;
h=0.001;
f=@(x) x^3 + 8*x^2 + 23*x +3;
f2_prime = (f(x+h)-2*f(x)+f(x-h))/(h^2);
how can i make the value of f(x), f(x+h) and f(x-h) to be only up to 4 decimal places when calculating for f2_prime?

Best Answer

You can use round()
x=4.21;
h=0.001;
f=@(x) x^3 + 8*x^2 + 23*x +3;
f2_prime = (round(f(x+h),4)-2*f(x)+round(f(x-h),4))/(h^2);