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..
-
2Don't duplicate your comments, and add a homework tag if appropriate. – Alex Budovski Nov 26 '09 at 12:04
4 Answers
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

- 10,184
- 9
- 54
- 87
-
I have n-equations and n-unknowns .What I must do is that define these equations so that when the input is entered,the output will be given. This is the sample input&output of our programme.This will help to recognise our problem. A1X1 + A2X2 .... + AnXn = B1 . . . AnXn ............... = Bn that we have n-unknowns and n equations and I must define this equations in matlab. – Şeref Acet Nov 26 '09 at 12:01
-
2SO? 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 Nov 26 '09 at 13:17
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.

- 17,947
- 6
- 53
- 58
-
I have n-equations and n-unknowns .What I must do is that define these equations so that when the input is entered,the output will be given. This is the sample input&output of our programme.This will help to recognise our problem. A1X1 + A2X2 .... + AnXn = B1 . . . AnXn ............... = Bn that we have n-unknowns and n equations and I must define this equations in matlab. – Şeref Acet Nov 26 '09 at 11:59
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.

- 21,229
- 9
- 65
- 75
-
But that will only work if there is a unique solution. (I.e. A is invertible). – Alex Budovski Dec 01 '09 at 23:30
-
1@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). – Hannes Ovrén Dec 02 '09 at 08:58
A Code for iterative method Guase Seidel tol is error tolerance x0 is first guess for solution
function seidel(A,b,x0,tol,itmax)
%Solve the system Ax=b using the Gauss-Seidel iteration method.
clc
% =======================================================
% Programmer : A. Ziaee mehr
%
help seidel
n=length(b);
x=zeros(n,1);
%fprintf('\n')
disp('The augumented matrix is = ')
Augm=[A b]
Y=zeros(n,1);
Y=x0;
for k=1:itmax +1
for ii=1:n
S=0;
for jj=1:ii-1
S=S+A(ii,jj)*x(jj);
end
for jj=ii+1:n
S=S+A(ii,jj)*x0(jj);
end
if (A(ii,ii)==0)
break
end
x(ii)=(-S+b(ii))/A(ii,ii);
end
err=abs(norm(x-x0));
rerr=err/(norm(x)+eps);
x0=x;
Y=[Y x];
if(rerr<tol)
break;
end
end
% Print the results
if (A(ii,ii)==0)
disp('division by zero')
elseif (k==itmax+1)
disp('No convergence')
else
%fprintf('\n')
disp('The solution vector are : ')
fprintf('\n')
disp('iter 0 1 2 3 4 ... ');
fprintf('\n')
for ii=1:n
fprintf('%1.0f= ',ii);
fprintf('%10.6f ',Y(ii,1:k+1));
fprintf('\n')
end
fprintf('\n')
disp(['The method converges after ',num2str(k),' iterations to'])
x
end

- 453
- 4
- 8
- 19
-
itmax is maximum number of iteration that prevent programm to go to a unfinishrd loop. – Abolfazl Feb 04 '11 at 05:23