## Homework 7, due November 11, 2010.

Due Thursday, November 11, 2010.
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Homework 7, due November 11, 2010.

Reading: The chapter on functions and subprograms.

Collaborative Problem: Write a FORTRAN program that:
1. has a double precision function called "mysine" that uses your series approximation from before to compute the sine of an angle;
2. an integer function called "fact" that computes the factorial and is used in the sine series by the sine function;
3. prints out the output from your function and compares it with the built-in sin() function for angles of 0.1, 0.2 ... 1.0 (radians);
4. has "implicit none" at the top.
If I did it correctly, when I wrote the program there was very little error if I carried the series out to 5 terms (through the 9th power).

Individual Problem: Modify your collaborative problem to try the following:
1. have the mysine function call itself from inside and indicate what happens;
2. have mysine call fact (which it should already do) and then have fact call mysine and indicate what happens;
3. give the nonmodified mysine and integer argument and fact a double precision argument and indicate what happens;
4. have a function have itself as its argument, e.g., mysine(mysine(.1)) and indicate what happens.
In all cases, by "indicate what happens" tell if pgf77 compiles it or not, whether it gives a warning, and if it can run whether it gives the right answer.
Bill Goodwine, 376 Fitzpatrick
edochert

### Re: Homework 7, due November 11, 2010.

Professor, is there any special way to declare functions as double precision? I am wondering because when I declare the arguments and the function as real values I get the desired result, but when I switch to double precision the values are passed as zeros to my function. Any ideas?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

edochert wrote:Professor, is there any special way to declare functions as double precision? I am wondering because when I declare the arguments and the function as real values I get the desired result, but when I switch to double precision the values are passed as zeros to my function. Any ideas?
Is everything declared as double precision, even the arguments that go into computing it? If that doesn't work, email your code to me.
Bill Goodwine, 376 Fitzpatrick
jrumpza

### Re: Homework 7, due November 11, 2010.

Professor,
Should we upload/print out programs for each of the individual exercises or just turn in a piece of paper indicating what happens?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

jrumpza wrote:Professor,
Should we upload/print out programs for each of the individual exercises or just turn in a piece of paper indicating what happens?
You don't have to print it out for everything.
Bill Goodwine, 376 Fitzpatrick
mmarszow

### Re: Homework 7, due November 11, 2010.

Professor,

For the mysine function, should we allow the function user to input the number of terms to carry out the approximation to, or should we just pick an arbitrary number?
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

mmarszow wrote:Professor,

For the mysine function, should we allow the function user to input the number of terms to carry out the approximation to, or should we just pick an arbitrary number?
Don't pick an arbitrary number. Experiment to find a number that gives a good answer but doesn't loop through stuff unnecessarily.
Bill Goodwine, 376 Fitzpatrick
jgalezie

### Re: Homework 7, due November 11, 2010.

Could you please post the programs from last Thursday's class? Thanks.

John Galeziewski
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

jgalezie wrote:Could you please post the programs from last Thursday's class? Thanks.
I posted them last Thursday. If they aren't there, let me know because that means the link disappeared.
Bill Goodwine, 376 Fitzpatrick
zharris

### Re: Homework 7, due November 11, 2010.

Prof,
I am having a similar problem to edochert above. My Collaborative program will run fine when everything is set as real but when I change it to double precision, it won't work. I think it might be having issues getting into the subprograms because it doesn't print out anything from inside of them. Do you know why this is?
Thanks
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

zharris wrote:Prof,
I am having a similar problem to edochert above. My Collaborative program will run fine when everything is set as real but when I change it to double precision, it won't work. I think it might be having issues getting into the subprograms because it doesn't print out anything from inside of them. Do you know why this is?
Thanks
You need to declare any variables you use in the function, including the variables you pass as arguments, as double precision both in the function and in the main program.
Bill Goodwine, 376 Fitzpatrick
sschwane

### Re: Homework 7, due November 11, 2010.

Professor Goodwine,
My program seems to be working correctly when I use a double precision function for MYSINE. However, as soon as I try to implement a factorial function into the MYSINE function, it starts giving me different values. I declared the variables inside the factorial function and the code in the factorial function is exactly the same as it was before I moved it out of the MYSINE function. Do you have any ideas as to why it's not working?
Thanks
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

sschwane wrote:Professor Goodwine,
My program seems to be working correctly when I use a double precision function for MYSINE. However, as soon as I try to implement a factorial function into the MYSINE function, it starts giving me different values. I declared the variables inside the factorial function and the code in the factorial function is exactly the same as it was before I moved it out of the MYSINE function. Do you have any ideas as to why it's not working?
Thanks
Are you declaring everything you are using in every function? I'd recommend just declaring everything everywhere.
Bill Goodwine, 376 Fitzpatrick
zharris

### Re: Homework 7, due November 11, 2010.

Prof,
When you ask us to give the nonmodified mysine and integer argument and fact a double precision argument, do you mean to just add an extra variable to definition of the function of the specified type such as: 'FUNCTION fact (double precision)' or to call the function in our main program using a variable of the specified type?
Thanks
goodwine
Posts: 1596
Joined: Tue Aug 24, 2004 4:54 pm
Location: 376 Fitzpatrick
Contact:

### Re: Homework 7, due November 11, 2010.

zharris wrote:Prof,
When you ask us to give the nonmodified mysine and integer argument and fact a double precision argument, do you mean to just add an extra variable to definition of the function of the specified type such as: 'FUNCTION fact (double precision)' or to call the function in our main program using a variable of the specified type?
Thanks
No, I don't mean to add a variable. I mean to try to use it with the wrong type of argument and say whether the compiler says something or something goes wrong when you run it or if everything seems ok.
Bill Goodwine, 376 Fitzpatrick
fvisovsk

Frank Visovsky