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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Program to find area with Trap rule, but input parameter is tabular data, while output is value of integral

aalvarez
1-Newbie

Program to find area with Trap rule, but input parameter is tabular data, while output is value of integral

Hi guys, I am finishing up a course focused on half Excel and half Mathcad, with a smaller focus on the programing part of mathcad. I am having a lot of difficulties creating certain programs. My professor is finishing up the course with a small assignment that i cant seem to figure out. I AM NOT trying to get someone to do my work for me, I'm just truly lost when it comes to programming. Anyway, I attached an image of the description of the program she wants. I am familiar with the Trapezoidal rule (from my other math classes), and can come up with a simple program for Trap rule, but not with input parameters being tabular data. I'm not sure if this is a really simple program and I'm just not seeing it, but any help would be awesome! thanks!

Screen Shot 2015-04-29 at 11.21.33 AM.jpgScreen Shot 2015-04-29 at 11.21.21 AM.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
RichardJ
19-Tanzanite
(To:aalvarez)

Use local assignments with column operators on the first two lines of the program to split the table into two vectors:

View solution in original post

21 REPLIES 21
StuartBruff
23-Emerald II
(To:aalvarez)

The clue is in the text of the question. Although an integral is often the easy way to calculate the area given a function, you will have to sum all of the individual areas (defined by neighbouring x and y points. (You could convert the data to a function by one of fitting functions, but that doesn't appear to be what she's after so go for the "obvious" route).

A simple approach would be to create an x and y vector using your data,

then write a program that

....uses a for loop to caculate each area and assign it to a vector A (Mathcad's indices make the A<sub>i</sub> calculations look almost identical to the printed version you've shown),

....and then use the vector sum operator to add up all of the elements of A and returns the sum.

There are other ways of doing it, but this one is fairly straigtforward and meets the implicit need to "write a program".

Stuart

that seems like a straight forward approach with the Trap rule. my only issue is she wants the input parameters as "a tabular data", not in vector form. Unless theres someway of tricking mathcad into thinking the tabular data is actually two vectors. I'm going to ask her if vector inputs would be meeting her criteria. Thanks for your help/input. I really appreciate it!

RichardJ
19-Tanzanite
(To:aalvarez)

Use local assignments with column operators on the first two lines of the program to split the table into two vectors:

yes! that seems to be exactly what i needed. do you mind showing me the local assignment, how i would tell mathcad to split the table into the two vectors. I understand the image you attached, just want to know the pre-step. thanks again!

RichardJ
19-Tanzanite
(To:aalvarez)

If what I wrote is not clear please attach a worksheet with the data (click on "use advanced editor" at the top right of the edit dialog)

I entered the data in a tabular format above this programming, but it's not working...please help

StuartBruff
23-Emerald II
(To:lmousseau)

Leila Mousseau wrote:

I entered the data in a tabular format above this programming, but it's not working...please help

It's usually better to post a worksheet - some of the potential errors aren't visible in an image, such as writing a literal subscript when it should have been an index. It also makes it easier to see what the error messages are and try fixes. It also means that anybody looking at your problem doesn't have to type it all in.

In this instance, though, it looks as if you are using the default array ORIGIN of zero, which means that you would be trying to index Ylast row index + 1, and similar for X. This will result in an error. The easiest way to fix this would be to change the range of i from 1..last(Data) to 0..last(Data)-1. Alternatively, you could change i to i-1 and i+1 to i.

However, this is where a second problem comes in (actually, the first as far as Mathcad is concerned), namely that the function last only applies to vectors and not 2-dimensional arrays, such as Data; you need to replace last(Data) with last(X) - alternatively, you could use rows(Data)-ORIGIN-2 (or, simplifying for ORIGIN=0, rows(Data)-2).

As an aside, whilst assigning the sum of A to AT will work, it's rather inefficient (and a potential source of problems in more complex programs). Move AT outside of the for loop so that it's at the same indentation level as the return statement. (You could also do away with the assignment altogether; Mathcad functions return the value of the last evaluation, so you miss out the return statement entirely and just type the summation as the last line in the program).

Stuart

Stuart,

Thank you for your reply.

This is my first time here , so I apologize for not attaching the file. Here is what I have so far...

Thanks!

StuartBruff
23-Emerald II
(To:lmousseau)

Change last(Data)-1 to rows(Data)-2 in the for loop.

Remove the index from A in the summation .. the vector summation operator assumes its argument is a vector and doesn't need the index (also you're no longer in i's for loop).

It should work then.

Stuart

It worked!!!

Thank you so much 🙂

Hi Leila,

Do you mind attaching your finished work.

I keep trying to run my program but it keeps giving me an error when i go to solve for the area. specifically after i type Area(Data). the data part is in red and says this value must be a matrix of scalar elements. Thank you!

StuartBruff
23-Emerald II
(To:aalvarez)

Alexander Alvarez wrote:

Hi Leila,

Do you mind attaching your finished work.

I keep trying to run my program but it keeps giving me an error when i go to solve for the area. specifically after i type Area(Data). the data part is in red and says this value must be a matrix of scalar elements. Thank you!

It might be of more value to you to post your worksheet; that way somebody can assist you by pointing out the reasons you are getting the error and what steps you can take to put it right. It will be a better learning experience for others in a similar boat.

Stuart

Sure, Sorry about that!

RichardJ
19-Tanzanite
(To:aalvarez)

On the left of the assignment in the loop you should have A sub i (A[i), rather than just A.

did that, but now its giving me another errorScreen Shot 2015-05-03 at 11.35.01 AM.jpg

StuartBruff
23-Emerald II
(To:aalvarez)

Remove the index i from where you initialize A (A<-0); the index variable isn't defined at this point, hence the undefined variable error, and all that you effectively doing is declaring A and setting it value to 0.

You only need to add the array index to A inside the for loop. Mathcad automatically changes the type of A from scalar to array as soon as the first indexing operation occurs.

Stuart

RichardJ
19-Tanzanite
(To:StuartBruff)

The initialization of A and AT could both be removed. The assignment of the vector sum and the final return statement could also be combined to simply be "return vector sum", so the variable AT is dispensed with completely. Even the return keyword on the last line is not necessary, although it is good programming form.

Sorry I'm really bad at this, how would I add the array index?

never mind figured it out! Thank you!

StuartBruff
23-Emerald II
(To:aalvarez)

Alexander Alvarez wrote:

Sure, Sorry about that!

No worries, Alexander. The Posting Of The Worksheet is probably the most common thing that newcomers to the forum have to get used to.

Stuart

StuartBruff
23-Emerald II
(To:lmousseau)

Leila Mousseau wrote:

It worked!!!

Thank you so much 🙂

No worries, Leila.

Stuart

Even after several decades, I still find it surprising and exciting when a piece of code does what I intended it to do!

Top Tags