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

Newbie question

brittkelly2
2-Guest

Newbie question

I am simply trying to write a for loop that takes in an array and does some algebra on each number in the array and puts the result into a new array. 

 

I have attached the function(num,array)  whcih take in: num=the length of the array and array=my already made array. For simplicity of working on just the loop and arrays, I have made the algrebra portion just adding the number 1 to each value in the original array being sent into the function. 

 

Once I understand what I am doing wrong I can add in the actual math, however I'm stuck on how to make this for loop take in an array and return a new array. I know this is basic but I do not have any other instructions on mathcad other than the user manual and internet searches, however I still can't get it. 

 

Please help. Thank you. 

Brittany

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

OK, first let me say that you should always provide your Mathcad/Prime sheet (probably you will have to zip it before you can upload it here) rather than a pic as its hard to debug a picture and the willingness to help fades a little when one sees that its necessary to retype all from scratch rather than playing around with a provided worksheet.

In case of Prime you should additionally attach screenshots and/or pdf prints as because of the incompatibility of the file formats of the various versions and the inability of a higher version to save a file in the format of an older one, many people here can't read the format of the newer versions of Prime,, but maybe could help by just seeing a picture.

 

You don't say what kind of error message you get and its hard to see from the picture, but it looks to me like you had used the wrong type of index. In Mathcad we have two different types of indices. The literal index (keyboard: Ctrl+Shift+-) and the vector index (keyboard: [ ). You have to use the latter for indexing matrices and vectors like your array.

 

Using the right index you don't get an error anymore but sure not the result you expect as in the last line you use math[i and that way you just return a single scalar value and not the whole matrix (see the screenshot below)

 

Some remarks:

  • Its not necessary to initialize the for-loop variable i to zero
  • You don't have to provide the length of the array as function argument as Mathcad provides the function "last" to determine the last index of a vector
  • You introduce two new arrays in your function, math and newArray. Thats inefficient and not necessary. Unless you have to reuse "older" array values for your calculations you can use the argument for the new values as well. This does not change anything in the array you provide as function argument. The function is playing with its own local copy. A program in Mathcad can't change any worksheet variable from within the program.

In my example function3 I set up a function which handles a scalar (in our case simply adding 1 to the argument) and then I call ths function with a vector as argument. As long as the calculations in that function don't make sense for vectors (like x^2 or sin(x), ...) Mathcad automatically performs so called "vectorization". This means that it feeds all elements of the argument vector one after the other into the function and collects and returns the results in a vector. If you want to be on the safe side you apply vectorization manually (the arrow above the expression).

 

Give it a try yourself - define f(x):=x*x (don't type  x^2) and call that function with a vector as argument. Without vectorization you will get a scalar (the dot product), with vectorization you get a vector with squared elements as result.

 

Bild.png

View solution in original post

3 REPLIES 3

OK, first let me say that you should always provide your Mathcad/Prime sheet (probably you will have to zip it before you can upload it here) rather than a pic as its hard to debug a picture and the willingness to help fades a little when one sees that its necessary to retype all from scratch rather than playing around with a provided worksheet.

In case of Prime you should additionally attach screenshots and/or pdf prints as because of the incompatibility of the file formats of the various versions and the inability of a higher version to save a file in the format of an older one, many people here can't read the format of the newer versions of Prime,, but maybe could help by just seeing a picture.

 

You don't say what kind of error message you get and its hard to see from the picture, but it looks to me like you had used the wrong type of index. In Mathcad we have two different types of indices. The literal index (keyboard: Ctrl+Shift+-) and the vector index (keyboard: [ ). You have to use the latter for indexing matrices and vectors like your array.

 

Using the right index you don't get an error anymore but sure not the result you expect as in the last line you use math[i and that way you just return a single scalar value and not the whole matrix (see the screenshot below)

 

Some remarks:

  • Its not necessary to initialize the for-loop variable i to zero
  • You don't have to provide the length of the array as function argument as Mathcad provides the function "last" to determine the last index of a vector
  • You introduce two new arrays in your function, math and newArray. Thats inefficient and not necessary. Unless you have to reuse "older" array values for your calculations you can use the argument for the new values as well. This does not change anything in the array you provide as function argument. The function is playing with its own local copy. A program in Mathcad can't change any worksheet variable from within the program.

In my example function3 I set up a function which handles a scalar (in our case simply adding 1 to the argument) and then I call ths function with a vector as argument. As long as the calculations in that function don't make sense for vectors (like x^2 or sin(x), ...) Mathcad automatically performs so called "vectorization". This means that it feeds all elements of the argument vector one after the other into the function and collects and returns the results in a vector. If you want to be on the safe side you apply vectorization manually (the arrow above the expression).

 

Give it a try yourself - define f(x):=x*x (don't type  x^2) and call that function with a vector as argument. Without vectorization you will get a scalar (the dot product), with vectorization you get a vector with squared elements as result.

 

Bild.png

OH my, I'm sorry I am a super noob. It is only my third day using mathCAD. 

I was definitely using the literal index and not the vector index, you are right about that. Also the vectorization part was really confusing to me as well. Thank you for the excellent explanation! 

You are welcome!

Keep on going and come back asking here if you get stuck.

 

Top Tags