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

Click For Summary

Discussion Overview

The discussion revolves around a Fortran program designed to calculate and print the roots of a quadratic equation, specifically addressing issues with correctly displaying imaginary numbers when the discriminant is negative. Participants explore coding techniques, mathematical expressions, and potential corrections to improve the program's functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in printing imaginary numbers correctly, noting that the output always shows a zero imaginary part.
  • Another participant questions whether the issue is with the values being incorrect or with the display format.
  • Some participants suggest rewriting the code to use only real numbers and represent complex roots in the form a + ib.
  • Concerns are raised about the grouping and number of parentheses in the calculations for Croot1, Croot2, Root1, and Root2, which may lead to incorrect results.
  • A specific example of a quadratic equation (x² + x + 1 = 0) is provided to illustrate the expected complex roots and highlight the discrepancies in the participant's code.
  • One participant acknowledges the need to handle cases where the discriminant is zero, indicating that only one real root exists.
  • Another participant suggests simplifying the calculation of the imaginary part by directly using the negative discriminant in the square root calculation.
  • There is a mention of a future programming task involving DNA markers, indicating ongoing challenges in coding.

Areas of Agreement / Disagreement

Participants express varying opinions on the best approach to handle complex numbers in the program. While some agree on the need for corrections and improvements, there is no clear consensus on the optimal solution or method to implement.

Contextual Notes

Participants note limitations in the current code, such as the unused discriminant variable and potential errors in mathematical expressions. There are also suggestions for improving code readability and efficiency.

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 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K