# Quadratic fortran program help

1. Jul 7, 2010

### mike81

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 cant figure it out. Here is the code, I am running Force 2.0. Thanks in advance for the help.
Implicit None
Real Root1, Root2, a, b, c, discri
Complex Croot1, Croot2
2 Write (*,*) 'Enter 3 numbers'
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

2. Jul 8, 2010

### TheoMcCloskey

What do you mean when you say
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.

3. Jul 8, 2010

### Staff: Mentor

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?

4. Jul 8, 2010

### Staff: Mentor

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!

5. Jul 8, 2010

### mike81

When I say they dont 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.

6. Jul 8, 2010

### Staff: Mentor

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

7. Jul 9, 2010

### Staff: Mentor

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

$$-b + \left( \frac {\sqrt{|b^2-4ac|}} {2} \right) a$$

8. Jul 9, 2010

### mike81

I think I got it, thanks a lot for the help.

Implicit None
Real Root1,Root2,a,b,c,discrim,r1,ri,root
2 Write (*,*) 'Enter 3 numbers'
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

9. Jul 10, 2010

### Staff: Mentor

Calculating the discriminant as a separate variable definitely makes this easier to read.

10. Jul 10, 2010

### Staff: Mentor

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. Jul 12, 2010

### mike81

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.