Skip to main content
3-Newcomer
July 10, 2025
Solved

Vector depending on variables

  • July 10, 2025
  • 1 reply
  • 893 views

Hello,

 

i like to calculate  force FA depending on three variables hS, hA, Hz.

 

Mathcad calculates FA and put into a vector.

 

Finally, i like to have the maximum of this vector.

 

Typing max(FA) gives back the whole vector not the single maximum value.

 

Also for mean and other functions.

 

Thank you for your help.

Best answer by Werner_E

If you define a vector with 8 steps you will get a vector with 9 entries.

So the number of combinations from vectors with 8 steps, 6 steps and 12 steps would be 819, not 576 šŸ˜‰

 

I would suggest a different approach. Instead of creating these huge input vectors with repeated values I would leave them as we already had defined. 

Its a good thing that you have turned all your calculations into function of the three inputs.

I think what you are interested in are the three forces and the maximum of each.

So why not separate definition and display of values.

Define your functions first without having  the inputs already defined and without displaying numeric intermediate results like angles.

After all functions are defined, define the three input vectors and use a program to cycle through all values:

Werner_E_0-1752251229164.png

Werner_E_1-1752251266541.png

FAall, etc. are vectors with 819 elements (because I used your example of 8, 6 and 12 steps for the input vectors).

At the end of the attached Prime 10 sheet I also show a way to create a table of values, not sure if that would be of interest:

Werner_E_3-1752251829927.png

As a byproduct, you also are offered a way of creating the three input vectors with repeating elements that you have suggested. You can therefore continue with your approach and then decide which is more suitable for you.

 

Prime 10 sheet attached

 

 

1 reply

25-Diamond I
July 10, 2025

Welcome to the forum.

You should always whenever somehow possible attach your worksheet and also say which version of Prime you are using. This is especially important to do if, like so many, you are not using the latest version.

 

According your problem - you are not showing how you defined your  three variables.

My guess is that you defined them as ranges and not as vectors which would explain the effects you experience.

 

Solution is to define the three input variables as vectors and use vectorization when you call your function FA(hS, hA, hZ). Depending on the way your function FA is defined it could be that explicit vectorization is not necessary because Prime uses automatic implicit vectorization, but I would strongly suggest that you apply explicit vectorization in any case.

 

If you are using Prime 11 you can use the convenient new function "vec" to create the vector. If you are using an older version there sure are many other ways to create your vectors or turn the ranges you have defined into vectors.

 

If you are unsure, feel free to come back here, attach your worksheet, state the version and ask.

 

BTW, the difference between vectors and ranges has confused users ever since Mathcad was originated. With Prime it got even worse, because both look completely identical when displayed. So you are certainly not alone and without doubt not the first to have problems with this. šŸ˜‰

 

Here is a small example demonstrating the difference between ranges and vectors and also the usage of vectorization:

Werner_E_1-1752135611079.png

 

Prime 11 sheet attached

3-Newcomer
July 10, 2025

Thank you Werner for your fast response. I am using Prime 10.0.1.0. I also attached my mcdx. 

 

You are right, i am using ranges for defining hS, hA, hZ.

 

My sheet calculates force on three cylinders of an arm with three joints.

 

The strokes of the cylinders hS, hA, hZ affect the resulting force.

 

The idea is to calculate various positoins (i) of hZ, various positoins (i) of hA and various positoins (i) of hS to have in total i^3 positions with respective force.

 

When I am using the range function e.q.

hA:=0,885..885 (two positions)

hS:=0,885..885 (two positions)

hZ:=0,885..885 (two positions)

It calculates 2^3=8  positions.

 

But when i am using:

hA:=[0 

        885] (two positions)

hS:=[0 

        885] (two positions)

hS:=[0 

        885] (two positions)

 

Result is also a vector 2 positions.

 

So I think now I am searching for soultion to automize definition of my input vectors, because I like to vary the cylinder position of each cylinder at least by 10 steps. If I am right i need three vectors each with 10^3 entries.

 

thanks in advance

 

 

 

25-Diamond I
July 10, 2025

Here are two ways to create the necessary input vectors.

For convenience I use a variable "n" which is the number of intervals between start and end value of the vector which means that the vectors will have n+1 elements each.

First method uses a range variable "i" which had to be defined:

Werner_E_0-1752161766604.png

A range variable is not a collection of values but rather kind of an implicit loop. So the same effect can be achieved by using a small utility program with an explicit for-loop. This utility function could be hidden at the top of the sheet in a collapsed area or to the right of the right margin. By using this function, we can omit the definition of the range variable ā€œiā€ which is the reason I prefer this way.

The utility function:

Werner_E_1-1752162001351.png

and how to use it:

Werner_E_2-1752162052002.png

 

A word of caution: You called your various function with the vectors h.Z, etc as function arguments and did not use vectorization (the arrow over the expression).  This seems to work well in the case of your function, but can lead to dangerous errors that are difficult to detect.

For example, if one of your functions contained an expression such as

Werner_E_3-1752162447969.png

the function result would be incorrect when called without vectorization and you might not recognize it at first glance. Here are the differences:

Werner_E_5-1752162488324.png

Reason for the difference is that without vectorization the expression

Werner_E_6-1752162548795.png

is calculated as the scalar product of two vectors. Adding the vector h.S makes the result a vector again.

Using vectorization lets Prime do the multiplication element by element and collects the results in a vector, which is whats needed here

Werner_E_7-1752162671034.png

If you feel the using vectorization every time a function is called with vector arguments is annoying and may irritate a reader, you can also apply the vectorization when you define the function. For example:

Werner_E_8-1752162781880.png

So here vectorization is not necessary when you call the function.

Personally I try to use vectorization every time I intend to call a function written for scalar arguments with vector arguments, no matter if its really necessary (because otherwise a scalar product would be calculated) or not - just to be on the safe side.

 

Prime 10 sheet attached