Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
x[1],..,x[i],..,x[n]
y[1],..,y[i],..,y[n]
for i=1,..,n-1:
dy[i] = (y(i+1]-y[i])/(x(i+1]-x[i])
modelling said:I read about it, but for me it is complicated to program such a routine.
[COLOR=#a020f0]program[/color] deriv
[COLOR=#0000ff]! declare variables[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: i
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: N [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]9[/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](N) :: x, y, dy
[COLOR=#0000ff]! assign data[/color]
x [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]0[/color]., [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]0.2[/color], [COLOR=#ff00ff]0.3[/color], [COLOR=#ff00ff]0.5[/color], [COLOR=#ff00ff]0.6[/color], [COLOR=#ff00ff]0.8[/color], [COLOR=#ff00ff]0.9[/color], [COLOR=#ff00ff]1[/color]. [COLOR=#804040][b]/[/b][/color])
y [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]0[/color]., [COLOR=#ff00ff]0.01[/color], [COLOR=#ff00ff]0.04[/color], [COLOR=#ff00ff]0.09[/color], [COLOR=#ff00ff]0.25[/color], [COLOR=#ff00ff]0.36[/color], [COLOR=#ff00ff]0.64[/color], [COLOR=#ff00ff]0.81[/color], [COLOR=#ff00ff]1[/color]. [COLOR=#804040][b]/[/b][/color])
[COLOR=#0000ff]! compute derivation[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
dy(i) [COLOR=#804040][b]=[/b][/color] (y(i[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]-[/b][/color] y(i)) [COLOR=#804040][b]/[/b][/color] (x(i[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]-[/b][/color] x(i))
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#0000ff]! computing last derivation using linear extrapolation[/color]
dy(N) [COLOR=#804040][b]=[/b][/color] dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]+[/b][/color] (dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]/[/b][/color](x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]*[/b][/color](x(N)[COLOR=#804040][b]-[/b][/color]x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]))
[COLOR=#0000ff]! print the results[/color]
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]10[/color]) [COLOR=#ff00ff]'x'[/color], [COLOR=#ff00ff]'y=f(x)'[/color], [COLOR=#ff00ff]'dy/dx'[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], N
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]20[/color]) x(i), y(i), dy(i)
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color](a10, a10, a10)
[COLOR=#6a5acd]20[/color] [COLOR=#804040][b]format[/b][/color]([COLOR=#008080]f10.2[/color], [COLOR=#008080]f10.2[/color], [COLOR=#008080]f10.2[/color])
[COLOR=#a020f0]end program[/color] deriv
$ g95 deriv.f90 -o deriv
$ deriv
x y=f(x) dy/dx
0.00 0.00 0.10
0.10 0.01 0.30
0.20 0.04 0.50
0.30 0.09 0.80
0.50 0.25 1.10
0.60 0.36 1.40
0.80 0.64 1.70
0.90 0.81 1.90
1.00 1.00 2.10
write ([COLOR=red]*[/color],'(3a10)') 'x', 'y=f(x)', 'dy/dx'
write([COLOR=red]1[/color],'(5f10.2)') x(i), y(i), c(2,i)
x y=f(x) dy/dx
0.00 0.00 0.10
0.10 0.01 0.17
0.20 0.04 0.41
0.30 0.09 0.60
0.40 0.16 0.80
0.50 0.25 1.00
0.60 0.36 1.20
0.70 0.49 1.40
0.80 0.64 1.60
0.90 0.81 1.80
1.00 1.00 2.00
1.10 1.21 2.20
1.20 1.44 2.40
1.30 1.69 2.59
1.40 1.96 2.83
1.50 2.25 2.90
1.60 2.56 3.57
1.70 2.89 2.02
1.80 3.24 8.77
1.90 3.61 3.70
n = 9
x = (/ 0., 0.1, 0.2, 0.3, 0.5, 0.6, 0.8, 0.9, 1. /)
y = (/ 0., 0.01, 0.04, 0.09, 0.25, 0.36, 0.64, 0.81, 1. /)
x y=f(x) dy/dx
0.00 0.00 0.10
0.10 0.01 0.17
0.20 0.04 0.40
0.30 0.09 0.61
0.50 0.25 0.94
0.60 0.36 1.38
0.80 0.64 0.66
0.90 0.81 4.52
1.00 1.00 1.90
I post the code between the tagsgullipe said:By the way, how do you put the code and data in these boxes with the "CODE" header and Courier font?
...
[COLOR=#a020f0]program[/color] deriv
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#0000ff]! declare variables[/color]
[COLOR=#2e8b57][b]integer[/b][/color] :: i
[COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]parameter[/b][/color] :: N [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]9[/color]
[COLOR=#2e8b57][b] real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](N) :: x, y, dy
[COLOR=#0000ff]! assign data[/color]
x [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]0[/color]., [COLOR=#ff00ff]0.1[/color], [COLOR=#ff00ff]0.2[/color], [COLOR=#ff00ff]0.3[/color], [COLOR=#ff00ff]0.5[/color], [COLOR=#ff00ff]0.6[/color], [COLOR=#ff00ff]0.8[/color], [COLOR=#ff00ff]0.9[/color], [COLOR=#ff00ff]1[/color]. [COLOR=#804040][b]/[/b][/color])
y [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]0[/color]., [COLOR=#ff00ff]0.01[/color], [COLOR=#ff00ff]0.04[/color], [COLOR=#ff00ff]0.09[/color], [COLOR=#ff00ff]0.25[/color], [COLOR=#ff00ff]0.36[/color], [COLOR=#ff00ff]0.64[/color], [COLOR=#ff00ff]0.81[/color], [COLOR=#ff00ff]1[/color]. [COLOR=#804040][b]/[/b][/color])
[COLOR=#0000ff]! compute derivation[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color], N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]
dy(i) [COLOR=#804040][b]=[/b][/color] (y(i[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]-[/b][/color] y(i[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])) [COLOR=#804040][b]/[/b][/color] (x(i[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]-[/b][/color] x(i[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]))
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#0000ff]! compute first and last derivation using linear extrapolation[/color]
dy([COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] dy([COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]+[/b][/color] (dy([COLOR=#ff00ff]3[/color])[COLOR=#804040][b]-[/b][/color]dy([COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]/[/b][/color](x([COLOR=#ff00ff]3[/color])[COLOR=#804040][b]-[/b][/color]x([COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]*[/b][/color](x([COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]x([COLOR=#ff00ff]2[/color]))
dy(N) [COLOR=#804040][b]=[/b][/color] dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]+[/b][/color] (dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]dy(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]/[/b][/color](x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]2[/color]))[COLOR=#804040][b]*[/b][/color](x(N)[COLOR=#804040][b]-[/b][/color]x(N[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color]))
[COLOR=#0000ff]! print the results[/color]
[COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(3a10)'[/color]) [COLOR=#ff00ff]'x'[/color], [COLOR=#ff00ff]'y=f(x)'[/color], [COLOR=#ff00ff]'dy/dx'[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], N
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(3f10.2)'[/color]) x(i), y(i), dy(i)
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end program[/color] deriv
$ g95 deriv.f90 -o deriv
$ deriv
x y=f(x) dy/dx
0.00 0.00 -0.00
0.10 0.01 0.20
0.20 0.04 0.40
0.30 0.09 0.70
0.50 0.25 0.90
0.60 0.36 1.30
0.80 0.64 1.50
0.90 0.81 1.80
1.00 1.00 2.10
Program PPolyn
implicit none
real*8 x(100),y(100),dydx(100)
real*8 dd
real*8 r1,r2,r3,r4,s1,s2
real*8 A,B,C
integer*4 i,n
n = 19
x(1) = 0.0
do i=2,n
dd = 0.1
if((i.eq.5).or.(i.eq.8)) dd = 0.2
x(i) = x(i-1)+dd
c y(i) = x(i)**2
y(i) = (1./3.)*x(i)**3
enddo
open(1,file='out.txt',status='unknown')
write (*,'(3a10)') 'x', 'y=f(x)', 'dy/dx'
write (1,'(3a10)') 'x', 'y=f(x)', 'dy/dx'
do i=2,n-1
s1 = (x(i-1)-x(i))*(x(i)**2-x(i+1)**2)
s2 = (x(i)-x(i+1))*(x(i-1)**2-x(i)**2)
r1 = (y(i-1)-y(i))*(x(i)-x(i+1))
r2 = (y(i)-y(i+1))*(x(i-1)-x(i))
r3 = (y(i-1)-y(i))*(x(i)**2-x(i+1)**2)
r4 = (y(i)-y(i+1))*(x(i-1)**2-x(i)**2)
A = (r1-r2)/(s2-s1)
B = (r3-r4)/(s1-s2)
C = y(i) - A*x(i)**2 - B*x(i)
dydx(i) = 2*A*x(i) + B
enddo
do i=2,n-1
write(*,'(3f10.2)') x(i), y(i), dydx(i)
write(1,'(3f10.2)') x(i), y(i), dydx(i)
enddo
end
x y=f(x) dy/dx
.10 .00 .01
.20 .00 .04
.30 .01 .10
.50 .04 .26
.60 .07 .36
.70 .11 .50
.90 .24 .82
1.00 .33 1.00
1.10 .44 1.21
1.20 .58 1.44
1.30 .73 1.69
1.40 .91 1.96
1.50 1.13 2.25
1.60 1.37 2.56
1.70 1.64 2.89
1.80 1.94 3.24
1.90 2.29 3.61
x y=f(x) dy/dx real
.00 1.00 1.00 1.00
.10 1.11 1.11 1.11
.20 1.22 1.22 1.22
.30 1.35 1.35 1.35
.50 1.65 1.65 1.65
.60 1.82 1.83 1.82
.70 2.01 2.02 2.01
.90 2.46 2.47 2.46
1.00 2.72 2.72 2.72
1.10 3.00 3.01 3.00
1.20 3.32 3.33 3.32
1.30 3.67 3.68 3.67
1.40 4.06 4.06 4.06
1.50 4.48 4.49 4.48
1.60 4.95 4.96 4.95
1.70 5.47 5.48 5.47
1.80 6.05 6.06 6.05
1.90 6.69 6.70 6.69
2.00 7.39 7.37 7.39
do i=2,n-1
s1 = (x(i-1)-x(i))*(x(i)**2-x(i+1)**2)
s2 = (x(i)-x(i+1))*(x(i-1)**2-x(i)**2)
r1 = (y(i-1)-y(i))*(x(i)-x(i+1))
r2 = (y(i)-y(i+1))*(x(i-1)-x(i))
r3 = (y(i-1)-y(i))*(x(i)**2-x(i+1)**2)
r4 = (y(i)-y(i+1))*(x(i-1)**2-x(i)**2)
A = (r1-r2)/(s2-s1)
B = (r3-r4)/(s1-s2)
C = y(i) - A*x(i)**2 - B*x(i)
dydx(i) = 2*A*x(i) + B
if(i.eq.2) dydx(i-1) = 2*A*x(i-1) + B
if(i.eq.(n-1)) dydx(i+1) = 2*A*x(i+1) + B
enddo
x y=f(x) dy/dx real
.00 .00 1.00 1.00
.10 .10 .99 1.00
.20 .20 .98 .98
.30 .30 .95 .96
.50 .48 .87 .88
.60 .56 .82 .83
.70 .64 .76 .76
.90 .78 .62 .62
1.00 .84 .54 .54
1.10 .89 .45 .45
1.20 .93 .36 .36
1.30 .96 .27 .27
1.40 .99 .17 .17
1.50 1.00 .07 .07
1.60 1.00 -.03 -.03
1.70 .99 -.13 -.13
1.80 .97 -.23 -.23
1.90 .95 -.32 -.32
2.00 .91 -.42 -.42
A*x1**2 + B*x1 + C = y1
A*x2**2 + B*x2 + C = y2
A*x3**2 + B*x3 + C = y3