Matlab Derivative Approximation

In summary, the conversation is about a program that estimates the derivative of a polynomial and calculates the error. The code for the program is provided and the conversation includes a discussion about potential errors and adjustments to the for-loop.
  • #1
JefeNorte
9
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
  • #2
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.
 
  • #3
?

I would suggest checking your code for any errors or typos. It is also important to double check your equations and make sure they are correct. Additionally, it may be helpful to break down your code into smaller steps and test each step individually to pinpoint where the error is occurring. You could also try using a different method for estimating the derivative, such as the forward or backward difference method, to see if you get the same results. Lastly, it is always a good idea to consult with other experts or resources to get feedback and troubleshoot any issues you may be having.
 

What is Matlab Derivative Approximation?

Matlab Derivative Approximation is a numerical method used to approximate the derivative of a function at a given point. It uses a finite difference formula to estimate the slope of the function at that point.

Why is Matlab Derivative Approximation useful?

Matlab Derivative Approximation is useful because it allows for the calculation of derivatives without the need for an explicit formula. This is particularly helpful when dealing with complex functions or when an analytic expression for the derivative is not available.

How is Matlab Derivative Approximation implemented?

Matlab Derivative Approximation is implemented by using the 'diff' function in Matlab. This function takes in a vector of data points and calculates the first-order derivative of the data points at each interval. The result is a vector of derivative estimates.

What are the limitations of Matlab Derivative Approximation?

One limitation of Matlab Derivative Approximation is that it can only provide an estimate of the derivative at a given point. The accuracy of the estimate depends on the step size used in the finite difference formula. Using too large of a step size can result in a less accurate approximation. Additionally, Matlab Derivative Approximation may not work well for functions with sharp changes or discontinuities.

Are there other methods for calculating derivatives in Matlab?

Yes, there are other methods for calculating derivatives in Matlab, such as the 'gradient' function and the 'diff' function with higher-order derivative options. These methods may provide more accurate results for certain functions, but they may also require more computational resources.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • Introductory Physics Homework Help
Replies
30
Views
1K
  • General Math
Replies
11
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top