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

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Matrix function Issues

MattLiberty
1-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

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

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

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.

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.

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.

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.

I guess you start with T0:=T, V0:=V and does not change.

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??

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.

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.

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.

I'll try to do so.

Can you provide the following:

1) A way to calculate the next Temperature vector T, which may be dependent on the current T, the current vector P, the timestep Delta-t (or H, as you called it) and of course will use Ae, etc.

2) A way to calculate the next Pressure vector P, which may be dependent on the current T, the current vector P, the timestep Delta-t (or H, as you called it) and of course will use Ae, etc.

The next question is, what you are after with your iteration. Are you interested in the last pressure and temperarure values only or do you need the whole history (eg. to graph the changing pressure in selected compartments).

In the first case the iteration will have to return just two 4x1 vectors with the last P and T values.

In the latter case the output would be more complex. If you make an iteration with 1000 steps the return would be two vectors which consists of 1000 4x1 vectors each. See this thread for an example: http://communities.ptc.com/message/207529#207529. Here the questioner had the problem that he needed 2 millions of iteration steps and Mathcad would run out of memory. My proposal for solution was not to save and return every iteration step but only every 1000th which cured the memory issue.

In case you are interested and not able to open files in MC15 format, just ask and I provide a pdf-printout of the worksheet in question.

Hopefully I gave you enough information to help. I did what I could in regards to making the functions only functions of P and T instead of Ti and Pi and Pj. I wrote in some annotations that should prove helpful in understanding exactly what I am trying to do. The main goal of this process is to be able to see a history of the pressures and temperatures in each compartment and be able to graph these with respect to time. Let me know if you need anything else.

Unfortunately the functions Tn() and Pn() you provided do not work!!

If you get them to run the iteration and sample graph I provided in the attachment should do the job.

Addendum: Your function fail as because of the vectoriziation the functions you call (wi, Tn) are called with individual elements of the vectors P and T, not with the whole vector. Your function rp() expects a vector and so it throws the error. If you omit the vectorization, you get a unit mismatch error. I'm not sure why you use only index 0 and 2 in your rp(), f() and AA(). Probably because in the first iteration step the compartments 0 and 2 are the only ones which are connected and have different pressure values. But this is true only in the first iteration step. You have to provide a calculation method which works for every step equally well. You will also have to consider more combinations than the ones you mention as you should be able to change Cd and the calculation should still work.

I will help with the iteration and graphing, but you have to set up the calculations you want to be done.

could you maybe write a brief explaination of what is going on inside the program that you wrote?

Matthew Tomaw wrote:

could you maybe write a brief explaination of what is going on inside the program that you wrote?

First, as you wrote that the main goal is the graphing, I decided that program should return two (N+1)x5 matrices, one for temperature, one for pressure (N is the number of iteration steps). Each column of these matrices would hold the values for one of the compartments.

The rows of these matrices are the transposed vectors P resp. T.

First the matrices (local variables Pv and Tv) are initialized with the start vectors which are parameters P0, T0 of the function. Then I call your functions Pn and Tn to calculate the next vectors and store the results in variables P1 and T1. These vectors are now added as rows to the matrices Pv and Tv (via transpose and stack) and then P0 and T0 are overwritten with P1 and T1 and the whole process starts all over.

After the loop finishes the two matrices which hold all the calculated vales (row by row) are packed in a 2x1 vector and returned.

If you need much more than 2000 iteration steps it would be a good idea to store only n-th value as in the thread i mentioned in the post above.

Transposing and especially stack have proven to be very, very slow (again see the thread mentioned). So if speed is an issue the construction of the return matrices will have to be rewritten. but first we need working functions Pn and Tn to calcluate the next vectors.

Thank you that makes a lot of sense. My main issue with the functions Pn and Tn is the sub-function rp(P) and f(P). This is the part of the series of equations that needs to select a specific set of pressures from P and a specific temperature from T. I have attached the same mathcad file as before but with green annotations that explain in more detail what I am looking for with respect to my equations.

Think that your rp() and f() are the minor problems. Calculation of w.dot seems to need a huge rework as you would have to cycle through all combination of different compartments to calculate that vector.

See if the attached makes sense. Guess you will have to rework on Pn because setting V[0 to 1 ft^3 to avoid the divison of zero may not be correct - you have to know what to do.

It doesn't seem correct that the pressure in all four compartments is rising linear.

I modified the sheet to keep atmosphere pressure and temperature constant. Also I made the timestep (H) a parameter of the functions and included the option of storing not every calculated value to be able to analyze a larger time range without getting in conflict with Prime's silly 50K point limit.

But it seems to me that the pressure and temp graphs are not correct at all. Temperature is decreasing too low, I think, and there is a strange step in both graphs after about 29 second. I'm not sure but the latter may be due to the constant flow rate in your formula if the pressure ratio is below 0.52828.

So you will have to check your formulas (or what I made out of them)

pressure.pngtemperature.png

Yes I agree that the w_dot equation is a little faulty and I also think it may be finding incorrect signs for each individual w_dot before the addition takes place inside each compartment. So I think that is also messing up some of the values so I'll keep looking into that. Can you show me how you got the these previous graps. I'm not sure how you would make the time step H apart of the funcitons other than how I had it originally?

Oops. sorry! I forgot to attach the file in my previous post.

Here it is.

what is nr in the program?

In the program "make P & T" "n" is the number of iterations and "nr" means that only every nr-th calculated value is added to the return vectors.

When I call that function I use the worksheet variable N (120000) for iteration number and worksheet n (300) gets nr in the program. I thought that 400 points are enough, so I set n to 300 (=120000/400).

When I think about it - it would make more sense if the last parameter nr would mean the number of points (at least approx.) which are returned. To make this work you could add "nr <-- floor(n/nr)" somewhere in the front before the for-loop.

Is there a way to display w_dot(P,T) at any of the given time steps? I'd like to see what is going on in the calculation for w_dot during each time increment. There should be a 5x5 matrix called w_dot during each time increment and I would like to display certain ones.

First it seems that you have a superfluous line in your program because w is not used in you calculations at all.

no+effect.png

As for the wdot values. You should be able to calculate and display them after the routine has run and you would not have to change anything in the program. The programm returns all Pressure and temperature values, so you could select the step of interest (a specific column in the resulting matrices) and feed them in wdot().

I can't try as the sheet does not work without the extern excel table, but something like the following should work:

wdot.png

What you posted comes up with an error. I'll attach the excel file so you can work with it.

You are right, the P- and T-values for each step are the rows, not the columns as I wrote. So we have to use the row selector and then transpose to get a columnvector.

wdot2.png

You could graph the values of wdot if that would help

wdot3.png

Top Tags