summaryrefslogtreecommitdiff
path: root/260/CH12/EX12.1/12_1.sce
blob: 2ee6e0e88e748fec897d86421c409f637ca5cbbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//Eg-12.1
//pg-507

clear
clc

x = 2.5;
h = 0.1;

deff('out = func(in)','out = in^3 -6*in^2 + 11*in -6')

//Forward Difference formulas

f(1) = (func(x+h) - func(x))/h;
f(2) = (-func(x+2*h)+4*func(x+h)-3*func(x))/(2*h);

//Backward Difference formulas

f(3) = (func(x)-func(x-h))/h;
f(4) = (3*func(x)-4*func(x-h)+func(x-2*h))/(2*h);

//Central Difference formulas

f(5) = (func(x+h)-f(x-h))/(2*h);
f(6) = (-func(x+2*h)+8*func(x+h)-8*func(x-h)+func(x-2*h))/(12*h);


printf('Value of derivative      Type    Order of error\n') 
printf('      %f         Forward       1\n',f(1)) 
printf('      %f         Forward       2\n',f(2)) 
printf('      %f         Backward      1\n',f(3)) 
printf('      %f         Backward      2\n',f(4)) 
printf('      %f         Central       2\n',f(5)) 
printf('      %f         Central       4\n\n',f(6)) 

printf('The exact value is  -0.25\nNote that the central difference formulas provide the best estimates of the derivative\n')