cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Jacobi method - show all the partial solution of the sys. iteration by iteration

tubar
14-Alexandrite

Jacobi method - show all the partial solution of the sys. iteration by iteration

Jacobi method - show all the partial solution of the sys. iteration by iteration

 

Hello,

I want to out put all the partial solution of the sys. iteration by iteration in a table (or m x n matrix):

 

itr     x1    x2    x3    x4

0      ...    ...    ....    ....

1    ....    ....    ....    ....

- - - - - - - - - - - - - - - - -

itr    ....    ....    ....    ....

 

 

Also i don't know why in my file does not accept itr->0   instead accept begining from itr->1.  I attached the file.

 

 

17 REPLIES 17
LucMeekes
23-Emerald III
(To:tubar)

You've set ORIGIN:=1, this means that indexing in arrays and matrices starts at 1, not 0.

 

Success!
Luc

Werner_E
25-Diamond I
(To:tubar)


@tubar wrote:

 

 

Also i don't know why in my file does not accept itr->0   instead accept begining from itr->1.  I attached the file.

 

 


Because you have set ORIGIN to be 1 at the top of your sheet and you use x[itr in your program. itr=0 would mean an invalid array index if ORIGIN=1 and thats exactly what the error messages says.

 

tubar
14-Alexandrite
(To:Werner_E)

That one related to the ORIGIN is the minor problem. I take it as it is. Instead, the biggest problem that I still not solved is related to the OUTPUT : iteration vs partial solution of the system. I cannot make the function/file to group all iteration solutions in a table or a matrix.  The file is attached in the previous messaje.

 

tubar_1-1591897050192.png

 

LucMeekes
23-Emerald III
(To:tubar)

It's not clear what you expect as OUTPUT. Please post a picture of what you would like the OUTPUT to be for the illustrated case.

 

Success!
Luc

tubar
14-Alexandrite
(To:LucMeekes)

The solution, just for the given example from the file should appear like this (in table or matrix format):

 

tubar_0-1591899031169.png

 

I cannot obtain this entire table of partial solutions, I can obtain displayed only the final solutions (1 , -3, 4, 0.5) from the last iteration 15 when it is matched the convergence. For me it is not enough, and I am frustrated to be locked only in a problem of displaying the solution despite the algorithm from there solved the problem.

 

 

tubar
14-Alexandrite
(To:tubar)

Finally I obtained the OUTPUT in the way represented in the attached file.  If anybody have an additional idea to compact more the algorithm, I am waiting with interest.    I attached the file.

 

Thank you very much for the support.  You are cool.

 

tubar_0-1591901886920.png

 

LucMeekes
23-Emerald III
(To:tubar)

I see you found it.

You have to note: The result of a function is the result of the last statement executed in the function. Generally it is better (programming practice) to use the explicit statement involving 'return' for that. (Note that you cannot type 'return', you have to take it from the programming palet.)

But 'return' can also be used in the middle of a program. basically it causes the program to stop and present the result of its argument.

 

Here's a very simple example:

LucMeekes_2-1591904794002.png

Now see what happens if I take away the last statement:

LucMeekes_3-1591904849443.png

If you end a program with a loop (a for loop in this case) the last statement of the last iteration through the loop is returned as the result. Hope this helps with understanding Mathcad progams.

 

Success!
Luc

tubar
14-Alexandrite
(To:LucMeekes)

Still exist a problem: while the partial solutions are extracted correctly for displaying, instead the partial errors are extracted bad.  I don't know why.

 

Please, see on the attached file.

 

tubar_0-1591906639205.png

 

LucMeekes
23-Emerald III
(To:tubar)

Check your mathcad implementation of the jacobi() function against the example (just above your 'correct' picture:

1. The conditions ifor the WHILE loop ar NOT exactly the same.

2. The first statement inside the WHILE loop is different.

3. The last statement of the first FOR loop contains an absolute value. You've swapped the arguments to it. (That should not cause different results)

...

 

Success

Luc

tubar
14-Alexandrite
(To:LucMeekes)

I found. There was problem in link with how xold read the values of x from the previous cycle of while.  So I added a new FOR instruction for reading.

 

 

Thank you very much. Now the problem solved exhaustively. I attached the corrected file to be used by others interested.

 

tubar_0-1591911719279.png

 

tubar
14-Alexandrite
(To:tubar)

Something still remains strange in the way Mathcad Works:

 

1) If I remove from the script ORIGIN:=1  and instead I set from Menu the ORIGIN=1 the algorithm works setting itr-->0.  Otherwise, it imposes to set itr--->1.  It should not be the same thing?  Why the manual setting of ORIGIN affects the program running.

 

2) it is a way to escape from the intermediary variable e -->err_i     so that the matrix of results to read directly the results from err ???

 

The problem is solved by now, but I want to add some perfection and elegance to the concept of solving.

LucMeekes
23-Emerald III
(To:tubar)

If you want 'more elegance' and more general usability, you should make your program work independently from the actual value of ORIGIN. For that you have to use the variable ORIGIN in your program, but at the right place.

Here's an example, simple, but it's for demonstration purposes:

LucMeekes_0-1591983302352.png

The functions vectorsum() and sumvector() both attempt to sum alle elements of the vector supplied in the argument. The answer, 1+2+3=6, is easy.

Since ORIGIN=0 ate the start, vectorsum() fails, because it incorrectly assumes the ORIGIN=1.

Just setting ORIGIN to 1 after the definition of the function doesn't help. The function must be redefined before vectorsum() runs with no error.

As you can see sumvector() is well programmed, it works irrespective of the actual value of ORIGIN.

Note that length() give the length of a vector, in itself that number is '1-based' (starts at 1) so it generally works with an ORIGIN setting of 1.

In general, using ORIGIN and last() cause the least problems when indexing vectors. For matrices the functions rows() and cols() behave similar to length() of a vector. They give the number of rows and columns. So with matrices you'd have to define lastrow() and lastcol() yourself, or work with ORIGIN+rows()-1, and ORIGIN+cols()-1 respectively. Note that while ORIGIN is usually set to either 0 or 1, it can be set to (almost) any number. You could set it to -10, or 100...

 

Success!
Luc

tubar
14-Alexandrite
(To:LucMeekes)

I  appreciate the description of the instruction ORIGIN.  However, setting it in the ways:

 

1) Tools ------> Worksheet options ------> Buil-in variables ------> Array origin (ORIGIN)  = 1

 

2)   Just writing ORIGIN:=1 in the code that overlap ORIGIN=0 by default from Menu.

 

the behavior of the processed algorithm is different when I set particularly for this code itr--->1.    This is the main contradiction that I can get through of it but I cannot explain it.

Werner_E
25-Diamond I
(To:tubar)

I get the very same error when itr is set to 0 in the program, regardless if ORIGIN is set to 1 via the menu or explicitly with ORIGIN:=1.

 

Maybe you forgot to delete an ORIGIN:=0 statement in your sheet? This would overrule the ORIGIN setting in the menu beginning from the position in the sheet where it is positioned.

tubar
14-Alexandrite
(To:Werner_E)

Yes, indeed, in the last file where the algorithm is well refined does not make the difference the choose ORIGIN related to itr.   I think there were a conjunction of problems in the first file that I shared in the first message, a file that now is superseded.

 

Thanks.

tubar
14-Alexandrite
(To:tubar)

From intermediary variable e_i if I would escape, it will be cool. I want just to express   ee_itr,m ---> err_m      instead to create intermediary variable e_i<----err   then ee_itr,m ---> e_m .    It is somehow too indirect the actual path with intermediary variable.

tubar
14-Alexandrite
(To:tubar)

I got it !!!    Yeeeep.    I had to write ee_itr,m ---> err ,  so err without to be err_m in order to be displayed the solution.    The intermediary and non-necessary variable e or e_i is now removed.

 

Thank you very much for the Socratic disposition of debate. It conducted to the truth.

 

Now, my file is perfect with minimal instructions. I attached it.

Announcements

Top Tags