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

Newbie

## Matrix function Issues

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!

26 REPLIES 26

## Re: Matrix function Issues

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.

## Re: Matrix function Issues

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:

Error: "This value has to be an integer"

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

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

Error: "This value has to be an integer"

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

Error: "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

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:

## Re: Matrix function Issues

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.

## Re: Matrix function Issues

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.

## Re: Matrix function Issues

I tried to clean up my sheet a little bit and provided some explaination for what I am trying to accomplish in some text in this attached file. I'm just having a little trouble picturing how the iterative process works in mathcad as I am a relatively new user to MathCAD. Sorry that there is a lot of detail in there but I'm just not entirely sure how to go about doing this. Thanks you all of your help already too.

Highlighted

## Re: Matrix function Issues

I don't see an iteration formula in your sheet; don't see, how time increment H is used or what the initial values (vectors??) Pi and Pj would be.

But what about Pi and Pj. I could image that at the start we have Pi=Pj=P, but we would need a function which would return the TWO vectors Pi and Pj for the next iteration step. Pn() would only return ONE vector.

You write that Pi is always the higher pressure (compared tp Pj). So why do you ask in your functions ro(), tp(), etc. if Pj<Pi??

You write that Pi is the higher pressure of the two - which two? One compartment - two pressures??

## Re: Matrix function Issues

Pi and Pj are within the vector P. I want my program to compare different entries in the vector P. So according to my example, there are areas between compartments 1,2 2,4 3,4 1,3 and 0,2 (0 represents atmospheric conditions). Pi and Pj need to be values from the vector P, or in the case of future iterations it will eventually be the values inside of Pn. rp and tp are functions put in place because the pressure ratio much always be less than one, that is why those functions are in there. Pi and Pj are not set numbers, they are taken from P. So at time =0 I want my (Pi,Pj) to be (P_2,P_0), (P_2,P_1) etc. The only case that will matter for time =0 is for compartments 2,0 because that is the only area between compartments that will have a pressure ratio equal to 1. Thus making the mass flow equal to 0 and not changing other values. But for the second time increment the pressure in compartment 2 will be less become it has lost air to ambient. So for this time increment any compartment connected to 2 will have a mass flow, and so on throughout calculations. There is no iteration formula in my worksheet because I do not know how to set one up and that is the main thing I need help on actually. Sorry for the confusion. I also used Pi and Pj because I am not sure how to pull out information from a vector and compare it with another entry from that vector. So Pi and Pj are just arbitrary names. Pi and Pj represent the pressures in two of the compartments, and they will be compared by the ratio function. I think I told you wrong that Pi and Pj are always the higher and lower pressure because this is not always true. Sorry about that. However, the pressure ratio must alwas be less than one.

## Re: Matrix function Issues

So as far as I understand it, you will need first to set up a routine which will calculate the 4 pressure values after one timestep. Input for this routine should only be the vector P of the current pressure values and the vector T with the current temperature values and the routine would use the vector V with the volume of the compartment, the matrix Ae and whatever else would be necessary to do the calculations. The output of this routine would be new vectors P and T.

I don't see that Tn() and Pn() would do the job as of its two unknown scalar parameters Pi, Pj.

I'm not versed with the technical background so I can't help with that part.

## Re: Matrix function Issues

do you think you could maybe set up a really simple, general way to solve some equations through iterations of a constant time increment? That may be enough to help me through this.

Announcements