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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Why is my program returning these two values?

SS_9985257
4-Participant

Why is my program returning these two values?

Capture1.PNG

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

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

Are you looking for something like this:

Werner_E_0-1626229348498.png

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.

 

LucMeekes
23-Emerald III
(To:Werner_E)

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

LucMeekes_0-1626249460214.png

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

 

Success!
Luc

 

Top Tags