Skip to main content
1-Visitor
June 21, 2013
Question

Matrix function Issues

  • June 21, 2013
  • 2 replies
  • 12930 views

Hi all,

I have been having some trouble getting a function to work despite each individual part of the equation works and has the same dimesions as the other matricies in the equation. I have attached a file that demonstrates my issue. I want the function Tn to spit out a 4x1 matrix so it can be used in a later function. Please help!

2 replies

25-Diamond I
June 21, 2013

The way you used the range variables and defined your vector singly is a bit irritating.

You have to be aware that Mathcad would begin the index count of vecors and matrices by 0. Thats the reason your 4-element vectors are 5x1 matrices with the first element set to zero. You should consider that fact and correct your indices accordingly. You can set the system variable ORIGIN to 1 to change that behaviour, but usually its a good idea to leave it at the default value 0.

One way to achieve what you want is the following which does not make use of the range variable j, but uses vectorization. Note that setting w[0 to 1 is only necessary because of the problem described above. If you change your vector definition that would be no longer necessary.

matrixfunction.png

25-Diamond I
June 22, 2013

As you aked WHY your way of function definition did not work, here is a short explanation. Basically the key for understanding what happens is the understanding of the difference between vectors and range variables. This was discussed quite a lot here in a multitude of threads and the subject was given a thorough treatise from time to time.

This is what failed in your sheet:

expl1.pngError: "This value has to be an integer"

and you wondered why, as the same expression seemed to work OK if used on its own:

expl2.png

The reason is that, while the above result looks like a vector, it isn't !!! At least not a normal one you could use as any other vector. The range variable acts as sort of an implicit for-loop and what looks like a result vector is just Mathcads way to present the results of this loop. For some reasons they have decided not to create a normal vector in this situation but a construction which is rather useless for further calculations.

You can see that its not a conventional vector as you are not allowed to assign that expression to a variable

expl3.pngError: "This value has to be an integer"

nor are you able to access a single element of that construction

expl4.pngError: "This value has to be a vector"

So one solution is to use Mathcads vectorization operator, which does the looping through the vector elements and creates a new vector as I showed in the last reply. Another way is to use Mathcads programming ability

expl5.png

You could even use j instead of k, but I thought that something like "j element-of j" would be too confusing. The left j would be a loacal variable and the rightmost j would be your worksheet range variable.

What I said in the last post still applies - you should cleanup your worksheet, especially concerning the indexing of matrices and vectors. Most of the time your worksheet is written as if you are not aware that Mathcad starts numbering at zero by default. Only in one definition of the Matrix Cd you have used an index 0 yourself. Not sure if that was done on purpose.

One last example as a conclusion:

expl6.png

1-Visitor
June 24, 2013

Wow this is really helpful so far. So the end goal of my problem is to calculate each of these values at a given time and then increment to the next time (for instance increase time by .1sec). I want to be able to do these calculations many times for a specific time increment. In the case of my attached example from the first post, I need to calculate w_dot, wi, Tn, and then there would also be a Pn after that. Then I would want to increase the time increment by .1sec. Then I would want to recalculate all of those again, where Pn and Tn and wi end up as starter data for the next time increment. How would i set that up generally? THe end goal is to have a Pn at each time increment for each V 1..4. Also in my attached file, H represents the time increment.

25-Diamond I
June 24, 2013

Not quite sure about the end goal.

maybe you cleanup your worksheet (indices) and add the first steps of the iteration you have in mind manually to show what results you are after.

Best idea is you write a function which takes as parameters all values which change over the iteration and returns a vector consisting of all values, vectors or scalars you wish to calculate. Then calling that function in a for-loop should do the job.