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.
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:
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.
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:
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
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)
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.