Home > Enterprise >  Calculate impedance using polar function in C
Calculate impedance using polar function in C

Time:03-12

Impedance is a complex number Z defined as Z = R Xi, where R is the active resistance and X is the reactive resistance, which can also be negative. If we have a parallel connection of n elements whose impedances are Z1, Z2,…, Zn , the total impedance of the parallel connection Z is calculated as Z = 1 / (1 / Z1 1 / Z2 … 1 / Zn). In other words, the situation is similar to the parallel connection of resistors, except that here we work with complex impedances.The program needs all impedances Zk instead of through active resistance and reactive resistance to be set via the apparent resistance Zk and the phase shift fik, where Zk = Zk * e ^ (ifik). formula The program should finally show the apparent resistance and phase shift for the whole parallel connection.

OUTPUT:

Enter number of elements: 2

Z1 = 10.5

fi1 = 30

Z2 = 2.8

fi2 = -47.6

Parallel connection has Z = 2.57147 and fi = -33.7613.

  • Note: You will need polar(), abs() and arg() functions

Code:

#include <iostream>
#include <complex>
int main()
{
    int n,i=0;
    double z,fi;
    std::complex<double>a;
    std::complex<double>sum=std::complex<double>(0,0);
    std::complex<double>one=std::complex<double>(1,1);
    std::cout << "Enter number of elements: ";
    std::cin >> n;
    for(;;)
    {
        i  ;
        std::cout<<"Z"<<i<<" = ";
        std::cin>>z;
        std::cout<<"fi"<<i<<" = ";
        std::cin>>fi;
        a=std::polar(z,fi);
        a=one/a;
        sum =a;
        if(i==n)break;
    }
    sum=one/sum;
    std::cout<<"Parallel connection has R = "<<std::abs(sum)<<" and X = "<<std::arg(sum)<<".";
    return 0;
}

This is my output:

Enter number of elements: 2

Z1 = 10.5

fi1 = 30

Z2 = 2.8

fi2 = -47.6

Parallel connection has R = 3.21929 and X = 2.91565.

My formula is incorrect. Could you help me fix this? I don't know how to convert formula to code.

CodePudding user response:

Simple solution:

#include <iostream>
#include <complex>
int main()
{
    int n,i=0;
    double z,zz,fi;
    const double PI=4*atan(1);
    std::complex<double>Z,sum;
    std::cout << "Enter number of elements: ";
    std::cin >> n;
    std::cout<<std::endl;
    for(;;)
    {
        i  ;
        std::cout<<"Z"<<i<<" = ";
        std::cin>>z;
        std::cout<<"fi"<<i<<" = ";
        std::cin>>fi;
        fi=fi*PI/180;
        Z=std::polar(z,fi);
        sum=sum (1./Z);
        if(i==n)break;
    }
    Z=1./sum;
    std::cout<<"Parallel connection has R = "<<std::abs(Z)<<" and X = "<<std::arg(Z)*180/PI<<".";
    return 0;
}

Output:

Enter number of elements: 2
Z1 = 10.5
fi1 = 30
Z2 = 2.8
fi2 = -47.6
Parallel connection has R = 2.57147 and X = -33.7613.
  • Related