MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Salar on 7 Jun 2014

Hello,

I need to construct a code that finds solution to a set of equation by domain decomposition. it should be really easy, but I'm just not really an expert in while/for loops, so I need help with this, and I rely appreciate it. I try to explain the problem as best as I can here: so let's say we have 3 equations with 3 unknowns for example a=[2,3,0;4,5,6;0,2,3] and a*x=b with b=[9;2;5] , so I come up with an initial guess for x_2 so we can call it x_0_2, and then MATLAB should run this through a loop/while to do the followings : from the first equation it should solve for x_1_1 which is equal to (9-3*X_0_2)/2 and then solve for x_1_3 by using the third equation as follows x_1_3 = (5 - 2*X_0_2)/3 and then use those two and solve for new x2 which is x_1_2 = (2- 4*x_1_1 - 6*x_1_3)/5 by the second equation, and then give me a matrix with [x_1_1; x_1_2 ; x_1_3] and then it keeps doing this and it will give me [x_2_1; x_2_2 ; x_2_3] until it reaches a limit and I will define that limit in while loop . I think I know how to define that limit, but I need help with writing this process. Thanks

*No products are associated with this question.*

Answer by Roger Stafford on 7 Jun 2014

Accepted answer

There is no reason to suppose that such a procedure as you describe will converge properly to a solution. It all depends on the element values in 'a' and 'b'. I hate to see you spend so much time on a method that may not work, when matlab makes available to you a very easy method for solving it using the 'backslash' operator:

x = a\b;

Show 3 older comments

Roger Stafford on 7 Jun 2014

Yes, with those revised values in 'a' and 'b' your iteration does converge. Probably your biggest challenge will be figuring out how to exit from a 'while' loop when you have attained the desired accuracy.

Salar on 7 Jun 2014

so, can you help me with the loop? how did you write it? what I was thinking for stopping it was doing something like:

while (rerr>tol && n<itmax)

on the first line, but before doing this, I really need help construction the while loop, I keep messing up the indices I think. I really appreciate it if you could help me with that.

Roger Stafford on 7 Jun 2014

You don't need indices in this problem unless you wish to store the entire history of convergence. All you meed is an "old set" of three elements and a "new set". At the appropriate point in your loop you copy the new set into the old set and then compute an updated new set from that updated old set using your iteration scheme. When the two sets are sufficiently close, you exit. You should be able to do the rest of this coding yourself.

Don't be surprised if it doesn't converge for other values of the matrix 'a' and vector 'b'.

## 0 Comments