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

 채택된 답변

Roger Stafford
Roger Stafford 2014년 6월 7일

2 개 추천

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;

댓글 수: 6

Salar
Salar 2014년 6월 7일
Thanks, but it's a class project, and I have to do it this way.
Roger Stafford
Roger Stafford 2014년 6월 7일
편집: Roger Stafford 2014년 6월 7일
As a matter of fact I tried your particular set of equations and found that it does indeed diverge away from a solution very rapidly for each random initial value of x_0_2 of the many I tried to use. I'm afraid your class project is faced with the prospect of disappointing results for many sets of equations they may try to solve using the blind technique you describe. Some may converge but many will not.
Salar
Salar 2014년 6월 7일
편집: Salar 2014년 6월 7일
Thanks for the prompt response, well, I didn't use the exact numbers from my project, they are : a=[2,-1,0;-1,2,-1;0,-1,2] with b=[1;2;3] and initial guess of x_0_2 = 3. can you please try these and see if it works?
Roger Stafford
Roger Stafford 2014년 6월 7일
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.
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
Roger Stafford 2014년 6월 7일
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개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 6월 7일

댓글:

2014년 6월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by