Create a MATLAB script to find the first and second derivati

Create a MATLAB script to find the first and second derivative of given function using Forward, Backward, central and Taylor numerical schemes. Test your code using the following functions:

a. 1 32)( 2 ++= xxxexf x and find f’(3) and f’’(3) for with h = 0.1, 0.01 and 0.001

b. Approximate y’(1) and y’’(1) using the following table:

x f(x)
0.8 0.992
0.9 0.999
1.0 1.0
1.1 1.001
1.2 1.008

Solution

a) Matlab Code

f= @(x) x*exp(x)+3*x^2+x-1;
h = [0.1 0.01 0.001];
% for h=0.1
firstorder_forward1 = (f(3+h(1))-f(3))/h(1)
firstorder_backward1 = (f(3)-f(3-h(1)))/h(1)
firstorder_central1 = (f(3+h(1))-f(3-h(1)))/(2*h(1))
secondorder_central1 = (f(3+h(1))-2*f(3)+f(3-h(1)))/h(1)^2
% for h=0.01
firstorder_forward2 = (f(3+h(2))-f(3))/h(2)
firstorder_backward2 = (f(3)-f(3-h(2)))/h(2)
firstorder_central2 = (f(3+h(2))-f(3-h(2)))/(2*h(2))
secondorder_central2 = (f(3+h(2))-2*f(3)+f(3-h(2)))/h(2)^2
% for h=0.001
firstorder_forward3 = (f(3+h(3))-f(3))/h(3)
firstorder_backward3 = (f(3)-f(3-h(3)))/h(3)
firstorder_central3 = (f(3+h(3))-f(3-h(3)))/(2*h(3))
secondorder_central3 = (f(3+h(3))-2*f(3)+f(3-h(3)))/h(3)^2

b) Matlab Code

    x = [0.8 0.9 1.0 1.1 1.2];
f = [0.992 0.999 1.0 1.001 1.008];
firstorder_forward = (f(4)-f(3))/0.1
firstorder_backward = (f(3)-f(2))/0.1
firstorder_central = (f(4)-f(2))/0.2
secondorder_central = (f(4)-2*f(3)+f(2))/0.1^2
secondorder_higher = (-f(1)+16*f(2)-30*f(3)+16*f(4)-f(5))/(12*0.1^2)

Create a MATLAB script to find the first and second derivative of given function using Forward, Backward, central and Taylor numerical schemes. Test your code u

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site