IDL Exercise 5
The purpose of this exercise is to write and execute a function and to see how information is passed between program modules. See the documentation on functions for additional information.
 
  1. A simple function has the form shown below. Type this information into the IDL edit window and save it in a file named modelfun.pro that is in your IDL path.
  2. FUNCTION modelfun,a
    y=3*a
    RETURN,y
    END

    modelfun.pro is a text file that contains the above statements. It could be created with any text editor.

    The first line contains three pieces of information: the identification of the file as a function (FUNCTION), the name of the function (modelfun), and a parameter (a) that will be used to pass information in to the function.

    The second line is an IDL statement that computes an internal value, y. It could be followed by more statements to do additional computations.

    The third line contains the value that should be returned to the program that calls modelfun.

    The END statement tells the compiler that the function is complete.

  3. You can use the function you wrote in the last step. Enter the following commands at the IDL command line
  4. s=[1,2,3,4]
    t=modelfun(s)
    print,'t=',t

    You should get the following items printed in the IDL output window

    IDL> s=[1,2,3,4]
    IDL> t=modelfun(s)
    % Compiled module: MODELFUN.
    IDL> print,'t=',t
    t=       3       6       9      12

    Facts obtained from this simple example:

    1. The argument of the function, s, does not have to be the same letter as the parameter, a, that was used in the definition.
    2. The argument does not have to be a scalar number. It can be an array. This function works on arrays of any size.
    3. The value that is returned is assigned to t by the assignment statement.
    4. Your program was compiled before the result was computed. If you edit the program you will have to save it and then tell the computer to compile it again before the changes you make are used. You can compile a program by using the COMPILE command under the RUN menu.
  5. You will now modify the program so that it will compute something else.  Change the program file so it reads as shown below. The line to change is shown in green. Save the file, but do not compile it yet.
  6. FUNCTION modelfun,a
    y=3*a-8
    RETURN,y
    END

    Type the following instructions in the command line.

    s=[1,2,3,4]
    t=modelfun(s)
    print,'t=',t

    IDL> s=[1,2,3,4]
    IDL> t=modelfun(s)
    IDL> print,'t=',t
    t=       3       6       9      12

    Note that now the function is not compiled again and the result is the same as it was before you made the changes. Now compile the function by selecting "COMPILE modelfun.pro" under the RUN menu. Repeat the above commands and you should see the following.

    IDL> s=[1,2,3,4]
    IDL> t=modelfun(s)
    IDL> print,'t=',t
    t=     -5      -2       1       4

    The changes that you made are now in effect.

  7. Modify the modelfun.pro file so that you can compute r=(z^2)*cos(z). Give the function the name newfun and store it in a file named newfun.pro. (Use "save as" to give the file the new name.)
  8. Create a vector of values for z and compute a result for r using your program. The program below would draw a graph of  r as a function of z. Compare your result with the graph shown.

    z=FINDGEN(101)/25
    r=newfun(z)
    plot,z,r

    The above commands do the following things:

    1. Make a vector of 101 values that cover the range [0,4] and give the vector the name z.
    2. Pass z to the function newfun as an argument. The function results are returned and assigned to a variable named r.
    3. Both z and r are passed to a procedure named PLOT, which draws the graph.

     
  9. More than one parameter can be passed to a function. One simply puts them into a list that follows the function name.
  10. Suppose that we wanted to create a function that would compute a polynomial with some given coefficients over a set of values contained in a vector x. Imagine that the polynomial is y=a0+a1*x+a2*x^2+a3*x^3. We could then write a function like that given below:

    FUNCTION polyone,a,b,c,d,x
    y=a+b*x+c*x^2+d*x^3
    RETURN,y
    END

    Save the above code in a file named polyone.pro. Then enter the following at the command line. You should get a plot like that shown below.

    z=FINDGEN(101)/25-2
    y=polyone(1,1,2,-2,z)
    PLOT,z,y

  11. The function polyone requires that we provide exactly four coefficients in a list. This can be made less cumbersome by making the coefficients be terms in a vector. We will do this as the first step in making a more general function. Modify polyone so that it reads as shown below, and save it in a file polytwo.pro
  12. FUNCTION polytwo,a,x
    y=a[0]+a[1]*x+a[2]*x^2+a[3]*x^3
    RETURN,y
    END

    Enter the following commands. You should get an plot identical to the previous plot.

    z=FINDGEN(101)/25-2
    p=[1,1,2,-2]
    y=polytwo(p,z)
    PLOT,z,y

    The function polytwo has the advantage that all of the coefficients can be passed as one parameter. The values are contained in a vector. The number of terms in the coefficient vector must still be four, but we will remove that restriction by an improvement we make below.

    The function as it is written above is inefficient because it does too much multiplication. It would be better to compute x^3 by multiplying x^2 by x, and so on. This can be accomplished by writing the calculation statement as a nested set of terms. This also leads us along the way to the general-purpose algorithm. Change your program as shown below. Save it as polythree.pro.

    FUNCTION polythree,a,x
    y=((a[3]*x+a[2])*x+a[1])*x+a[0]
    RETURN,y
    END

    When you enter the following command you should get the same plot as you did before.

    PLOT,z,polythree(p,z)

  13. We will now change the function so the polynomial can have any number of coefficients. The function will use a built-in IDL function named N_ELEMENTS to find out how many coefficients there are. It will then use a computational loop to combine the terms. The loop is called a FOR loop, which we will study very soon. You can look up information about FOR in the IDL online help. A statement like
  14. FOR k=m,n,step DO statement does whatever you write in statement with k=m, then with k=m+step, and so on until k reaches n.

    Create a file polyfour.pro containing the following code.

    FUNCTION polyfour,a,x
    n=N_ELEMENTS(a)
    y=a[n-1]
    FOR k=n-2,0,-1 DO y=y*x+a[k]
    RETURN,y
    END

    Create plots for several different polynomials. You do this by entering the coefficients in a vector. Remember to enter 0 for any coefficient that has zero value. The program can be used for any range of values for x.

  15. Programming assignment: Write a function program that will compute y=a*cos(f*x)+b*sin(f*x), where a,b and f are numerical parameters and x is a vector of variables. Make plots of y as a function of x for several combinations of values for a,b,f.

  16.