Matlab script for central difference

  • Context: MATLAB 
  • Thread starter Thread starter NYK
  • Start date Start date
  • Tags Tags
    Difference Matlab
Click For Summary

Discussion Overview

The discussion revolves around writing a Matlab script to compute the centered difference approximation for estimating the first derivative of a polynomial function. Participants explore issues related to script structure, function definitions, and error handling, particularly focusing on how round-off error is affected by decreasing step sizes.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes their approach to implementing a centered difference approximation for the derivative of a given polynomial function and seeks advice on their script.
  • Another participant requests clarification on the output of the script to identify what might be going wrong.
  • A participant points out an error message regarding function definitions in Matlab and suggests that the user may need to write a script instead of a function file.
  • Some participants recommend starting with a simple loop to compute the function and its derivative directly, rather than using function definitions.
  • Another participant explains that the error message about function definitions arises when trying to define a function at the command line, indicating that functions must be saved in an mfile.

Areas of Agreement / Disagreement

Participants generally agree that the error message is related to the context in which the function is defined, but there is no consensus on the best approach to resolve the issue, with differing opinions on whether to use a script or an mfile.

Contextual Notes

Participants have not fully resolved the issue of how to structure the Matlab code, and there are varying levels of familiarity with Matlab functions among participants, which may affect the discussion.

NYK
Messages
27
Reaction score
0
I am trying to write a script that will compute repeatedly, beginning with step size h=1 and then progressively divide the step size by a factor of 10 to demonstrate how round-off error becomes dominant as the step size is reduced.

In using centered difference approximation of O(h2) to estimate the first derivative at x=0.7 of:

f(x) = -0.25x^5+0.15x^4-1.25x^3+0.35x^2+2.1x-1.4
I have posted the script I have so far bellow:clear;clc;clear all

ff=@(x) -0.25*x^5+0.15*x^4-1.25*x^3+0.35*x^2+2.1*x-1.4;
df=@(x) -1.25*x^4+0.6*x^3-3.75*x^2+0.7*x+2.1;

function diffex(func,dfunc,x,n)

format long
dftrue=dfunc(x);
h=1;
H(1)=h;
D(1)=(func(x+h)-func(x-h))/(2*h);
E(1)=abs(dftrue-D(i));
for i=2:n
h=h/10;
H(i)=h;
D(i)=(func(x+h)-func(x-h))/(2*h);
E(i)=abs(dftrue-D(i));
end
L=[H' D' E']';
fprintf(' step size finite difference round-off error\n');
fprintf('%14.10f %16.14f %16.13f\n',L);
loglog(H,E),xlabel('Step Size'),ylabel('Error')
title('Plot of Error vs. Step Size')
format shortdiffex(ff,df,0.7,11)



Any advice, pointers or suggestions will be greatly appreciated!

thank you
 
Physics news on Phys.org
What goes wrong? Can you post here the output?
 
The error code i get is that "Function definitions are not permitted in this context", but i really don't need a function file. I have begun writing it as a regular script file.
 
If you are not familiar with the use of the Matlab functions then you should try first the easy way: simple loop where you compute directly the function, its derivative and the corresponding error.
If you really want to use Matlab functions you should have a look at some simple example provided by Matlab Help.
 
FYI: The error message "Function definitions are not permitted in this context" arises when you try to type a function in at the command line. That is disallowed; all functions must be saved in an mfile.

Rewriting this to be a script rather than a function is one option, but the other way would be to just paste the code into an mfile, save the file, and then you can call the function freely.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
10K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K