-2

Question: i'd like to write a little c program that finds parameter of a parametric quadratic equation like:

(k+2)x²+(k-2)x+k-2=0

If i want to find k for x1=-x2, how can i do? Is there a way to convert inserted string to mathematical operations(if i insert k-2, can the program resolve it, and even with a fraction?).

EDIT: my program would ask for the 'a' factor(k+2), then for the 'b'(k-2) and 'c'(k-2) ones. My problem is to read the operations in input and even combine them in fractions.

Saverio
  • 1
  • 1
  • 1
    You'd have to write a program to do that from a to z. C is an imperative language, not a declarative one. – StoryTeller - Unslander Monica Mar 06 '17 at 14:16
  • You have a [quadratic equation](https://en.wikipedia.org/wiki/Quadratic_equation). Its algebraic solutions are well known. I personally recommend using the [discriminant](https://en.wikipedia.org/wiki/Quadratic_equation#Discriminant) as the basis of your solution. Since C99 and later have [complex number support](http://man7.org/linux/man-pages/man7/complex.7.html), your program can easily report either real or complex root(s) of the equation, whichever the equation happens to have. – Nominal Animal Mar 06 '17 at 16:35

1 Answers1

2

By the Viete equations, -(x1+x2) is the linear coefficient divided by the leading coefficient.

As the sum of roots here is zero, this translates to k=2. But then also both roots are zero, as the equation reduces to 4x²=0.

Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
  • That equation was random, i just want a way to read operations in input and resolve them – Saverio Mar 06 '17 at 14:32
  • Have you thought of using an existing CAS? Or at least a symbolic calculation library? You will need to program in a lot of intelligence to interpret the problem as stated. You will need something like (towers of) arithmetic extensions and their simplifications, the most used tool is Gröbner bases,... Check out, for example, Pari/GP and Cohen's book on its principles in Computational Algebra. – Lutz Lehmann Mar 06 '17 at 15:35
  • Thanks, i'll go check. Maybe it's too early for me – Saverio Mar 07 '17 at 16:19