1

I want to write in Matlab a function that appreciates the fixed point iteration for a system of equations. The idea is:

$\begin{bmatrix} x{_{1}}^{m+1}\\ x{_{2}}^{m+1} \end{bmatrix}= \begin{bmatrix}y{_{1}}^{n}+hf{_{1}}(x{_{1}},x{_{2}})\\ y{_{2}}^{n}+hf{_{2}}(x{_{1}},x{_{2}}) \end{bmatrix}$

The iterations stop when $ |x_{1}^{m+1} -x_{1}^{m}|<TOL$ and $ |x_{2}^{m+1} -x_{2}^{m}|<TOL$ or when m arrives the limit of iterations M...

At the function, do I have to write : if ($|x_{1}^{m+1} -x_{1}^{m}|<TOL$ && $|x_{2}^{m+1} -x_{2}^{m}|<TOL $){ $y_{1}=x_{1}^{m+1}$; $y_{2}=x_{2}^{m+1}$; } ? But when only one of them is smaller than TOL how can it get its value ??

Mary Star
  • 13,956
  • If only one of them is smaller than TOL, the iteration goes on. – Shuhao Cao Jun 02 '13 at 21:57
  • So is my "if" loop right, or should it be OR instead of AND ??? – Mary Star Jun 02 '13 at 22:00
  • Based on your description, definitely you should use &&. – Shuhao Cao Jun 02 '13 at 22:36
  • Could you take a look at my "for" loop?? Is this right?? [code]for m=1:M $x_{1}$(m+1)=y1n+h$f_{1}$($tn+h,x_{1}(m),x-{2}(m)$); $x_{2}$(m+1)=y2n+h$f_{2}$($tn+h,x_{1}(m),x_{2}(m)$); if (abs($x_{1}(m+1)-x_{1}(m))<TOL$ && abs($x_{2}(m+1)-x_{2}(m))<TOL$) $y_{1}=x_{1}(m+1)$; $y_{2}=x_{2}(m+1)$; return end x1(m)=x1(m+1); x2(m)=x2(m+1); end $y_{1}=x1(M)$; $y_{2}=x2(M)$;[/code] – Mary Star Jun 02 '13 at 22:41
  • Looks ok, but if you literally write y_{1} and put it in MATLAB, I believe MATLAB will give you a syntax error. – Shuhao Cao Jun 02 '13 at 22:45
  • I want to use this function for the Backward Euler Method for 2x2 systems... For example: $\begin{bmatrix} y{{1}(t)}\ y{{2}(t)} \end{bmatrix}'= \begin{bmatrix}-y{{2}(t)}\ y{{1}}(t) \end{bmatrix}$, $y_{1}(0)=0$, $y_{2}(0)=1$... I wrote it by hand, and the results are not the same with of the Matlab code...

    At the Backward Euler Code, I wrote: for n=1:N [Y1,Y2]=stage(y1(n),y2(n),h,t(n)); t(n+1)=t(n)+h; y1(n+1)=y1(n)+hS(t(n+1),Y1,Y2); y2(n+1)=y2(n)+hG(t(n+1),Y1,Y2); end

    (stage() is the function of the fixed point iteration) What I'm doing wrong??

    – Mary Star Jun 03 '13 at 01:07
  • Do you have to write the code by hand? for there is something like ode45...you know...Actual coding is never ever never ever done by hand first, pseudo-code and structure maybe, but you have to test your code line by line by choosing appropriate testing cases. – Shuhao Cao Jun 03 '13 at 01:10
  • I have to implement the Backward Euler Method for the system above using the fixed point iteration... Now I checked the results with ode45, and they are wrong... I think that I did a mistake either at the function that appreciates the fixed point or at the BackwardEuler function at the line where I'mcalling the function stage... But I do not know what exactly my mistake is... :/ – Mary Star Jun 03 '13 at 01:23

0 Answers0