Solving set of equations from loop

1 view (last 30 days)
RS
RS on 24 Oct 2013
Commented: Vishal Agrawal on 9 Mar 2015
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 Comment
Vishal Agrawal
Vishal Agrawal on 9 Mar 2015
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Oct 2013
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 Comments
RS
RS on 25 Oct 2013
Edited: RS on 25 Oct 2013
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 on 25 Oct 2013
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.

Sign in to comment.

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!