Fortran How Can I Correctly Print Imaginary Numbers in My Fortran Quadratic Program?

AI Thread Summary
The discussion centers around troubleshooting a program that calculates the roots of a quadratic equation, specifically focusing on the correct handling and display of complex numbers. The original code encounters issues where complex roots are incorrectly calculated and displayed as real numbers, leading to an imaginary part of zero. Participants emphasize the importance of correctly grouping parentheses in calculations and suggest using real numbers to express complex roots in the form of a + bi. They also recommend checking the discriminant to determine the nature of the roots and suggest adding a condition for when the discriminant equals zero. The revised code provided improves clarity and correctness, with suggestions to simplify calculations and enhance readability. The user expresses gratitude for the assistance and hints at a challenging upcoming programming task involving DNA markers.
mike81
Messages
6
Reaction score
0
I am trying to write this program, it runs fine but I can't get the imaginary numbers to print correctly. I have been trying this for a few hours now and can't figure it out. Here is the code, I am running Force 2.0. Thanks in advance for the help.
Program Quad
Implicit None
Real Root1, Root2, a, b, c, discri
Complex Croot1, Croot2
2 Write (*,*) 'Enter 3 numbers'
Read (*,*) a,b,c
discri=(b**2-(4*a*c))
If (discri.lt.0) Then
Write (*,*) 'Roots are Complex'
Croot1=(-b+SQRT(abs(b**2-4*a*c))/2*a)
Croot2=(-b-SQRT(abs(b**2-4*a*c))/2*a)
Write (*,*) 'Root 1 is',Croot1
Write (*,*) 'Root 2 is',Croot2
Else
Write (*,*) 'Roots are Real'
Root1=(-b+SQRT(abs(b**2-4*a*c))/2*a)
Root2=(-b-SQRT(abs(b**2-4*a*c))/2*a)
Write (*,*) 'Root 1 is',Root1
Write (*,*) 'Root 2 is',Root2
End If
Goto 2
End
 
Technology news on Phys.org
What do you mean when you say
I can't get the imaginary numbers to print correctly

Do you mean the values are incorrect or you can't get them to display correctly.

Also, check your grouping (and number) of parenthesis in your computation of Croot1, Croot2 and Root1, Root2 - your current expressions will not compute correct results.
 
You can, and probably should, write the code for this problem using only REALs, giving the roots when the discriminant is negative in the form a + ib. Here a and b are real numbers.

Also, you store a value in discri and never use it, so why is it there?
 
Here's a simple example you can try your code on - x2 + x + 1 = 0. Since the discriminant = -3, the roots are complex. Using the quadratic formula, you can find that the roots are x1 = (-1 + i*sqrt(3))/2, and x2 = (-1 - i*sqrt(3))/2

Your code will give you approximate values for (-1 + sqrt(3))/2 and (-1 - sqrt(3))/2. These are real numbers!
 
When I say they don't print out correctly I mean that it always writes, for complex (somenumber, 0). No matter what the second number is always zero. I see what your saying Mark, I could have used discri but initially just used it to have the program use either real or complex numbers. I am short on time tonight, I am going to play around with it some more tomorrow night. I really appreciate the help.
 
mike81 said:
When I say they don't print out correctly I mean that it always writes, for complex (somenumber, 0). No matter what the second number is always zero.
Which is what I'm saying. You're thinking that you're getting complex numbers, but you are really getting real numbers, so the imaginary part is zero.
mike81 said:
I see what your saying Mark, I could have used discri but initially just used it to have the program use either real or complex numbers. I am short on time tonight, I am going to play around with it some more tomorrow night. I really appreciate the help.

Once you calculate the discriminant, you know whether the roots are real or complex. BTW, you should also have a separate else if group for when the discriminant is zero, since you will get only one root (repeated) then.

Let's take another look at the example I gave earlier, x2 + x + 1 = 0. Here, the discriminant is -3, so the roots are complex, and would be given by
x = \frac{-1 \pm \sqrt{-3}}{2}
This can also be written as
x = \frac{-1}{2} \pm i \cdot \frac{\sqrt{-(-3)}}{2}

One final thing. Take a look again at what TheoMcCloskey said at the end of his post.
 
TheoMcCloskey said:
Also, check your grouping (and number) of parenthesis in your computation of Croot1, Croot2 and Root1, Root2 - your current expressions will not compute correct results.

In particular, the statement for Root1 actually calculates the following quantity:

-b + \left( \frac {\sqrt{|b^2-4ac|}} {2} \right) a
 
I think I got it, thanks a lot for the help.

Program Quad
Implicit None
Real Root1,Root2,a,b,c,discrim,r1,ri,root
2 Write (*,*) 'Enter 3 numbers'
Read (*,*) a,b,c
discrim=b**2-(4*a*c)
If (discrim.lt.0) Then
discrim=-discrim
Write (*,*) 'Roots are Complex'
r1=-b/(2*a)
ri=(Sqrt(discrim))/(2*a)
Write (*,*) 'Root 1 is (',r1,'+',ri,'i)'
Write (*,*) 'Root 2 is (',r1,'-',ri,'i)'
Else if (discrim.eq.0) Then
Write (*,*) 'There is one real root'
Root=-b/(2*a)
Write (*,*) 'Root is ', root
Else
Write (*,*) 'Roots are real'
Root1=(-b+(SQRT(discrim)/(2*a)))
Root2=(-b-(SQRT(discrim)/(2*a)))
Write (*,*) 'Root 1 is', Root1
Write (*,*) 'Root 2 is', Root2
End If
Goto 2
End
 
Calculating the discriminant as a separate variable definitely makes this easier to read. :smile:
 
  • #10
A couple of minor points.
1) Instead of b**2, you can use b*b
2) In the case where discrim < 0, you can omit this line:
discrim = - discrim

and change this line:
ri=(Sqrt(discrim))/(2*a)

to this:
ri=(Sqrt(-discrim))/(2*a)

Otherwise, it looks fine!
 
  • #11
Made the changes you suggested except I left b**2 as is since he taught it that way in class. I really appreciate the help. I am sure I will posting our last program, its going to be a nightmare. He wrote a file with 1.4 million letters that are dna markers (4 letters in groups of 3). We have to write a program to show the frequency of the possible combination's. I am planning to get started on it this week sometime, I have looked forward to writing the others, this one...not so much.
 

Similar threads

Replies
12
Views
1K
Replies
4
Views
2K
Replies
19
Views
2K
Replies
13
Views
2K
Replies
4
Views
2K
Replies
22
Views
5K
Replies
11
Views
2K
Back
Top