# Fortran Program to plot multiple datasets on a single graph in Fortran95

Summary
Hi, I am new to the forum &New to Fortran: HELP!

Trying to put together a program to calculate on a single graph, a plot the following:
*the luminosity distance,
*the present proper distance,
*the angular diameter distance,
*the relativistic Hubble law distance estimate and
*the non-relativistic Hubble law distance for values of z between 0 and 4. Expressed these in units of c/H_0

What I have done so far is below: still cant get it to just give me just me 5 rows of values.
Fortran:
PROGRAM Test
IMPLICIT NONE

REAL, PARAMETER :: Omega_m0 = 0.27, Omega_rel0 = 8.24E-5, Omega_L0 = 0.73, Omega_0 = 1.02

REAL :: z_max, z, dz
REAL :: I, g_z, g_zplus, S
REAL :: d_p0, d_L, d_A, d_nr, d_r

INTEGER :: n = 100000

OPEN(UNIT = 10)

z_max = 0

DO WHILE (z_max <= 4.001)
dz = z_max/n

!integrand of Eq. (29.168) z=0 g_z = 1/SQRT(Omega_m0*(1 + z)**3 + Omega_rel0*(1 + z)**4 + Omega_L0 + (1 - Omega_0)*(1 + z)**2)

I = 0

DO WHILE (z < z_max)
z = z + dz
g_zplus = 1/SQRT(Omega_m0*(1 + z)**3 + Omega_rel0*(1 + z)**4 + Omega_L0 + (1 - Omega_0)*(1 + z)**2)

I = (I + g_z)*(dz/2)

END DO

S = SIN(I*SQRT(Omega_0 - 1))/sqrt(Omega_0 - 1)  !Eq. (29.174) for Omega_0 > 1

d_L = S*(1 + z_max)                                 !Eq. (29.184) in units of c/H_0
d_p0 = I                                                      !Eq. (29.169) in units of c/H_0
d_A = d_L/(1 + z_max)**2                       !Eq. (29.192) in units of c/H_0
d_r = ((z + 1)**2 - 1)/((z + 1)**2 + 1)     !Eq. (27.7) in units of c/H_0
d_nr = z                                                       !Eq. (27.8) in units of c/H_0

WRITE(10,'(7E13.6)') z, d_L, d_p0, d_A, d_r, d_nr, ABS((d_L - d_r)/d_L)

z_max = z_max + 0.01
END DO

PAUSE
STOP
END

Last edited by a moderator:
Related Programming and Computer Science News on Phys.org

#### jedishrfu

Mentor
Try putting print statements after key calculations to see if what you get is what you expect. I’d put one in your while loop too to see how many iterations it does.

If you have a FORTRAN debugger that would be even better. You could step thru your program line by line and verify results.

When you first calculate dz on line 17 it will be zero right? because zmax is zero, is that expected?

Greetings

I'm pretty much flying by the seat of my pants on this..using notes and manuals to try and nut this out. I should note that programming is not my thing, but the task is to try a simple program to answer a question. Yes, it will be zero (i think). I'm unfamiliar with debuggers and fortran in general, as said, I've tried to piece this together with trial and error and bits and pieces through forums.

It would be great for someone to point out the areas that I have got wrong in this. Ive been tearing hair out for a few weeks on this.

Really need a fortran guru, who can say...this is where you are wrong..and can fix it by doing this.

Cheers Petra

#### FactChecker

Gold Member
2018 Award
Your code looks pretty good, especially for a beginner. You don't say if the problem is when it compiles or when it runs. Does it currently write anything? Does the program end without writing more than 5 lines? Or do you kill it? As @jedishrfu suggested, you can put some print statements inside the loops. Or you can write to the same file or to another file. I suggest making a smaller test version where the loops iterate fewer times. The write statements put the information in a buffer that only gets flushed to the file periodically. So if you kill the program, you will probably not see the last of the written output.

Greetings there
I tested it on an online compiler and it gave me 7 lines of numbers..which I only need 5. I don't know what "print statements inside loops means"?

I don't know "Or do you kill it?" means

I don't know "can write to the same file or to another file. I suggest making a smaller test version where the loops iterate fewer times. The write statements put the information in a buffer that only gets flushed to the file periodically" means either.

Seriously, i've had to piece together from various tutorials and manuals.. I'm really stuck - can someone run it and tell me at which point I have gone wrong, please.

Cheers

#### DrClaude

Mentor
What I have done so far is below: still cant get it to just give me just me 5 rows of values.
Fortran:
WRITE(10,'(7E13.6)') z, d_L, d_p0, d_A, d_r, d_nr, ABS((d_L - d_r)/d_L)
There are 7 elements in that write statement, therefore, 7 numbers are printed. If you want only 5, you need change that statement.

#### FactChecker

Gold Member
2018 Award
Are you talking about the wrong number of rows (lines printed) or the number of columns (values on each line)?
You say there are 7 rows when you want 5 rows.
But z_max starts at 0, increments by 0.01 and ends when z_max >= 4.001. So that should be printing hundreds of rows.

If you are talking about the only wanting 5 numbers on each line, that is different, and @DrClaude has the solution -- change the write statement on line 40 of your code. Change the '7' to a '5' and remove two of the variables to print.

#### Klystron

Gold Member
A few general hints from an old FORTRAN 77 programmer:

You initialize REAL variable z_max to zero. You can zero or initialize all your variables. Helps spot typos and makes your code more portable. Takes very little computer time.​
Iteration (looping) is a powerful tool. Try adding DEBUG statements within your loops such as @jedishrfu and @FactChecker describe that write your iteration control variables to the screen and/or an output file along with intermediate computations. You can comment out these statements after debugging. Also consider PAUSE debug statements, say after 5 iterations to give you a chance to read the values and avoid runaways.​
IIRC FORTAN lets you use "constant variables". You can define INTEGER num_rows = 100, num_cols = 5 then use these 'constants' so you only need to change them once. Members with more current knowledge can fix my mistakes for FORTRAN 95, but the idea is to avoid hard coding numbers deep in your code and place them at the top for easier debugging.​

Are you talking about the wrong number of rows (lines printed) or the number of columns (values on each line)?
You say there are 7 rows when you want 5 rows.
But z_max starts at 0, increments by 0.01 and ends when z_max >= 4.001. So that should be printing hundreds of rows.

If you are talking about the only wanting 5 numbers on each line, that is different, and @DrClaude has the solution -- change the write statement on line 40 of your code. Change the '7' to a '5' and remove two of the variables to print.
Hi there,

I should have 5 columns of numbers to then plot on an x y axis. So all five sets of data should appear on the same graph at the end of the day. I'm okay with excel and doing the plot- i just need to generate the 5 columns of info.

I should have quite a few numbers in each column - prob about 400 rows in each column. So im just confused as to how i can program this properly.

I'm sorry to be a pain, but I literally only started getting my head around this in the last month, so im really really new to this program and programming in general.

Cheers

#### FactChecker

Gold Member
2018 Award
What do you want in your 5 columns? As @DrClaude has said, your code on line 40 will make 7 columns:

WRITE(10,'(7E13.6)') z, d_L, d_p0, d_A, d_r, d_nr, ABS((d_L - d_r)/d_L)

You will need to decide which two variables to remove and change the '7' to a '5'.

Fortran:
WRITE(10,'(7E13.6)') z, d_L, d_p0, d_A, d_r, d_nr, ABS((d_L - d_r)/d_L)
There are 7 elements in that write statement, therefore, 7 numbers are printed. If you want only 5, you need change that statement.
These are the five sets of data I should have so I can plot on an x y axis. I think I have more information than I need. I would be helpful - someone could tell me what I do and dont need in the code.

d_L = S*(1 + z_max) !Eq. (29.184) in units of c/H_0
d_p0 = I !Eq. (29.169) in units of c/H_0
d_A = d_L/(1 + z_max)**2 !Eq. (29.192) in units of c/H_0
d_r = ((z + 1)**2 - 1)/((z + 1)**2 + 1) !Eq. (27.7) in units of c/H_0
d_nr = z !Eq. (27.8) in units of c/H_0

z = redshift will appear on the x axis *max value (4)
distance (c/Ho) = will appear on the y axis *max value (10)

In both cases the iterations are very small - so that's why there are lots of values in each column.

Does that make sense?

Cheers

#### FactChecker

Gold Member
2018 Award
Do you know what document is being referred to in the comments? This is not a FORTRAN programming issue. It is an issue with understanding the physics equations. As such, it is beyond my ability to help. I suggest that this question be moved to the section on astronomy (I guess).

Do you know what document is being referred to in the comments? This is not a FORTRAN programming issue. It is an issue with understanding the physics equations. As such, it is beyond my ability to help. I suggest that this question be moved to the section on astronomy (I guess).
I understand the physics and the astronomy, all I am doing is to trying to organise the programing to run these equations. I can get the info to run, but some reason I'm getting more information than I need.

It would be great if someone has a Fortran95 background and can run a quick eye over this and just point out the excess info, Im pretty sure that the maths is right just in the wrong area to make it work.

Cheers

#### FactChecker

Gold Member
2018 Award
It isn't necessary to skip calculations just because you don't want to output them all in the columns. If you understand the equations, then identify the variables that you want to put in your file and include them in the write statement. Eliminate any unnecessary ones from the write statement.

Greetings
The only error I am getting when i run it is this, can you explain this part

(40) : error 564 - Number found where an operator was expected
Compilation failed.

#### FactChecker

Gold Member
2018 Award
Is this new? Did you change line 40?

I'm trying to nut out the problem and this is the error i received. Can you suggest someone on the forum who may be able to assist with this. I do appreciate your assistance, but I'm not getting anyway with this atm.

Cheers

#### PeterDonis

Mentor
I'm not getting anyway with this atm
You're not getting anywhere because you're not giving enough information for other people to help you. You need to show an explicit listing of the program and an explicit transcript of what commands you issue to compile and run it and the output from each command. It would also be helpful to know what operating system you are running on. We can't read your mind and we can't see your computer; the only information we have is what you post here.

#### FactChecker

Gold Member
2018 Award
Apparently there is something wrong suddenly in code line 40. What is that line now? We can't guess at it.

You're not getting anywhere because you're not giving enough information for other people to help you. You need to show an explicit listing of the program and an explicit transcript of what commands you issue to compile and run it and the output from each command. It would also be helpful to know what operating system you are running on. We can't read your mind and we can't see your computer; the only information we have is what you post here.
Greetings
Sorry a little confused here, what do you mean by explicit listing? Sorry not sure what you mean here, at the beginning of the post - I posted all the information of the program. I'm running windows and using Plato32 to compile. Used fortran95 manual for the program language.
I dont know all the ins and outs of how a debugger works, as stated just flying by the seat of my pants on this.

Im not a programmer - some of the language you are using in the responses I dont understand. Im just a mature aged student - trying to nut this out. Thank you for your patience on this.

Cheers

I went back to the original code I had at the start of the post - because I deleted to much. and destroyed all the work I had done.

So back to where I began. :-(

Cheer

#### FactChecker

Gold Member
2018 Award
I went back to the original code I had at the start of the post - because I deleted to much. and destroyed all the work I had done.

So back to where I began. :-(

Cheer
Apparently not. That compiler error didn't just occur for no reason. Something has changed. Check line 40 carefully and if it looks ok, check the lines above and below it.

#### PeterDonis

Mentor
what do you mean by explicit listing?
I mean what you posted in post #1 of this thread: your actual code.

at the beginning of the post - I posted all the information of the program
But you didn't say you had a problem at line 40 in post #1 of this thread. You said the program was giving output, just not the output you wanted.

Then in post #15 you said you were getting an error. That tells us that something changed in the program from what you posted in post #1. So we need to see the changed program, and we need to see what commands you typed to compile and run it and what the exact output was.

I went back to the original code I had at the start of the post - because I deleted to much.

So back to where I began. :-(
Then you'll need to start asking questions again in a new thread, because this one is being closed due to lack of information being provided.

If you start a new thread, you need to post exactly what code you are using, exactly what commands you type to compile and run it, and exactly what output you get. Then you need to explain what output you think you should have gotten. You need to go through the same procedure every time you make any change, no matter how small, that you want to ask about. Again, we can't read your mind and we can't see your computer; all we know is what you post here. If you leave information out that we need, it means we can't help you.

#### PeterDonis

Mentor
im really really new to this program and programming in general.
As another note: if you are new to programming in general, something like this might not be the best first programming project for learning how to program. You might want to try some simpler things first to learn the skills of programming.

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving