Hello

I want to resolve a lineare systeme n*m in the C language but i don't know which metod to use or which method is the best (easiest) (newton/gauss etc..).
I want to write a programme in C which solve this problem but idk how to start.

Thanks for helping me.

Dani AI

Generated

Short answer: choose the algorithm by the shape and size of your system. For a square n×n system start with Gaussian elimination (with partial pivoting) or LU. For overdetermined systems (more equations than unknowns) use QR or SVD (least-squares). For large sparse systems use iterative solvers (Conjugate Gradient for symmetric positive‑definite, GMRES/BI-CGSTAB for general). Follow ’s advice to break the task into steps (parse input → pick method → implement solver → test), and note ’s pointer that Gaussian elimination is the obvious starting point for square matrices — but add pivoting and checks for numerical issues.

Practical C roadmap and gotchas: store matrices in a single row‑major 1D array for performance, use double (not float), allocate dynamically, and always perform partial pivoting (swap rows) to avoid division by tiny pivots. Check a small epsilon (e.g. 1e-12) to detect singular or nearly singular matrices and return an error code. For overdetermined systems avoid solving normal equations directly (A^T A) — that squares the condition number; prefer QR or call a library (LAPACK/GSL) for production.

Minimal Gaussian elimination (augmented matrix n x (n+1)) with partial pivoting — solution ends up in the rightmost column:

#include <math.h>
#include <stdlib.h>

int gauss_solve(int n, double *aug, double eps) {
    for (int i = 0; i < n; ++i) {
        int piv = i;
        double max = fabs(aug[i*(n+1)+i]);
        for (int r = i+1; r < n; ++r) {
            double v = fabs(aug[r*(n+1)+i]);
            if (v > max) { max = v; piv = r; }
        }
        if (max < eps) return -1;          /* singular or ill-conditioned */
        if (piv != i) {
            for (int c = i; c <= n; ++c) {
                double t = aug[i*(n+1)+c];
                aug[i*(n+1)+c] = aug[piv*(n+1)+c];
                aug[piv*(n+1)+c] = t;
            }
        }
        for (int r = i+1; r < n; ++r) {
            double f = aug[r*(n+1)+i] / aug[i*(n+1)+i];
            for (int c = i; c <= n; ++c) aug[r*(n+1)+c] -= f * aug[i*(n+1)+c];
        }
    }
    for (int i = n-1; i >= 0; --i) {
        double s = aug[i*(n+1)+n];
        for (int j = i+1; j < n; ++j) s -= aug[i*(n+1)+j] * aug[j*(n+1)+n];
        aug[i*(n+1)+n] = s / aug[i*(n+1)+i];
    }
    return 0;
}

Testing and validation: verify with identity and small known systems, compute the residual r = Ax−b and its norm, and try an ill‑conditioned test (Hilbert matrix) to learn limitations. If you want a worked example for a specific n and m (rectangular, sparse, or dense), post those sizes and a sample matrix and the code can be adapted.

Recommended Answers

All 2 Replies

  1. Try google and google scholar about your topic.
  2. Put it into Wolfram Alpha ( https://www.wolframalpha.com/examples/EquationSolving.html ) and see if you get your answer.

  3. There are folk that attempt such but don't write code and ask "How to do it?" Another member here shared a video about this. I'm hoping one day they do another skit about programming.

  4. Solve it and write down all the steps. Now take each step and create code that the computer and you understand. Complete for each step and then you've done it.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.