Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve written a code in C++. It needs to solve a system of equations. What am I

ID: 3766337 • Letter: I

Question

I've written a code in C++. It needs to solve a system of equations. What am I doing wrong? I know that whenever a matrix leads to a solution that has 0=1 there are no solutions and when 0=0 there are infinitely many solutions. How am I suposed to implemenet that into my code? Test cases that should have no errors work fine, but when there is a special case like 0=1 or 0=0 there is a problem.

#include "stdio.h"
#include "math.h"
#include <iostream>
using namespace std;

void Solve3x3_Jacobi(double A[3][3], double b[3], double x[3]);
void Solve3x3_GaussSeidel(double A[3][3], double b[3], double x[3]);

int main() {
    double A1[3][3] = {{ 3.0, 2.0, 1.0},{4.0,6.0,5.0},{7.0,8.0,9.0}};
double b1[3]={6.0, 15.0, 24.0}, xJ[3], xGS[3];
    double R1, R2, R3, R4, R5, V1, V2, i1, i2, i3;

    while(true){
                  cout<<"Enter value of Resistor 1: ";
                  cin>> R1;
                  cout<<"Enter value of Resistor 2: ";
                  cin>> R2;
                  cout<<"Enter value of Resistor 3: ";
                  cin>> R3;
                  cout<<"Enter value of Resistor 4: ";
                  cin>> R4;  
                  cout<<"Enter value of Resistor 5: ";
                  cin>> R5;                                                
    printf("Case 1: A1 = [ %6.2lf %6.2lf %6.2lf ", A1[0][0]=R1+R2, A1[0][1]=-R2, A1[0][2]=0);
    printf("        %6.2lf %6.2lf %6.2lf ", A1[1][0]=-R2, A1[1][1]=R2+R3+R4, A1[1][2]=-R4);
    printf("        %6.2lf %6.2lf %6.2lf ", A1[2][0]=0, A1[2][1]=-R4, A1[2][2]=R4+R5);
                  cout<<"Enter value of Voltage source 1: ";
                  cin>> V1;
                  cout<<"Enter value of Voltage source 2: ";
                  cin>> V2;                 
printf("          b1 = [ %6.2lf ", b1[0]=V1);
    printf("                 %6.2lf ", b1[1]=0);
printf("                 %6.2lf] ", b1[2]=-V2);
Solve3x3_GaussSeidel(A1,b1,xGS);
printf("         xGS = [ %6.2lf ", xGS[0]);
printf("                 %6.2lf ", xGS[1]);
printf("                 %6.2lf ] ", xGS[2]);
//tell what results mean aka what the mesh currents are
printf("         i1= %6.2lf ", xGS[0]);
printf("         i2= %6.2lf ", xGS[1]);
printf("         i3= %6.2lf ", xGS[2]);
system("pause");//*added for the purpose of making the output window stay open
    return 0;
    }
}
void Solve3x3_GaussSeidel(double A[3][3], double b[3], double x[3])
{
const double TOL = 1.0e-8;
const int MAX_ITER = 10000;

int iter=0;
double error = TOL+1.0, xPrev[3]={0.0,0.0,0.0};
while (error>TOL && iter++ < MAX_ITER) {
  error = 0.0;
  for (int i=0; i<3; i++) {
   double dx = 0.0;
   for (int j=0; j<3; j++) {
    if (i!=j) {
     dx += A[i][j]*xPrev[j];
    }
   }
   x[i] = (b[i]-dx)/A[i][i];
   double rowError = fabs(x[i]-xPrev[i]);
   if (rowError>error) {
    error = rowError;
   }
   xPrev[i]=x[i];
  }
  for (int i=0; i<3; i++) {
   xPrev[i]=x[i];
  }
  printf("%6.2lf %6.2lf %6.2lf %6.2e ", x[0], x[1], x[2], error);
}
}
//cases that should have no errors work fine, need to figure out how to make the errors known

Explanation / Answer

#include <iostream>

#include <limits>

int main()

{

std::cout<<"This program is designed to test two linear equations. ";

std::cout<<"In order to best solve the system, ";

std::cout<<"equations will be in the form of a*x + b*y = c. ";

std::cout<<"and d*x + e*y =f. ";

std::cout<<"Please enter an integer for a, b, and c. ";

double a, b, c, d, e, f;

while ((std::cout << "Enter a.")

     && !(std::cin >> a))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

}

while ((std::cout << "Enter b.")

          && !(std::cin >> b))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

}

while ((std::cout << "Enter c.")

          && !(std::cin >> c))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

}

std::cout<<"Equation 1 is "<<a<<"x +"<<b<<"y ="<<c;

std::cout<<"Please enter an integer for d, e, and f. ";

while ((std::cout << "Enter d.")

         && !(std::cin >> d))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

}

while ((std::cout << "Enter e.")

          && !(std::cin >> e))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

}

while ((std::cout << "Enter f.")

          && !(std::cin >> f))

{

    std::cout << "That's not a number ";

    std::cin.clear();

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' ');

    }

    std::cout<<"Equation 2 is "<<d<<"x +"<<e<<"y ="<<f;

    double slope1, slope2;

    double x1, x2, y1, y2;

    slope1 = (b / a);

    slope2 = (e / d);

    std::cout<<" Slope of Equation 1 is "<<slope1<<" ";

    std::cout<<" Slope of Equation 2 is "<<slope2<<" ";

    x1 = ((c*e)-(b*f))/ ((a*e)-(b*d));

    y1 = ((a*f)-(c*d))/ ((a*e)-(b*d));

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote