Solving set of equations from loop

2 次查看(过去 30 天)
RS
RS 2013-10-24
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