cancel
Showing results for
Did you mean:
cancel
Showing results 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

4-Participant

## Why is my program returning these two values?

Relatively green Mathcad Prime 7 user here. I am trying to write a simple iteration program so that I can eventually try to adapt it for my needs. My question is: Why is my program returning 0 and 1 as well as multiplying everything by 1? I just want to see the values from 1..n where's f(n) is my input. I have minimal experience coding so I apologize if the answer is relatively easy or has been in front of my face the entire time. I will attach a file upload as well as a pictures, thanks in advance.

3 REPLIES 3
23-Emerald II
(To:SS_9985257)

The reason you are seeing a "0" is that Mathcad's default matrix index origin is zero.    When you assign a value to a nonexistent array, Mathcad creates the array, with you index being the last item in the new array.   Mathcad fills all previous undefined elements with zeros.

You've created an array R with index 1 at the start of your loop.   Mathcad creates the array R with index 1 and fills index 0 with zero (as per the array creation rules).   Interestingly, it doesn't complain about R not existing during the first assignment, so I'm guessing that it creates R as a single element array with index 0 before doing anything else.

So, at the end of the first iteration, you have an array with index 0 = 0 (autofill) and index 1 = 1.

However, at the next iteration, you set the array index 2 to be the entire current value of  R plus 1.  Hence the appearance of a 2x1 vector in index 2.   Similarly at each subsequent iteration, you add one to the the entire array.

To get a better picture of  what's happening, go to the Matrices/Tables tab and deselect the "Collapse Nested Matrices) button.   This should show all the values for each index of R.

What were you hoping that the result would be?

Stuart

25-Diamond I
(To:SS_9985257)

Are you looking for something like this:

Note that all functions are written to be independent of the current setting of the system variable ORIGIN. This variable is set by default to 0 while I guess you think it should be 1.

23-Emerald III
(To:Werner_E)

An alternative for ones4 (simpler, and works in express) and another possibility: ones5.

Although, I must admit, ones5 does not produce a vector, it only appears to be a vector.

Success!
Luc

Announcements

Top Tags