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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

recursivic Calculation

sm�ller-2
12-Amethyst

recursivic Calculation

Hey guys,

i would need a recursivic calculation with a break condition. I made it so far but now i would need an extention to this calculation.

I added a mathcad file in which i described my problem.

I hope that someone can help me 😃

This is only an example but is describing my problem very good

Best thx!

ACCEPTED SOLUTION

Accepted Solutions
-MFra-
21-Topaz II
(To:sm�ller-2)

Hello Stefan, do you like my solution below?

for smoll 2.jpg

or like this:

for smoll 2.jpg

View solution in original post

30 REPLIES 30
LucMeekes
23-Emerald III
(To:sm�ller-2)

It's not completely clear what you want to accomplish. Here some first comments:

Luc

LucMeekes
23-Emerald III
(To:sm�ller-2)

Is this what you are looking for?

Luc

Werner_E
25-Diamond I
(To:sm�ller-2)

>> This is only an example but is describing my problem very good

Really? Not in my eyes.

First, all I see is a somewhat strange iteration, no recursion!

You write, that you don't want to write the formula with a and b - what do you mean by that?

In your example, you first define a as 1 x 1 vector, then, by a <- a[i you make it a scalar and immideatly after that you use it with an index as if it still would be a vector. THis throws the error.

If you replace every a by a[i your prgram works, but a[i never gets a new value, all a[i equal to 1 and so b is always calculated as 0,75 and as 0,25/1 is never smaller than 0,1%, you never reach the break command.

So its unclear to me what you are trying to do.

Also absolutely unclear is, why you expect a 3 x 3 matrix as result. Your routine just returns a 2 x 1 vector.

R

-MFra-
21-Topaz II
(To:sm�ller-2)

Hello Stefan, do you like my solution below?

for smoll 2.jpg

or like this:

for smoll 2.jpg

Thx for the fast help so far!

I think i am confused with the the meaning of recursivic.

I think i only need a normal iteration.

I try to explain a second time for better understanding.

I have a Input Vector.

Example: Force [100N;200N;300N]

For Every Force i need a separat Iteration with well defined formulas. Here just for simple: 1/2*(1+Force/2) and a breaking condition

The Resulting matrix should then be the INput force (here 100N;200N;300N); the last iteration step before breaking condition and the Result of the Iteration.

In the upper example instead of Force (100;200;300); the vector a is given (1,2,3)

So it is a normal iteration calculation, applied to every row of the input Vector until breaking condition

--------------------------------------------

One last question: Do i need a vectorising operation when im doing a complicated calculation for b1?

------------------------------------------------

Thx all for help

@FM thx for your file

For me its important to get the resulting value of the last iterationstep from every row of the input vector. (As to say the maximum needed iterations are different, depending on the input vector.

Here: a0=1 a1=2 a3=3

------------------------------------------------------

So far i have a problem to realise the calculation 😞

Maybe somone can give me a tip.

Regards!

@LucMeekes

yes i think that is what i need. I added my Mathcad file but there is a small problem so far. I didnt get the failure.

The excel file shows the result of every loadstep. I think it is what i need.

Hmm lazyness yes maybe

But it has certain reasons: I have very long formulas with many  indizes. It is very confusing when i have more indizes than i need

When im finished with my project im gonna post it here so you can see what i need hehe

Werner_E
25-Diamond I
(To:sm�ller-2)

So maybe the following is what you are looking for.

In most cases its better to write the function to work with scalars and then call that function vectorized with a vector as argument.

If you insist on a pure 3 x 3 result matrix, you can turn "Tabelle" in that kind of matrix the following way:

WE

i always get an error message in my file ;-(

Best thx

Werner_E
25-Diamond I
(To:sm�ller-2)

There are two errors.

The return value in the last line is a 1 x 3 matrix

Somehow you managed to make Mathcad think, the the absolute value should be a matrix determinant - thats the reason for the error message which tells you that Mathcad had expected a matrix of scalars.

You can change the meaning of those vertical bars via right click menu in "absolute Value":

Same error BTW in the first routine, too.

Attached the working sheet.

WE

Werner_E
25-Diamond I
(To:Werner_E)

An additional remark. I think Richard once asked (in another thread) if a solve block wouldn't do the job better than an iteration.

This is an example of what he meant:

You won't get the number of iterations necessary and you can't control the precision the way you do it in a self-made routine, but you sure get high precision correct results.

Here is Herons method of calculating the square root of a number implemented in a similar way (here the parameter matters and influences the result)

WE

Werner_E
25-Diamond I
(To:Werner_E)

Just for fun here is a true recursive variant of your sample iteration

Best thx for the answer!

I get my failure and it works!

Your Calculation is very comfortable because i can write the input Vector after the Iteration programmcode

Werner_E
25-Diamond I
(To:sm�ller-2)

Stefan Müller wrote:

Your Calculation is very comfortable because i can write the input Vector after the Iteration programmcode

Thats one of the benefit of creating self-contained functions which are only using their input arguments and no global worksheet variable. You can give those functions meaningful, self-explanatory names, hide them in a collapsed region and make your report nice, tight and easy readable and you are able to compare the results of different input values side by side.

Hope you are able to implement something similar for your actual problem you are working on.

WE

yes for sure thats what i was looking fore.

I need to use it in my work. But the Legal Law i should use has hundreds of sites to convert to mathcad so it is going to take longer

Also i was looking for an automatic DOE optimisation.

Input: 2 variables.

Force 1

Force 2 (starting value for force 3)

Optimisation goal: Force 3 should be maximum on the basis of Force 1 and 2 (Force 1,2,3 leads to certain OK; NOK validations which defines the Range of maxiter)

Force 3 has to be iterative calculated out of Force 1 and 2

But yea step by step now im gonna implement your iterative calculation.

Im gonna try to add the possibility to add 2 input parameters and do a curve discussion for Force 3 (break if Force 3 = maximum)

-MFra-
21-Topaz II
(To:sm�ller-2)

for smoll 2.jpg

sm�ller-2
12-Amethyst
(To:-MFra-)

that really destroys my brain. Hmm i dont understand that code clearly hmm.

I think the maxiter needs to be a function of the input parameter.

Where is "a" going in the code?

Thx for the help!

LucMeekes
23-Emerald III
(To:sm�ller-2)

To repair your brain:

Note that Test(X,maxiter)  only defines the function.

Then it is actually called with

Test(a,20)

which produces the output. There 'a' replaces the formal parameter 'X' of the function and '20' replaces the formal parameter 'maxiter'.

In other words, Test is called with X=a and maxiter=20.

The definition of X at the top of the sheet does nothing (unless e.g. you add another evaluation below the definition of Test like: Test(X,20) which would give as result a vector with 2 elements, first a vector of three values and the other a single value).

Luc

OK Thx 😃

Thx for the Posts to help me startening of the Iterative Calculation.

Up to now i have problems to realise it 😞

How can i make the break condition with "OK" "NOK"

Hmm would be great if you guys can help me out

Ok i think i found the failures of by myself.

But still i have the problem of that i dont know how i can say to mathcad that i want the previous iterationstep as result.

Because the previous step the condition dm1 <= am1  should be true

Sorry for double postings

Werner_E
25-Diamond I
(To:sm�ller-2)

This question is already marked answered and its already quite cluttered, so it may make more sense to open a new thread for a new question.

It looks to me that you can much better achieve what you are trying to do using a solve block!

Werner_E
25-Diamond I
(To:sm�ller-2)

I guess there is an easy answer to your question.

The only value which is changed in your routine is F by a constant of 1kN in every step.

So simply return F.new-1kN to get the value you want.

You can always re-calculate d.m1 with this Force to verify the condition d.m1 <= a.m

WE

Werner_E
25-Diamond I
(To:sm�ller-2)

OK, as I don't like the way you use your vectors directly in your calculation routine - here is a solution using a solve block.

Its always better to set up all calculation (no matter if iterations, programs, solve blocks, ..) to work for single scalar arguments and and only later call them vectorized with your vector arguments to get the results for a bunch of values with one call.

As the equation a.m=d.m1 can easily be solve symbolically, I also attached a function for direct calculation.

WE

Thx for your help....

I solved it that way because i need to write much more complicated input....

here are 2 examples i finished so far.

But i still i have a failure on here 😞

I think the problem is the condition and the Number "1" in the routine

Maybe you can help me out another time

Thx

Still trying to understand your post.

The problem is that i have more than 20 input variables. This is only the beginning of the iteration (see my postet files)

I think i should have testet the program while writing regularly

Werner_E
25-Diamond I
(To:sm�ller-2)

> The problem is that i have more than 20 input variables. This is only the beginning of the iteration (see my postet files)

I see, but nevertheless I guess its better to create functions depending on 20 arguments and call them later vectorized than to deal with the vectors from the very beginning.

This has nothing to do with the question if a solve block could do the job better or not.

Working with named functions is one of the benefits of Mathcad once you have accustomed yourself to them.

The problem in your file is here:

And isn't a minus sign missing here in front or something else? Your condition as its written means asking if A.m1[s equals 1/3 a[2

You can see i have a massive amount of break conditions on which i need to stop the routine calculation and give out the resulting Force, which i need to know.

Yea your right im gonna open up a new post.

Learning so far how to achive what i need to do.

Not sure how i can write that massive amount of formulas best and define my breaking conditions.

Can you see the Loadcase Definition at the very beginning =)))

Could manage it and it works very good.

I want to connect that loadcase calculation with that iterative calculation and write out the resulting Force to excel.

So simply return F.new-1kN to get the value you want. How can i do this?


In most cases i now what i want to do but i dont know how to write it in mathcad, so i always try to look for a workaround for that problems

Announcements

Top Tags