Quadratic fortran program help

In summary, the conversation discussed an issue with writing a program that involves calculating roots and handling complex numbers. The code provided runs fine, but the imaginary numbers do not print correctly. There was a suggestion to use only REALs for the code and to check the grouping of parenthesis in the computation. It was also pointed out that the value of the discriminant was not being used. An example was given to demonstrate the issue with the current code and a revised version was provided to address the problem. Further help was offered for a future program involving DNA markers.
  • #1
mike81
6
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
  • #2
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.
 
  • #3
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
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
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.
 
  • #6
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
[tex]x = \frac{-1 \pm \sqrt{-3}}{2}[/tex]
This can also be written as
[tex]x = \frac{-1}{2} \pm i \cdot \frac{\sqrt{-(-3)}}{2}[/tex]

One final thing. Take a look again at what TheoMcCloskey said at the end of his post.
 
  • #7
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:

[tex]-b + \left( \frac {\sqrt{|b^2-4ac|}} {2} \right) a[/tex]
 
  • #8
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
 
  • #9
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.
 

What is a Quadratic Fortran Program?

A Quadratic Fortran Program is a computer program written in the Fortran programming language that is designed to solve quadratic equations. These equations have the form ax^2 + bx + c = 0, where a, b, and c are constants and x is the variable.

Why is Fortran used for writing Quadratic Programs?

Fortran is a high-level programming language that is specifically designed for scientific and engineering applications. It has built-in capabilities for handling mathematical operations, making it well-suited for solving quadratic equations.

How does a Quadratic Fortran Program work?

A Quadratic Fortran Program works by taking in the values of the coefficients a, b, and c, and using the quadratic formula to calculate the roots of the equation. It then outputs the solutions to the equation, which are the values of x where the equation equals 0.

What are the advantages of using a Quadratic Fortran Program?

One of the main advantages of using a Quadratic Fortran Program is its speed and efficiency. Fortran is a compiled language, meaning that the code is converted into machine code before execution, resulting in faster execution times compared to interpreted languages. Additionally, Fortran has a long history of use in scientific and engineering fields, making it a reliable and well-tested language for solving mathematical equations.

Are there any limitations to using a Quadratic Fortran Program?

While Fortran is a powerful language for scientific computing, it may not be the best choice for other types of programs. It has a relatively strict syntax and may not have the same level of support and resources as other more modern programming languages. Additionally, it may not be the most user-friendly language for those without a strong background in programming.

Similar threads

  • Programming and Computer Science
Replies
12
Views
931
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
499
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top