Hola como estan tengo esta funcion cuadratica.
puedo insertar datos para a,b,c. como enteros 1,2,3, o 1.2 3.5 5.4 etc..
y esto da 2 resultados para la funcion cuadratica.
Ahora necesito ingresar al programa numeros imaginarios para a,b,c y que esto me de los 2 resultados para x.
Como puedo hacer que el progrmama me reciba numeros imaginarios o complejos.
gracias.
funcion cuadratica:
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
/* Este programa permite el calculo de las raices
de una ecuacion cuadratica */
/* Declaración de variables*/
double a, b, c, x_1, x_2;
double x_1r, x_1i, x_2r, x_2i;
/* Entrada de datos */
cout<< " coeficientes a, b y c de la ecuacion cuadratica \n";
cin>>a >> b >> c;
/* Procesamiento de datos y Salida de Resultados */
if ( (pow(b,2.0) - 4.0 * a * c) > 0)
{
x_1 = ( -b + sqrt( pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
x_2 = ( -b - sqrt( pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
cout<<"\n";
cout<<"La primera raiz es "<< x_1 << "\n";
cout<<"La segunda raiz es "<< x_2 << "\n";
cout<<"\n";
}
else
{
x_1r = -b / (2.0 * a);
x_1i = sqrt( fabs(pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
x_2r = -b / (2.0 * a);
x_2i = -sqrt( fabs(pow(b,2.0) - 4.0 * a * c) ) / (2.0 * a);
cout<<"\n";
cout<<"La primera raiz es "<< x_1r <<" + " <<x_1i << " i \n";
cout<<"La segunda raiz es "<< x_2r <<" " <<x_2i << " i \n";
cout<<"\n";
}
Hello how are you I have this quadratic function.
I can insert data for a, b, c. as integers 1,2,3, or 1.2 3.5 4.5 etc. ..
and this gives 2 results for the quadratic function.
Now to enter the the program need imaginary numbers for a,b ,c and this gives me the 2 results for x.
As I can make me get progrmama imaginary or complex numbers.
thanks.
It's kind of hard to understand.. Maybe you could translate better than google? Anyway, my guess is that you should thake a look at std::complex http://www.cplusplus.com/reference/std/complex/
Ahora necesito ingresar al programa numeros imaginarios para a,b,c y que esto me de los 2 resultados para x.
Como puedo hacer que el progrmama me reciba numeros imaginarios o complejos.
Translation: He wants to know how to enter complex coefficients for the polynomial at run time.
My suggestion is that you enter the real and imaginary parts separately. Otherwise, you'd have to do text parsing, which is always a bother.
@Mathhead200 Fortunately, std::complex contains just such an overloaded version of sqrt().
However, writing such a function isn't too hard either.
Let a complex # z be represented in polar notation as z = r*ei*a where r = |z| and a is the phase angle in the complex plane. We get sqrt(z) = sqrt(r)*ei*a/2 or
sqrt(z) = sqrt(r)*( cos(a/2) + i*sin(a/2) )
Here's a little program to demonstrate (which is relevant to OP's problem because it shows the use of methods he needs).
#include <iostream>
#include <cmath>
#include <complex>
usingnamespace std;
int main()
{
double PI = 3.14159265;
double x, y, r, a;// for our representation of a complex #
cout << "Zr = "; cin >> x;// user enters real
cout << "Zi = "; cin >> y;// and imaginary parts for a complex #
// using the complex # library to find sqrt(z)
complex<double> z(x,y);// construct z from user entered values
cout << "z = " << z << endl;// echo input
cout << "Using complex sqrt(z) = " << sqrt(z) << endl;
// now by our own method
r = sqrt(x*x+y*y);// r = |z|
// find a = phase angle in complex plane
if( x != 0.0 )// z has a real pert
{
a = atan(y/x);// returns a = -PI/2 to +PI/2 ( for z in 1st or 4th quadrant)
if(x < 0.0)
a += PI;// for z in 2nd or 3rd quadrant
}
elseif(y > 0.0 )// z is purely imaginary
a = 0.5*PI;// z = i*r
else
a = 1.5*PI;// z = -i*r
cout << "Own method gives sqrt(z) = (" << sqrt(r)*cos(a/2.0) << "," << sqrt(r)*sin(a/2.0) << ")" << endl;
return 0;
}
I thought you might like this, considering your username.
You know I worked with complex numbers expressed in polar (r cis θ) form a while back in a high school trig course, but I haven't really needed them since.
I need imaginary numbers add to the variables a, b, c
This program solves a quadratic equation, only asks that I insert the data for the variables a, b, c. these variables can be integers like 2, 3, 56, 13, etc. .. 56.4 or 2.3 etc ... Now I need to know how to insert a, b, c imaginary numbers: 34i, 2i, 56i or negative roots.
Use the complex<> class to store your values, and ask for the values individually. You can parse it easily enough if you want to allow your user to enter something like "3.5 - 2.9i", but you can also just ask for both real and imaginary parts for each variable.
(Remember, that "3.5" is the same as "3.5 + 0i", and "-2.9i" is the same as "0 - 2.9i".)