PDA

View Full Version : Quadratic fortran program help


mike81
Jul7-10, 09:11 PM
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.
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

TheoMcCloskey
Jul8-10, 11:14 AM
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.

Mark44
Jul8-10, 01:12 PM
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?

Mark44
Jul8-10, 03:32 PM
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!

mike81
Jul8-10, 08:17 PM
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.

Mark44
Jul8-10, 10:47 PM
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.
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.
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.

jtbell
Jul9-10, 11:17 AM
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

mike81
Jul9-10, 09:52 PM
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

jtbell
Jul10-10, 12:36 AM
Calculating the discriminant as a separate variable definitely makes this easier to read. :smile:

Mark44
Jul10-10, 12:47 PM
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!

mike81
Jul12-10, 04:21 PM
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.