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

SOLVED
Highlighted

## Recursive funcion with arrays

Hello,

I have a problem implementing a recursive funcion in Mathcad Prime 3.1, hope someone can help me make it work. I want to calculate a heat transfer layer per layer. So the array should work like this: T_layer,time

Here's what I want it to do: Take given Temperature T_0,0 and given Temperature T_1,0 and calculate Q.dot_1,1. With Q.dot_1,1 and a given time increment (t) calculate Q_1,1. With Q_1,1 and the mass (which is dependent on the layer) I want to calculate T_1,1. With it and T_0,1 (given) calculate Q.dot_1,2 and so on Meanwhile it should also calculate the other layers. So T_1,0 and T_2,0 (both given) to Q.dot_2,1 with t to Q_2,1 with the mass to T_2,1. Now T_2,1 and T_1,1 (from the first layer calculation) to Q.dot_2,2 and with t to Q_2,2 and so on. I hope I could explain it well enough.

EDIT: Thanks to FredKohlhepp I zipped and uploaded the worksheet

Tags (4)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

## Re: Recursive funcion with arrays

I got it working! This is what it looks like: Thanks again for the help!

13 REPLIES 13
Highlighted

## Re: Recursive funcion with arrays

Attaching a MC file; right-mouse-click your file and "send to" "compressed folder (zip)" should zip it , which then can be attached to your post..

Attached is an example of muti-layered walls. Unfortunately, in P4, so I attach a pdf.

I hope this helps.

Norm

Highlighted

## Re: Recursive funcion with arrays

There's an old technique, initializing the first element of vectors, then stepping thru calculating each successive step in each vector.  I've attached a not-very-good example.

In this wonderful new web application many of us have to zip a file to attach it.  This problem was highlighted when the application first went into service, and PTC promised to have it fixed.  Like so many of their promises . . .

Highlighted

## Re: Recursive funcion with arrays

Thanks for the answer. This way of calculating could proof itself helpful.

Though when I try this I get an error I don't really understand saying the array index istn't valid. And that the index can't be smaller than ORIGIN neither bigger than the last element. But I don't see why it should be any of those. I attached the file trying this way of calculation.

Highlighted

## Re: Recursive funcion with arrays

You'rre running 3.1, I'm running 3.0--I can't open your file, you can't save back.

Attach a PDF and thank PTC for their marketing stragety!

Highlighted

## Re: Recursive funcion with arrays

Here's the picture, Fred. I guess the intent is to implement the recursion with just an assignment of a 2 element vector to a 2 element vector.

I'm afraid it doesn't work like that. Could be nice if it did though...

Success!
Luc

Highlighted

## Re: Recursive funcion with arrays

Okay I have it working now thanks to LucMeekes. But it still doesn't give me the expected results. Here's the actual version of the loop. I also attached the manual calculation as pdf. Thanks alot for helping me everybody!

Highlighted

## Re: Recursive funcion with arrays

So it's not recursion, but iteration you're doing (building a new result upon previously calculated data).

I think the problem you have is that, while defining a range in Mathcad results in (implicit) iteration, you are using two ranges, and you cannot control how they are executed. Which means that the matrix is filled with data in a different way than you think it is. Your matrix data is now being calculated with some numbers that are 0 where you think they are calculated by previous steps. You show the Q results, what are the Theta results?

I think you should find a way to have a single range (possibly 0 to 10x10-1=99) and an algorithm that determines which matrix element is being calculated at each of those 100 steps. Then you can make sure that previous data is correct before you use it in the next step.

Success!
Luc

Highlighted

## Re: Recursive funcion with arrays

Oh yeah, sorry I confused the terms. I think you're right about the calculation not using the previous data.

I'm not really sure how to implement what you're suggesting but it's something to work with.

And here the missing theta results from the same calculation as above: Highlighted

## Re: Recursive funcion with arrays

You have not initialized theta. Note that by default array indices in Mathcad start at 0, not 1 (that is what ORIGIN is).

If you initialize theta you have a unit mismatch problem.

Edit: You did initialize it, but you are attempting to get theta(1,0), which does not exist. And you still have a unit mismatch in the second expression.

Highlighted

## Re: Recursive funcion with arrays

Corrected the units (added W to the definition of Q[1,1

And I preset the final indices of the two result arrays Q and theta, so that the matrices can be filled.

This is what I get: It's now free of mathcad errors, not necessarily free of mathematical or physical (modelling) errors.

I Leave that up to the OP.

Success!
Luc

Highlighted

## Re: Recursive funcion with arrays

Looking at the first step of the calculation where z=1 and s=1, your definition of Q[1,1 and Theta[1,1 both require Theta[1,0.  However, Theta[1,0 is undefined.

Can you provide the result that you expect in your example?

Highlighted

## Re: Recursive funcion with arrays

Yes, thats true, thanks for the clue.

Now I took the error free version of LucMeekes (Thanks alot!) and fixed some technical conditions.

I already calculated the complete loop manually which gives you the expected result. I attached the manual calculation and the new array calculation as pdf and Mathcad Prime 3.1 worksheet.

As you can see there the first Q._1,1 is calculated correct, but it doesn't calculate a new Temperature and I don't know why not.

Also I'm confused why I need to define Q._s_E/s_R,9 but if I don't some error is happening.

Highlighted

## Re: Recursive funcion with arrays

I got it working! This is what it looks like: Thanks again for the help!