Skip to main content
15-Moonstone
April 16, 2024
Solved

Indexing for Advance in Time

  • April 16, 2024
  • 1 reply
  • 1478 views

I have a program that I have written for a fixed time step and then advanced it for two, but obviously I want to do this for many. I thought I could just do this with a for loop but I have various matrix operations in the program and cannot get the indexing right. Do I need to convert everything to indicies? There must be a simpler, more elegant and efficient way.

 

The program is highlighted and annotated at the bottom of the worksheet. There are some auxiliary programs at the top. I am using MathCad 14. (BTW I do have a reason for setting the ORIGIN to -N, but I do not think this is relevant).

 

Thanks in advance for any help.

Best answer by Werner_E

A quick hack would be to use my "WedgeNew" function in a loop and collect the results column-wise in a matrix:

Werner_E_0-1713361387086.png

But its more efficient to rewrite the WedgeNew function to do the job right away:

Werner_E_1-1713361433959.png

 

So when you demand for 20 steps, the results of "disp" and "pres" would be a 11 x 20 matrix each.

Werner_E_2-1713361598269.png

 

Mathcad 15 sheet attached

 

 

EDIT: I just noted that you sure can delete this loop

Werner_E_3-1713362551942.png

in the various "Wedge" functions as is has no effect. Y is never used later and Y would be identical to X anyway.

 

While I am at it - you can replace the nested loops

Werner_E_4-1713363280884.png

used to create matrix K by using the matrix command

Werner_E_5-1713363298821.png

But when I now compare the two, I guess the second isn't much easier or shorter and the nested loops may show clearer which calculation is applied, 😉

1 reply

25-Diamond I
April 16, 2024

It looks to me that you are only interested in the results of the last iteration step, right?

So it would not be necessary to store all the intermediate results in a matrix.

Here is what I came up with - hope it helps.

Werner_E_0-1713290624397.png

Werner_E_1-1713290887078.png

 

Not sure if the animation makes much sense

Ani1.gif

Worksheet and avi attached

 

15-Moonstone
April 17, 2024

Hi Werner,

 

Thanks. The animation does make perfect sense! Although it was not clear from the worksheet I posted I do want to store the values from all the steps. When I get all done, I want to plot certain features of the calculation as a function of time. I wanted to store, e.g. the values of u in a matrix with each column being the results at an individual time step. This what I could not figure out how to index.

 

John

Werner_E25-Diamond IAnswer
25-Diamond I
April 17, 2024

A quick hack would be to use my "WedgeNew" function in a loop and collect the results column-wise in a matrix:

Werner_E_0-1713361387086.png

But its more efficient to rewrite the WedgeNew function to do the job right away:

Werner_E_1-1713361433959.png

 

So when you demand for 20 steps, the results of "disp" and "pres" would be a 11 x 20 matrix each.

Werner_E_2-1713361598269.png

 

Mathcad 15 sheet attached

 

 

EDIT: I just noted that you sure can delete this loop

Werner_E_3-1713362551942.png

in the various "Wedge" functions as is has no effect. Y is never used later and Y would be identical to X anyway.

 

While I am at it - you can replace the nested loops

Werner_E_4-1713363280884.png

used to create matrix K by using the matrix command

Werner_E_5-1713363298821.png

But when I now compare the two, I guess the second isn't much easier or shorter and the nested loops may show clearer which calculation is applied, 😉