Matlab Derivative Approximation

  • MATLAB
  • Thread starter JefeNorte
  • Start date
9
0

Main Question or Discussion Point

I am trying to write a program that estimates the derivative of a polynominal and determines the error. So far my code is

% 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
 

Answers and Replies

263
70
I don't know if this has to with your problem. But in my opinion has your for-loop an infinite number of turns because dh will never be 0 (dh is a real number). So instead you should stop your loop then dh<e, where e is some limit.
 

Related Threads for: Matlab Derivative Approximation

Replies
1
Views
3K
  • Last Post
Replies
0
Views
1K
Replies
5
Views
711
Replies
1
Views
1K
Replies
1
Views
1K
Top