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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

FOR loop with "This value must be a scalar" error message

Ivan_Pat
3-Newcomer

FOR loop with "This value must be a scalar" error message

Hi to everyone!

I'm a newbie here and need help finding the idea for the solution to my issue—a brief explanation of the problem.
Mathcad Prime 9.0.0 is used for the modelling.

There is a FOR loop for which data is calculated, and there are conditions for selecting the coefficient. If the condition is TRUE, I want the program to choose the k1  and f values to be represented in the augmented matrix in the output.
But, as I can guess, the problem that prevents my loop from performing as it should in the type of input variable - each i-th value of the input variable u-value has to be checked for a condition—if it is TRUE, it goes to the list of output values; if FALSE, it returns an empty matrix.
Mathcad doesn't seem to recognise the particular u-value; it simultaneously recognises all the set of u-values [matrix]. Thus, I probably have the error message "this value must be a scalar."
PS The attached file could clarify my issue.

 

I would appreciate any information and help you can give.

ACCEPTED SOLUTION

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

I am not sure if the attached modification does what you intended, but I am pretty sure that you don't need to create any vectors (vector index i) in your program.

See if the result of the attached file meets your expectations.

 

@StuartBruff 

Zs1 and Zs2 are 2x2 matrices defined in front of the program

 

View solution in original post

9 REPLIES 9
StuartBruff
23-Emerald III
(To:Ivan_Pat)

The problem is that d1 and d2 are vectors within the loop.  At each iteration, their next value is the whole of the current value ... which is a vector.  Consequently, Interval creates a deeply nested vector of vectors and, hence, the "must be a scalar" error.

 

2024 12 12 A.png

 

You could try initializing d1 and d2 to be single-element vectors and then using d1i-1 and d2i-1 within the loop; as i runs from 1..n, and your ORIGIN=0, this should pick up the first values of d1 and d2, and give you the desired results.  

 

However, you have further problems further down the program in the expression Zs2*Zi*Zs1 as neither Zs2 nor Zs1 seem to be defined.

 

Stuart

@StuartBruff Thank you for such a simple explanation.

It is a way to see that my idea with these vectors is probably not the best way to iterate variables...

I'll try to think of what could be replaced instead.

 

Werner_E
25-Diamond I
(To:Ivan_Pat)

I am not sure if the attached modification does what you intended, but I am pretty sure that you don't need to create any vectors (vector index i) in your program.

See if the result of the attached file meets your expectations.

 

@StuartBruff 

Zs1 and Zs2 are 2x2 matrices defined in front of the program

 

Ivan_Pat
3-Newcomer
(To:Werner_E)

@Werner_E 

It looks pretty cool and meets my requirements. But, to be frank, I would like to sort solutions which meet my restrictions and not see all the dozens of "0" values in the final INTERNAL matrix. 

 

Nice to understand that here, in the community, I found the solution for my last month's headache.)))

StuartBruff
23-Emerald III
(To:Ivan_Pat)


@Ivan_Pat wrote:

@Werner_E 

It looks pretty cool and meets my requirements. But, to be frank, I would like to sort solutions which meet my restrictions and not see all the dozens of "0" values in the final INTERNAL matrix. 


 

What do you mean by not see the zero values in Internal?

 

Werner's version of your program returns a lot of zeros because that's what the conditionals select.  I've modified Werner's program to return u_value, d1, d2, and d1+d2 in addition to f and k1 to allow inspection of the values.  

 

2024 12 13 A.png

 

Do you want the program to return Internal with the zeros and then remove them, or do you want it not to add the zero f and k values?

 

One external method is to apply a filter function to Internal that selects for f and k1 both being non-zero.   There do not appear to be many non-zero values.  Is that what you would expect?

 

2024 12 13 B.png  

 

Stuart

 

I have Mathcad Prime 10 so can't save my worksheet as Mathcad Prime 9.  I recommend that you upgrade to Mathcad Prime 10, if possible.  You can install it in parallel with Mathcad Prime 10.

Werner_E
25-Diamond I
(To:Ivan_Pat)


@Ivan_Pat wrote:

@Werner_E 

It looks pretty cool and meets my requirements. But, to be frank, I would like to sort solutions which meet my restrictions and not see all the dozens of "0" values in the final INTERNAL matrix. 

 

Nice to understand that here, in the community, I found the solution for my last month's headache.)))


The zeros clearly stem from the else branches in the two if-statements.

Werner_E_0-1734104283005.png

The condition seems to be not fulfilled for most values.

I just noticed now that the condition in both if-statements is basically the very same, so this part of the program could be simplified.

As the start values for d1 and d2 are 0.1 cm and 0.5 cm and then are multiplied by positive numbers (i), they never can be zero or negative.

We can therefore omit asking if they are larger than or unequal to zero - they sure are.

I added a header line and added the index i as well as the values of d1 d2 and u_value (for space reasons with units stripped) for your information similar to what Stuart had done. Return values are created only if the conditions are met, so you get rid of the double zeros in the result matrix.

Werner_E_2-1734108215555.png

This creates a matrix with just 13 data rows.

Werner_E_3-1734108241887.png

 

I also added a routine Internal.all at the end of the sheet to create all 100 data rows so you can inspect the corresponding d1 and d2 values.
Unfortunately scrolling in a large matrix is horrible implemented in Prime 😞

 

But its easy to explain why the output of valid values is just a small matrix:

Our program starts with d1=0,1 cm and d2=0.5 cm and creates 100 pairs of d1-d2 values by increasing d1 for 0.1 cm and d2 for 2.5 cm in each step. The last pair of values thus is d1=0.1 m and d2=2,5 m.

For the first 10 pairs the condition u_value<0.25... is not met.

And for the pairs #24 up to #100 the condition d1+d1<0,6 m is not met because d2 is already too large.

Werner_E_4-1734108313481.png

 

I am not sure if you really wanted to increase d1 and d2  simultaneously the same time or if you rather wanted to increase them independent from each other, thus creating 100 * 100 = 10 000 pairs of values!? This would require two nested for loops.

 

Ivan_Pat
3-Newcomer
(To:Werner_E)

@Werner_E You are right " As the start values for d1 and d2 are 0.1 cm and 0.5 cm and then are multiplied by positive numbers (i), they never can be zero or negative." These values apriori will be positive non-zeros. We can omit such conditions.

Excellent, that is what I wanted to get. 

Thanks a lot. Problem solved.

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

@StuartBruff 

Zs1 and Zs2 are 2x2 matrices defined in front of the program


I'm not sure whether it's my eyes or brain that needs checking ... no, scrub that; I've just had my eyes checked.   🙂

 

I'd modified d1 and d2, and Mathcad threw up a "this variable is undefined" error on Zs1 or Zs2.  I might have accidentally modified a variable's name when editing the program.

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)


@StuartBruff wrote:

@Werner_E wrote:

@StuartBruff 

Zs1 and Zs2 are 2x2 matrices defined in front of the program


I'm not sure whether it's my eyes or brain that needs checking ... no, scrub that; I've just had my eyes checked.   🙂

Hmm, given from what you wrote about you 4K notebook display and the zoom/scaling factors you use, it seems not to be your eyes.

And judging from the quality of your contributions it can't be your brain, either.

Maybe its just a matter of "Nobody's perfect" 😉

Announcements

Top Tags