Get Help

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Community
- :
- PTC Mathcad
- :
- PTC Mathcad
- :
- Why is my program returning these two values?

Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

07-13-2021
07:57 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-13-2021
07:57 AM

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

07-13-2021
08:20 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-13-2021
08:20 AM

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

07-13-2021
09:29 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-13-2021
09:29 PM

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.

07-14-2021
03:58 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Notify Moderator

07-14-2021
03:58 AM

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