Matlab Derivative Approximation

Click For Summary
SUMMARY

This discussion focuses on a MATLAB program designed to estimate the derivative of a polynomial and calculate the associated error. The code utilizes a centered difference approximation method to compute the derivative, but the user reports that the output values for the derivative and error are switched. Additionally, concerns are raised regarding the for-loop potentially running indefinitely due to the step size 'dh' never reaching zero. A suggestion is made to terminate the loop when 'dh' is less than a specified limit 'e'.

PREREQUISITES
  • Understanding of MATLAB programming and syntax
  • Familiarity with numerical methods for derivative approximation
  • Knowledge of polynomial functions and their properties
  • Concept of error analysis in numerical computations
NEXT STEPS
  • Research MATLAB's built-in functions for numerical differentiation
  • Learn about error analysis techniques in numerical methods
  • Explore the concept of convergence in iterative algorithms
  • Investigate alternative derivative approximation methods, such as forward and backward differences
USEFUL FOR

Mathematics students, MATLAB programmers, and anyone interested in numerical analysis and derivative approximation techniques.

JefeNorte
Messages
9
Reaction score
0
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
 
Physics news on Phys.org
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K