views:

309

answers:

3

I have some trouble on setting of n-linear equations in matlab.I don't know how can I declare in matlab.I need matlab code for setting of n-linear equations..

+2  A: 

You can write n-linear equations as one matrix equation to solve it. Here you can find great example: http://blogs.mathworks.com/pick/2007/09/13/matlab-basics-video-solving-linear-equations/ (video!)

See also these pages:
http://en.wikipedia.org/wiki/System_of_linear_equations
http://en.wikipedia.org/wiki/Matrix_equation

Gacek
SO? What's the problem? Read the wiki... TUrning n-equations into matrix equation is one of the most basic problems of linear algebra. If you can't do so, then your problem is in poor mathematics, not matlab...
Gacek
A: 

You can solve a linear system in various ways, depending on whether there exists a unique solution or not.

A simple way is by reducing it to reduced-echelon form (rref).

Consider the system:

 x + 5y = 4
2x -  y = 1

You can write the coefficient matrix A, and the RHS, B as follows: (' is the transpose operator)

>> A = [1 5; 2 -1]

A =

     1     5
     2    -1

>> B = [4 1]'

B =

     4
     1

You can write it as an augmented matrix (A|B):

>> horzcat(A,B)

ans =

     1     5     4
     2    -1     1

And then find the REF(A|B)

>> rref(ans)

ans =

    1.0000         0    0.8182
         0    1.0000    0.6364

And hence x ~ .8182, y ~ .6364.

Alex
+1  A: 

The absolutely fastest way to solve linear equations in MATLAB is simply to setup your equation on the form

AX = B

and then solve by

X = A\B

You can issue

help mldivide

to find more information on matrix division and what limitations it has.

kigurai
But that will only work if there is a unique solution. (I.e. A is invertible).
Alex
@Alex: Yes, and it will produce a warning saying so. It will perform better than inv(A)*B though. I checked your solution as well, and it suffers from the same problem (while it is longer to type).
kigurai