Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

differenting a function over [a,b] using Taylor method

Status
Not open for further replies.

milkypros

Technical User
Oct 25, 2009
11
hi
i am new in fortran programming and i need help on differenting a function over [a,b] using Taylor method. In example the function f(x)=x**3 and i want to calculate the quantity df=(f(x+h)-f(x))/h where h is the (b-a)/n and n is for example 20. Any help?
 
Thanks for answering. The code i wrote so far is:

dimension x(0:20)
f(x)=x**3
a=0
b=4
n=20
x(0)=a
x(n)=b
h=(b-a)/n
do i=0,n
x(i)=x(0)+i*h
y=(f(x(i)+h)-f(x(i)))/h
print*,x(i),y
end do
end

I dont know if this is correct. I want to print in one column the x(i) from a to b with increments of h, and in a second column i want to print the coresponting y
 
milkypros said:
I dont know if this is correct.
You should compile and run it and then you would see.

You don't need to use the array x. I would say, something like this does the work:
Code:
f(t)[COLOR=#804040][b]=[/b][/color]t[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]3[/color]
df(t) [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]3[/color][COLOR=#804040][b]*[/b][/color]t[COLOR=#804040][b]**[/b][/color][COLOR=#ff00ff]2[/color]
a[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color]
b[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]4[/color]
n[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]20[/color]
h[COLOR=#804040][b]=[/b][/color](b[COLOR=#804040][b]-[/b][/color]a)[COLOR=#804040][b]/[/b][/color]n
x[COLOR=#804040][b]=[/b][/color]a
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]10[/color], [COLOR=#ff00ff]'x'[/color], [COLOR=#ff00ff]'df_num'[/color], [COLOR=#ff00ff]'df_exact'[/color]
[COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]0[/color], n
  y[COLOR=#804040][b]=[/b][/color](f(x[COLOR=#804040][b]+[/b][/color]h)[COLOR=#804040][b]-[/b][/color]f(x))[COLOR=#804040][b]/[/b][/color]h
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]20[/color], x, y, df(x)
  x[COLOR=#804040][b]=[/b][/color]x[COLOR=#804040][b]+[/b][/color]h
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]format[/b][/color] (A4, [COLOR=#008080]3X[/color] , A6, [COLOR=#008080]4X[/color] , A8)
[COLOR=#6a5acd]20[/color] [COLOR=#804040][b]format[/b][/color] ([COLOR=#008080]f4.2[/color], [COLOR=#008080]4X[/color] , [COLOR=#008080]f5.2[/color], [COLOR=#008080]4X[/color] , [COLOR=#008080]f5.2[/color])
[COLOR=#a020f0]end[/color]
Output:
Code:
   x   df_num    df_exact
0.00     0.04     0.00
0.20     0.28     0.12
0.40     0.76     0.48
0.60     1.48     1.08
0.80     2.44     1.92
1.00     3.64     3.00
1.20     5.08     4.32
1.40     6.76     5.88
1.60     8.68     7.68
1.80    10.84     9.72
2.00    13.24    12.00
2.20    15.88    14.52
2.40    18.76    17.28
2.60    21.88    20.28
2.80    25.24    23.52
3.00    28.84    27.00
3.20    32.68    30.72
3.40    36.76    34.68
3.60    41.08    38.88
3.80    45.64    43.32
4.00    50.44    48.00
 
microm
thanks for answering. It works fine. The thing is that when i am trying to set a bigger value of n in order to compare the results, in example n>350, the program doesn't print all the results but it starts from values of x>1,02. whats the problem?
 
Maybe, because of the settings of your screen buffer.
Redirect the output of the program into a file using >
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top