Richardson extrapolation matlab error

In summary, the issue with the program is that you are trying to define a function within another function, which is not allowed in MATLAB. You can fix this by moving the function definition outside of the difflim function or creating a separate .m file for the function. Also, consider using a more precise value for the error bound.
  • #1
Adrian Soo
8
0
Hi everyone, I am trying to differentiate a function by means of Richardson Extrapolation in MATLAB. However, I could not run the program. Here are the outputs and its codes. Could anyone explain to me
what is my mistake? The codes are based on "Numerical Methods Using MATLAB by John H. Matthews and
Kurtis D. Fink (International Edition)".

>> f = @(x) x^2;
>> toler = 0.005;
>> function [L,n] = difflim(f,x,toler)
function [L,n] = difflim(f,x,toler)
|
Error: Function definitions are not permitted in this context.

=========================================
M-file:

function [L,n] = difflim(f,x,toler)
%Input - f is the function input as a string 'f'
% - x is the differentiation point
% - toler is the tolerance for the error
%Output - L=[H' D' E']
% H is the vector of step sizes
% D is the vector of approximate derivatives
% E is the vector of error bounds
% n is the coordinate of the "best approximation"
max1 = 15;
h = 1;
H(1) = h;
D(1) = (feval(f,x+h)-feval(f,x-h))/(2*h);
E(1) = 0;
R(1) = 0;

for n = 1:2
h = h/10;
H(n+1) = h;
D(n+1) = (feval(f,x+h)-feval(f,x-h))/(2*h);
E(n+1) = abs(D(n+1)+abs(D(n))+eps);
end

n = 2;

while((E(n)>E(n+1))&(R(n)>toler))&n<max1
h=h/10;
H(n+2)=h;
D(n+2)=(feval(f,x+h)-feval(f,x-h))/(2*h);
E(n+2)=abs(D(n+2)-D(n+1));
R(n+2)=2*E(n+2)/(abs(D(n+2))+abs(D(n+1))+eps);
n=n+1;
end

n=length(D)-1;
L=[H' D' E'];
 
Physics news on Phys.org
  • #2


Hi there,

I see that you are trying to use Richardson Extrapolation in MATLAB to differentiate a function. However, it seems like you are getting an error when trying to run the program. Looking at the codes, I can see that the error is occurring because you are trying to define a function within another function. This is not allowed in MATLAB.

To fix this issue, you can either move the function definition outside of the difflim function or create a separate .m file for the function and call it within the difflim function.

Additionally, I noticed that you are using the eps function to calculate the error bound. This function returns the machine epsilon, which may not be accurate enough for your desired tolerance. I would suggest using a more precise value for the error bound, such as 10^-15.

I hope this helps and good luck with your project!
 

Related to Richardson extrapolation matlab error

What is Richardson extrapolation and how does it relate to Matlab error?

Richardson extrapolation is a numerical method used to improve the accuracy of a sequence of approximations by combining them. In Matlab, it can be used to reduce the error in calculations that involve iterative methods.

Why do we use Richardson extrapolation in Matlab?

Richardson extrapolation is used in Matlab to increase the accuracy of calculations and reduce the error. It can be particularly useful for iterative methods where the error can accumulate with each iteration.

What are some common sources of error when using Richardson extrapolation in Matlab?

Common sources of error when using Richardson extrapolation in Matlab include rounding errors, truncation errors, and machine precision limitations. It is important to carefully consider the input values and the algorithm used to minimize these errors.

How do we implement Richardson extrapolation in Matlab?

To implement Richardson extrapolation in Matlab, you can use the "richardson" function, which takes in a sequence of approximations and uses them to calculate a more accurate estimate. This function can be found in the Symbolic Math Toolbox.

Can Richardson extrapolation be used for any type of calculation in Matlab?

No, Richardson extrapolation is most commonly used for calculations that involve iterative methods. It may not be suitable for other types of calculations, such as analytical solutions or non-iterative algorithms.

Similar threads

  • Calculus and Beyond Homework Help
Replies
5
Views
686
  • Calculus and Beyond Homework Help
Replies
10
Views
2K
  • Calculus and Beyond Homework Help
Replies
3
Views
624
  • Calculus and Beyond Homework Help
Replies
6
Views
634
  • Calculus and Beyond Homework Help
Replies
1
Views
752
  • Calculus and Beyond Homework Help
Replies
8
Views
553
  • Calculus and Beyond Homework Help
Replies
8
Views
926
  • Calculus and Beyond Homework Help
Replies
18
Views
1K
  • Calculus and Beyond Homework Help
Replies
10
Views
1K
  • Calculus and Beyond Homework Help
Replies
9
Views
2K
Back
Top