Skip to main content
13-Aquamarine
December 12, 2024
Solved

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

  • December 12, 2024
  • 2 replies
  • 1633 views

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.

Best answer by Werner_E

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

 

2 replies

23-Emerald V
December 12, 2024

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

Ivan_Pat13-AquamarineAuthor
13-Aquamarine
December 13, 2024

@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_E25-Diamond IAnswer
25-Diamond I
December 13, 2024

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_Pat13-AquamarineAuthor
13-Aquamarine
December 13, 2024

@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.)))

23-Emerald V
December 13, 2024

@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.