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!

Class Project

Status
Not open for further replies.

frtran90

Programmer
Feb 19, 2009
11
US
Hey guys. I've been assigned this project for my intro to fortran90 class and I have no idea where to begin. Could someone give me a sample code for it?

The following data represents discrete values of a function for which an explicit formula is not known:

1.123400 167.5600

2.246800 137.6441

3.370200 110.2523

4.493600 85.38444

5.617000 63.04068


One can, however, use linear interpolation to approximate the f(x) – value for any given x – value between the smallest and the largest values x – values. First, find the two x – values xi and xi+1 , in the list that bracket the given x – value, and then interpolate to find the corresponding f(x) – value:

f(xi+1) - f(xi)

f(x) = f(xi) + --------------- (x - xi)

xi+1 - xi

(If the value is out of range, print a message.) Test your program with the following x-values: -7.8, 1.1234, 13.65, 22.5, 23.5914, 25, 25.085, and 33.8.
 
Thanks. Hopefully I can figure things out from there!
 
But as I see from your assignment, you have probably to do only the linear interpolation instead of polynomial interpolation.
If it's true then instead of Lagrange interpolation use this:
For 2 points [X(i), Y(i)] and [X(i+1), Y(i+1)] and a given x, so that X(i) < x < X(i+1), the interpolated value of y will be computed according to this simple formula:
Code:
y = X(i) + (Y(i+1)-Y(i))/(X(i+1)-X(i))*(x - X(i))

 
i think your equation is close, but my teacher told us to use this one: (yes i know that f(x) is the same thing as y)

f(x) = f(i) +[f(i+1)-f(i)] / [x(i+1)-x(i)] * (xo -xi)

xo is the value to be input by the user i guess.

I've gotten some of the program written out already but this is one of the parts im having a problem with... I put all the x values from above into a file that i'm going to open within my program:

open(unit=1, file='measurement.dat', status='old', action='read', iostat=status)
fileopen: if(status==0) then

I've also got an allocatable array declared as:
real, dimension:)), allocatable::x

how do i get the x values saved in 'measurement.dat' to go into the allocatable array x?
 
Hi frtran90,

Thanks for the correction. It should be
Code:
y = Y(i) + (Y(i+1)-Y(i))/(X(i+1)-X(i))*(x - X(i))
where x is the given x-coordinate and y is the fuction value to be computed. Now this is the same as you mean.
 
how do i get the x values saved in 'measurement.dat' to go into the allocatable array x?

Example:

Given is this file which contains in the first column the X-data and in the second column the Y-data:

measurement.dat
Code:
1.123400 167.5600
2.246800 137.6441
3.370200 110.2523
4.493600 85.38444
5.617000 63.04068
The following code shows you how to read the data from the file into the arrays X and Y:

read_measurement.f95
Code:
[COLOR=#a020f0]program[/color] read_measurement
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: fname
[COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, nr_elements, stat
[COLOR=#2e8b57][b]real[/b][/color] :: dummy
[COLOR=#2e8b57][b]real[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color] :: x(:), y(:)

fname[COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'measurement.dat'[/color]
[COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
[COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
  [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#0000ff]! count number of lines in the file[/color]
nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]do[/b][/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
  nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
[COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines
[COLOR=#0000ff]! allocate the arrays of given size [/color]
[COLOR=#804040][b]allocate[/b][/color] (x(nr_elements))
[COLOR=#804040][b]allocate[/b][/color] (y(nr_elements))

[COLOR=#0000ff]! read array elements form the file[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) x(i),y(i)
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

[COLOR=#0000ff]! print reading results [/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'number of data lines in the file ='[/color], nr_lines
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of X-values: '[/color]
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'X'[/color], x, nr_elements)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of Y-values: '[/color]
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'Y'[/color], y, nr_elements)

[COLOR=#0000ff]!!![/color]
[COLOR=#804040][b]stop[/b][/color]
[COLOR=#6a5acd]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'I/O error reading file !'[/color]
[COLOR=#a020f0]end program[/color] read_measurement

[COLOR=#0000ff]! ************** Functions/Procedures **************[/color]
[COLOR=#a020f0]subroutine[/color] print_array(array_name, array, n)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
[COLOR=#2e8b57][b]  real[/b][/color] :: array(n)
  [COLOR=#2e8b57][b]character[/b][/color] :: array_name

  [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=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], array(i)
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end subroutine[/color] print_array
Compilation:
Code:
$ g95 read_measurement.f95 -o read_measurement
Output:
Code:
$ read_measurement
 number of data lines in the file = 5

 Array of X-values: 
 X[ 1 ] =  1.1234
 X[ 2 ] =  2.2468
 X[ 3 ] =  3.3702
 X[ 4 ] =  4.4936
 X[ 5 ] =  5.617

 Array of Y-values: 
 Y[ 1 ] =  167.56
 Y[ 2 ] =  137.6441
 Y[ 3 ] =  110.2523
 Y[ 4 ] =  85.38444
 Y[ 5 ] =  63.04068
To see how it works, read the comments in source code.
 
Hi frtran90

Although you are not looking for the equation itself, it might be of interest to know that it is:

f(x) = x*x - 30*x + 200
 
Hi gullipe,

Indeed, it seems to be true. But how did you find that function out? Is therefore a software?
 
@ gullipe: how did you figure that out?
@ mikrom: thanks, that makes a lot of sense.

the last thing i cannot figure out is how to code this part in:

(If the value is out of range, print a message.) Test your program with the following x-values: -7.8, 1.1234, 13.65, 22.5, 23.5914, 25, 25.085, and 33.8.

I'm sure I will have to nest some type of if/then statement within a do loop, but i dont know how to assign x(i), x(i+1), y(i), and y(i+1) for specific x values.
 
This is what i have.. but it doesn't work, theres an error in the if statement

Code:
real:: testx
!now to input and figure out f(x)
write(*,*) 'Enter real number for X '
read(*,*) testx
write(*,*) 'Solving for F(X)...'

do i=1, nr_elements
read(1,*) x(i), y(i)
if(x(i) < testx < x(i+1)) then
write(*,*) 'Y = ', y(i) = (y(i+1) - y(i)) / (x(i+1)-(x(i))*&(testx - x(i))
else write(*,*) 'Error, X Value out of range!'
end if
end do
 
sorry, forgot to take out the &...

Code:
real:: testx
!now to input and figure out f(x)
write(*,*) 'Enter real number for X '
read(*,*) testx
write(*,*) 'Solving for F(X)...'

do i=1, nr_elements
read(1,*) x(i), y(i)
if(x(i) < testx < x(i+1)) then
write(*,*) 'Y = ', y(i) = (y(i+1) - y(i)) / (x(i+1)-(x(i))*(testx - x(i))
else write(*,*) 'Error, X Value out of range!'
end if
end do
 
Hi frtran90,

What are you studying, if you have to learn fortran?
 
Hi frtran90,
Here is what you probably want:
measurement_interpolation.f95
Code:
[COLOR=#a020f0]program[/color] measurement_interpolation
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]character[/b][/color]([COLOR=#ff00ff]30[/color]) :: fname
[COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, nr_elements, stat
[COLOR=#2e8b57][b]real[/b][/color] :: dummy, x0, y0, linear_interpolation
[COLOR=#2e8b57][b]real[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color] :: x(:), y(:)

fname[COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]'measurement.dat'[/color]
[COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
[COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
  [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#0000ff]! count number of lines in the file[/color]
nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
[COLOR=#804040][b]do[/b][/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
  nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
[COLOR=#804040][b]end do[/b][/color]

[COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
[COLOR=#6a5acd]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
[COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines

[COLOR=#0000ff]! allocate the arrays of given size [/color]
[COLOR=#804040][b]allocate[/b][/color] (x(nr_elements))
[COLOR=#804040][b]allocate[/b][/color] (y(nr_elements))

[COLOR=#0000ff]! read array elements from the file[/color]
[COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
  [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) x(i),y(i)
[COLOR=#804040][b]end do[/b][/color]
[COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

[COLOR=#0000ff]! print reading results [/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'number of data lines in the file ='[/color], nr_lines
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'x-values: '[/color]
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'x'[/color], x, nr_elements)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'y-values: '[/color]
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'y'[/color], y, nr_elements)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

[COLOR=#0000ff]! compute the interpolated function value[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Enter x0 = '[/color]
[COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) x0
[COLOR=#804040][b]if[/b][/color] ((x0 [COLOR=#804040][b].ge.[/b][/color] x([COLOR=#ff00ff]1[/color])) [COLOR=#804040][b].and.[/b][/color] (x0 [COLOR=#804040][b].le.[/b][/color] x(nr_elements))) [COLOR=#804040][b]then[/b][/color]
  y0[COLOR=#804040][b]=[/b][/color]linear_interpolation(x, y, nr_elements, x0)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'The interpolated value f('[/color],x0, [COLOR=#ff00ff]') = '[/color], y0
[COLOR=#804040][b]else[/b][/color]  
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'x0 ='[/color],x0, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
    [COLOR=#ff00ff]' is out of range <'[/color], x([COLOR=#ff00ff]1[/color]),[COLOR=#ff00ff]','[/color],x(nr_elements),[COLOR=#ff00ff]'>'[/color]
[COLOR=#804040][b]end if[/b][/color]

[COLOR=#0000ff]!!![/color]
[COLOR=#804040][b]stop[/b][/color]
[COLOR=#6a5acd]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'I/O error reading file !'[/color]
[COLOR=#a020f0]end program[/color] measurement_interpolation

[COLOR=#0000ff]! ************** Functions/Procedures **************[/color]
[COLOR=#a020f0]subroutine[/color] print_array(array_name, array, n)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
[COLOR=#2e8b57][b]  real[/b][/color] :: array(n)
  [COLOR=#2e8b57][b]character[/b][/color] :: array_name

  [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=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], array(i)
  [COLOR=#804040][b]end do[/b][/color]
[COLOR=#a020f0]end subroutine[/color] print_array

[COLOR=#2e8b57][b]real[/b][/color] [COLOR=#a020f0]function[/color] linear_interpolation(x, y, n, x0)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i, k
[COLOR=#2e8b57][b]  real[/b][/color] :: x(n), y(n), x0, y0
  k [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/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]
    [COLOR=#804040][b]if[/b][/color] ((x0 [COLOR=#804040][b]>=[/b][/color] x(i)) [COLOR=#804040][b].and.[/b][/color] (x0 [COLOR=#804040][b]<=[/b][/color] x(i[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]))) [COLOR=#804040][b]then[/b][/color]
      [COLOR=#0000ff]! k is the index where: x(k) <= x <= x(k+1)  [/color]
      k [COLOR=#804040][b]=[/b][/color] i
      [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'k ='[/color], k, [COLOR=#ff00ff]': x0 ='[/color],x0,   [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
        [COLOR=#ff00ff]'is from interval <x('[/color],k,[COLOR=#ff00ff]'), x('[/color],k[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]')> = <'[/color], [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
        x(k),[COLOR=#ff00ff]','[/color],x(k[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color]),[COLOR=#ff00ff]'>'[/color]
      [COLOR=#804040][b]exit[/b][/color] [COLOR=#0000ff]! exit loop[/color]
    [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]if[/b][/color] (k [COLOR=#804040][b]>[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]  
    [COLOR=#0000ff]! compute the interpolated value[/color]
    y0 [COLOR=#804040][b]=[/b][/color] y(k) [COLOR=#804040][b]+[/b][/color] (y(k[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]y(k))[COLOR=#804040][b]/[/b][/color](x(k[COLOR=#804040][b]+[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]-[/b][/color]x(k))[COLOR=#804040][b]*[/b][/color](x0[COLOR=#804040][b]-[/b][/color]x(k))
  [COLOR=#804040][b]else[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])[COLOR=#ff00ff]'Error computing the interpolation !!!'[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'x0 ='[/color],x0, [highlight #ffff00][COLOR=#0000ff]&[/color][/highlight]
     [COLOR=#ff00ff]' is out of range <'[/color], x([COLOR=#ff00ff]1[/color]),[COLOR=#ff00ff]','[/color],x(n),[COLOR=#ff00ff]'>'[/color]
  [COLOR=#804040][b]end if[/b][/color]
  [COLOR=#0000ff]! return value[/color]
  linear_interpolation [COLOR=#804040][b]=[/b][/color] y0
[COLOR=#a020f0]end function[/color] linear_interpolation
Output:
Code:
$ g95 measurement_interpolation.f95 -o measurement_interpolation

$ measurement_interpolation
 number of data lines in the file = 5

 x-values: 
 x[ 1 ] =  1.1234
 x[ 2 ] =  2.2468
 x[ 3 ] =  3.3702
 x[ 4 ] =  4.4936
 x[ 5 ] =  5.617

 y-values: 
 y[ 1 ] =  167.56
 y[ 2 ] =  137.6441
 y[ 3 ] =  110.2523
 y[ 4 ] =  85.38444
 y[ 5 ] =  63.04068

 Enter x0 = 
3.14
 k = 2 : x0 = 3.14 is from interval <x( 2 ), x( 3 )> = < 2.2468 , 3.3702 >
 The interpolated value f( 3.14 ) =  115.86525

$ measurement_interpolation
 number of data lines in the file = 5

 x-values: 
 x[ 1 ] =  1.1234
 x[ 2 ] =  2.2468
 x[ 3 ] =  3.3702
 x[ 4 ] =  4.4936
 x[ 5 ] =  5.617

 y-values: 
 y[ 1 ] =  167.56
 y[ 2 ] =  137.6441
 y[ 3 ] =  110.2523
 y[ 4 ] =  85.38444
 y[ 5 ] =  63.04068

 Enter x0 = 
1.1
 x0 = 1.1  is out of range < 1.1234 , 5.617 >

$ measurement_interpolation
 number of data lines in the file = 5

 x-values: 
 x[ 1 ] =  1.1234
 x[ 2 ] =  2.2468
 x[ 3 ] =  3.3702
 x[ 4 ] =  4.4936
 x[ 5 ] =  5.617

 y-values: 
 y[ 1 ] =  167.56
 y[ 2 ] =  137.6441
 y[ 3 ] =  110.2523
 y[ 4 ] =  85.38444
 y[ 5 ] =  63.04068

 Enter x0 = 
5.7
 x0 = 5.7  is out of range < 1.1234 , 5.617 >

But for your further growth, I would suggest you:
1. Read something about Fortran, e.g this little cca 100 pages handout: 2. Write programs. For example you can extend your program and write other function, which computes the interpolated value using interpolation polynomial of higher order according to the formulae by Lagrange
or by Newton
 
Hi both

How did I figure the formula out?
Well, that was easy. I just transported the figures into Excel, made a scatter diagram, added a polinomial trendline and displayed the equation on the chart. And there it was, simple and exact in this case ...
 
I am studying business but the I heard the class was really easy so I took it. Extremely mistaken.
 
...I heard the class was really easy so I took it. Extremely mistaken.
LOL [lol]

But seriously, IMHO Fortran is more useful for scientist and engineers, than for business people.
For learning to program it would be IMHO better to use Python.
You can do numerical computations in Python too and also more other things:
Text processing, database processing,...etc.

But on the other hand, Fortran is not so complicated as it seems.

And think at the philosopher's words:
"What does not kill me makes me stronger."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top