Solving set of equations from loop

조회 수: 3 (최근 30일)
RS
RS 2013년 10월 24일
댓글: Vishal Agrawal 2015년 3월 9일
I have a set of follwing 3 equations,
for i = 2:4;
x(i+1)- 2.25*x(i) + x(i-1)==0;
end
with folowing two boundary conditions,
x(1) = 0;
x(5) = 5;
The 3 equations comming from the for loop are like this:
y(3) - 2.25y(2) + y(1) = 0; y(4) - 2.25y(3) + y(2) = 0; y(5) - 2.25y(4) + y(3) = 0;
OR
y(3) - 2.25y(2) = - y(1) ; y(4) - 2.25y(3) + y(2) = 0 - 2.25y(4) + y(3) = -y(5) ;
OR
y(3) - 2.25y(2) = 0; y(4) - 2.25y(3) + y(2) = 0; - 2.25y(4) + y(3) = -5;
Now there are there equations and three unknowns.The solution is x(2)=0.7256; x(3)=1.6327;x(4)=2.9479.
Since the no. of equations (and unknows) are only 3, it is solve by commandslike 'equationsToMatrix' wherein we manually write down the three equaions with three uknowns as x, y and z.
But when the number of unknowns are as high as 20, it is impossible to use that command or any command that requires us to write down all the equations.
My questions is: Is there any command or method by which we can dierectly solve the above equations from the for loop.
OR
Any command to extract the matrix A (3x3) and vector b = [0,0,-5] directly from the for loop without the need to writing downn the equations, to get the solution vector x.
  댓글 수: 1
Vishal Agrawal
Vishal Agrawal 2015년 3월 9일
Use Gauss Elimination method to find out the inverse of matrix, it takes least time to find the inverse of any non-singular matrix. Generalize code can be obtain from the internet.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2013년 10월 25일
The above does not solve equations, it only rewrites them. If you want an alternative way of rewriting them instead of using equationsToMatrix, you could use MuPAD's coeff() and lhs() and rhs().
Once you get them in matrix form, the backslash operator, \, can be used to do the solving.
  댓글 수: 2
RS
RS 2013년 10월 25일
편집: RS 2013년 10월 25일
Hi WR,
Yes aboave command 'equationstomatrix' does not solve the equaitons, but it gives u co-efficients of the equations in the form of matrix, which can be easily solved to get solution.
I am not looking for re-writing the equations, i am looking for command/method to extract the equations from the for loop in the form of matrices (A and b) so that i can directly us the command 'x =inv(A)\b' to get the solution vector x.
Thanks
Walter Roberson
Walter Roberson 2013년 10월 25일
If you are using inv(A) then you are probably doing something wrong.
It looks to me like you should be creating a diagonal matrix using diag(), or perhaps a tri-diagonal matrix.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by