I got this assignment and I honestly don't even know where to begin. My teacher isn't making much sense to me at the moment, so if anybody can help me I would greatly appreciate it!
First we will need a bit more information for i) are you using the forward difference, backward difference, central difference. The central difference approximations are more accurate.
If you are using the central difference then you formula is
This is valid for any j in
Since they didn't give you a point to use fix a j in the interval and calculate
This will give you point you are approximating the derivative at.
Calculate the numerical approximation and then calculate the actual derivative and evaluate it at the point.
The error is the difference between the two values.
Give this a try and post back if you get stuck.
As I'm looking at this, it doesn't look like anything we've covered in class. We've done this sort of thing for given data, so I knew what the h values were numerically, rather than in terms like this. I was wondering if this might be something I would need to do in MatLab? We had a matlab demo, but I didn't understand any of it (nor did my classmates) and haven't used matlab. Does this code look like anything I could use or alter to use?
% f(x), the function to integrate
% f= @(x) x^4-2*x ;
% f= @(x) exp(x);
f=@(x) sin(x);
% a, the lower limit of integration
a=0 ;
% b, the upper limit of integration
b=pi ;
% b=1.0;
% n, the number of segments. Note that this number must be even.
% n=20 ;
%************************************************* *********************
format long g
h=(b-a)/n ;
% Sum the odd index function values, and multiply by 4
sumOdd=0 ;
for i=1:2:n-1
sumOdd=sumOdd+f(a+i*h) ;
end
% Sum the even index function values, and multiply by 2
sumEven=0 ;
for i=2:2:n-2
sumEven=sumEven+f(a+i*h) ;
end
sum=4*sumOdd+2*sumEven+f(a)+f(b) ;
% Then multiply by h/3
approx=h/3*sum ;
%exact = quad(f,a,b) ;
%exact=exp(b)-exp(a);
exact=-cos(b)-(-cos(a));
error=abs(approx-exact);
disp(approx);
disp(exact);
disp(error);
I'm not familiar with some of the syntax, (specifically those @(x)'s) but there are a couple of obvious comments.
The numerical method being used is Simpson's Rule.
The program will not work because n is not defined. You need to remove the % sign from the statement % n= 20.
Hopefully then it will work, but as they say, (covering myself !), 'there's always one more bug'.