Bek
Bek

Reputation: 261

Solving a linear system of equations in Scilab

I want to solve the following system Ax=b where the dimensions of A are m,n (m>n), b m,1 and x n,1. After the solution under Scilab, I found that some components of my vector x are complex, I find that strange because I was told that the vector x must be real. How can you explain that?

Here is my code :

function [x] = sys_()
    [fid1,err1] = mopen("D:\Documents\sys_surdet\Donnees_test_A.txt","r");
    [fid2,err2] = mopen("D:\Documents\sys_surdet\Donnees_test_B.txt","r");
    A = mfscanf(-1,fid1,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f');
    b = mfscanf(-1,fid2,'%f');
    A=10^(-A/10);
    b=10^(b/10);
    col = size(A);
    j = zeros(col(1,1),1);
    x = zeros(col(1,2),1);
    if rank(A)==col(1,2) then
        x=(A'*A)\(A'*b);
    else
        x=-1;
    end
    mclose(fid1);
    mclose(fid2);
endfunction

Upvotes: 1

Views: 7103

Answers (2)

burningbright
burningbright

Reputation: 152

The lines

A=10^(-A/10);

b=10^(b/10);

appear to be converting data from decibels back to standard units. This is done on the entire vector or matrix at once similarly to Matlab, etc. Did you confirm that the A and b are not complex-valued at this point?

x=(A'*A)\\(A'*b);

This is the standard normal equations for solving systems of equations with more equations than variables. It is (among other things) the least squares solution x. In SCILAB, you can get the same result with just

x=A\b;

This in general will be different from the solution

x=pinv(A)*b;

Your system of equations may be numerically close to singular (check for a singular value close to 0), or may have no solution.

In SCILAB, you can get more information about your system of equations with

cond(A'*A)

or

[x,kerA]=linsolve(A,-b);

See the SCILAB help section on linear algebra for more details.

Upvotes: 3

duffymo
duffymo

Reputation: 308733

I have no idea what these are about:

A=10^(-A/10);
b=10^(b/10);

I'd solve this either by premultiplying both sides by A transpose and using LU decomposition and back substitution to solve for x (linear least squares) or Singular Value Decomposition (SVD).

Upvotes: 1

Related Questions