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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Translate the entire conversation x

Issue with a function in a solve block

SM_6465178
10-Marble

Issue with a function in a solve block

Hi, 
I'm struggling with a function within a solve block that requires two inputs, a force and an angle as shown below

SM_6465178_0-1755496645678.png

 

So when I evaluate R3(F1a,_delta_) I get the error that it must be a 3 element vector. I'm pretty sure my mistake is simple, but I just can not work it out. 

 

Any help will be greatly appreciated

 

Shane

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:SM_6465178)

The reason for the error you see is because in your second equation you are using the just the function names P32 and P43 where you should use a 3x1 vector.

My guess is that you just forgot the function arguments.

Werner_E_0-1755512831351.png

M3 in the "find" function was wrongly labelled "System" but its label automatically changes to "Variable" as soon as you add the two function arguments.

 

But maybe Terry is right when he guessed that you meant P320 and P430 instead of P32 and P43? You have to decide...

 

In the solve block which follows it looks that you are missing the angle argument completely. Maybe this is a correct fix?

Werner_E_0-1755513550334.png

Haven't looked further at the next failing solve block but I guess there are similar errors.

View solution in original post

14 REPLIES 14

Hi,

Got all the solve blocks to work but cannot guarantee accuracy.

Cheers

Terry

Werner_E
25-Diamond I
(To:SM_6465178)

The reason for the error you see is because in your second equation you are using the just the function names P32 and P43 where you should use a 3x1 vector.

My guess is that you just forgot the function arguments.

Werner_E_0-1755512831351.png

M3 in the "find" function was wrongly labelled "System" but its label automatically changes to "Variable" as soon as you add the two function arguments.

 

But maybe Terry is right when he guessed that you meant P320 and P430 instead of P32 and P43? You have to decide...

 

In the solve block which follows it looks that you are missing the angle argument completely. Maybe this is a correct fix?

Werner_E_0-1755513550334.png

Haven't looked further at the next failing solve block but I guess there are similar errors.

Hi Werner and Terry,

 

Thanks for the help. Yes I had forgotten the function arguments in there. I thought I had covered them all but obviously not.

 

Thanks Again

Shane

Hi All,

 

That seemed to work, but it really killed the performance. Once I had finished, the sheet was un-usable as the calculation timing was just far too long. Any suggestions on how to accelerate it?

Werner_E
25-Diamond I
(To:SM_6465178)

Can you post the final fixed worksheet and indicate the calculation which takes up that much time?

 

Initially I thought I'd plot the force in the actuator, but it just chugged for ages with no result. I think my time is taken with variable in M6e in the sheet, but I am not really sure

 

Shane

Hi Shane,

The first thing to try if a multi-core processor is involved, is to turn on multithreading.  It is off by default.

 

Capture.JPG

Hi Shane

Second thing to try is better guesses for the solve blocks.  Is there any way to get better start values for the unknowns?

Cheers

Terry

Hi Terry, 

 

I had a look at the multithreading, but it produced no notable improvement. I guess I can use my manual version of the sheet to get an average of the guesses and use that as the input and see what happens.

 

Thanks for the suggestions

Shane

Werner_E
25-Diamond I
(To:SM_6465178)

Your solve blocks use functions which are defined via prior solveblocks. Each call to a solve block function costs time and I guess its done in every iteration step of the solve block.

So it may speed up things if you pre-calculate those values so they are calculated only once.

For example the second solve block calls function F5 (based on the first solve block) even twice!

So we sure can save calc time if we rewrite the solve block function with these precalculated function values as arguments. The final function Rs6 calls the necessary function values and uses them to call the solve block function.

Werner_E_0-1755580281728.png

Replacing the function call P56(i) by a constant value P56_i so it has not to be called in every iteration step may not help that much as this function calculates rather quick, but replacing the two function calls F5(F, i) by a precalculated value sure should be an improvement.

Nonetheless calculation time is still 'infinite'.

EDIT: No, that's not true! The approach works pretty well - see my post below.

As you can see I tried to replace the simple zeros by the correct vectors and units, but to no avail. Turning on multithreading did not help and I also tried to use the correct results as guess values but doing so did not speed up calc time either.

Its already the first solve block function Rs34 which seems to run endless. 

EDIT: Not true! Rs34 is pretty fast. Not sure what I did wrong the first time I tried it.

I also tried setting TOL and CTOL from their default 10^-3 up to 10^-1 but again without noticeable effect.

Can't think of other attempts to speed up the calcs ..

EDIT: Not necessary to come up with other attempts 😉 The one outlined seems to be good enough - see below

Werner_E
25-Diamond I
(To:Werner_E)

Hmmm, I tried it again and I have no idea why the even first solve block seemed to take an endless time. It was lightning fast now.

I applied the changes described above (only replaced the solve block functions by precalculated constants) to the second, third and fourth solve block and calculation even of the last solve block is pretty fast now! I additionally trapped the error of the solve block not finding a solution for some angle values in the last solve block function and return a vector with NaN's in that case. If we would not do so and try to create a larger vector of R9 or R10 values and if just one angle values fails, the whole vector would fail.

 

Here is a rather meaningless 3D plot of the three components of R10 using about 50 angle values from -1.8 to +1.5

Werner_E_0-1755585507569.png

and here a 2D plot of the x and the z component of R9 for 166 angle values from -1.8 to 1.5. All done in very reasonable time.

Werner_E_1-1755585838351.png

 

Prime 11 sheet attached

 

Thanks for the help! 

Just to clarify in my mind, I can't see outside of the solve block where you have defined the outcome of the previous itteration of the function as a variable to feed into the next solve block. Does the underscore with the function components mean anything? In theory in the second solve block F5 could be defined as the result of a function before going into the solve block, but it seems that you define the variable of F5_Fi in the solve block itself. I'm guessing I missed something somewhere?

Werner_E
25-Diamond I
(To:SM_6465178)

OK, the second solve block function Rs6(F,i) should calculate the vectors R6 and M6 and has to use the result of the solve block function F5(F,i). Because F5(F,i) is dependent on F an i we cannot use an already calculated value in the second solve block. We must use the correct value which is dependent on the very values F and i which are provided as arguments in function Rs6.

So the solve block is turned into a function Rs6_SB which expects the correct value of F5 as one of its arguments (I used the name F5_Fi as formal argument name) and i as its second argument (to be able to calculate P56(i)). The final function Rs6(F,i) located outside of the solve block calculates the correct value of F5 based on the arguments F and i and calls Rs6_SB to make the desired calculation.

The big difference to your original approach of using F5(F,i) twice in the second solve block is that doing so would call function F5 twice in every iteration the solve block algorithm has to do. So if, lets say, the algorithm needs 300 iteration steps to calculate the vectors R6 and M6 with sufficient accuracy, the solve block function F5 would be called 600 times.

And as the solve block function of the second solve block is again used in the third solve block, this exacerbates the problem...

I guess you already have experienced the difference in calculation speed when you tried the worksheet in my reply.

I have not tried but I guess a tiny increase of speed could be gained if P56(i) is also provided as argument of Rs6_SB as I had shown in the picture in my previous reply. Similar doing with the other functions dependent on i (or i and  F) in the other solve block may squeeze out a little more speed.

 

Here is a simpler example of three cascaded solve blocks.

Basically both f3 and g3 calculate the 8th root of their arguments, using the solve blocks where each is calculating the square root.

Using a simple timer function where we call f3 resp. g3 (50 times in the screenshot) shows that g3 is about 100 times faster than f3 (varies from 80 times to 110 times when the sheet is recalculated a couple of times)

Speed gain is even greater if more blocks are cascaded and if the solve blocks have to work harder as in your case and so one single call takes much longer.

Werner_E_1-1755660956376.png

 

Prime 11 sheet attached

Ahh, I see. Makes perfect sense now. This will definely be very useful in a number of underperforming sheets we have here. Thanks for the clarifications

 

Shane

Announcements

Top Tags