I am trying to write a program that estimates the derivative of a polynominal and determines the error. So far my code is(adsbygoogle = window.adsbygoogle || []).push({});

% The code for Problem 3.

a=5-4*x^2+3*x^3-2*x^4+x^5; % ask for a function to be differentiated

x=input('Enter the value x at which to find the derivative '); % ask for input

dh=.5;

for i=1:dh:100% do the loop

dh=.5*(1/2)^i; % step size

df=((5-4.*(x+dh).^2+3.*(x+dh).^3-2.*(x+dh).^4+(x+dh).^5)-(5-4*x^2+3*x^3-2*x^4+x^5))/dh; % centered difference approximation

if 1+dh==1

break

else

dh=dh/2;

end

end

error=(-8*x+9*x^2-8*x^3+5*x^4)-df;

disp(df)

disp(error)

The result is two numbers that are correct but the df is switched with error and vise versa. Does anyone know what I am doing wrong

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab Derivative Approximation

**Physics Forums | Science Articles, Homework Help, Discussion**