Skip to main content
8-Gravel
October 2, 2025
Solved

Mathcad Intersections and Discretized Matrix Data

  • October 2, 2025
  • 2 replies
  • 1962 views

I would like some help to develop a loop in Mathcad that appends each intersection point between a piecewise linear curve and a set of straight lines, to the piecewise linear curve matrix. The attached Mathcad Prime 11 file contains the details and a fuller explanation of this request for assistance. 

Best answer by Werner_E

Here is your program fixed :

Werner_E_4-1759468115865.png

But I would rather prefer to make the program self-contained, independent from the existence of global worksheet variables ninc, Avals and Bvals. And it may also be a good idea to make it ORIGIN aware so that it does not rely on ORIGIN being set to zero:

Werner_E_5-1759468262931.png

You now can create a table similar to the one in the picture embedded in your sheet

Werner_E_1-1759467611390.png

or you may create a plot showing all the points of intersection

Werner_E_2-1759467621772.png

For the plot I had replaced two values in the C-vector to test my function in case a horizontal line is too low or too high and does not intersect the graph at all. Therefore the lines at height 24 and 36 are missing as they were replaced by lines in height -10 and 50. In this case my program returns a simple NaN instead of a row vector with x-values. 
I am aware that you wrote that this case never occurs in your application, but I believe that a program should always be able to handle all possible cases whenever possible (provided that this can be achieved with reasonable programming effort).

 

EDIT: Added a version where multiple identical values such as the duplicate 500 in the last row are removed.

 

Hope that the attached Prime 11 file will help.

2 replies

21-Topaz II
October 3, 2025
8-Gravel
October 3, 2025

Thanks Terry. This is a neat solution and does achieve what I had requested. Grateful for the time you spent on it and help it provided me in seeing alternative approaches to solving the problem.

I am impressed by the short time between posting the request for help and the provision of your answer.

Werner_E25-Diamond IAnswer
25-Diamond I
October 3, 2025

Here is your program fixed :

Werner_E_4-1759468115865.png

But I would rather prefer to make the program self-contained, independent from the existence of global worksheet variables ninc, Avals and Bvals. And it may also be a good idea to make it ORIGIN aware so that it does not rely on ORIGIN being set to zero:

Werner_E_5-1759468262931.png

You now can create a table similar to the one in the picture embedded in your sheet

Werner_E_1-1759467611390.png

or you may create a plot showing all the points of intersection

Werner_E_2-1759467621772.png

For the plot I had replaced two values in the C-vector to test my function in case a horizontal line is too low or too high and does not intersect the graph at all. Therefore the lines at height 24 and 36 are missing as they were replaced by lines in height -10 and 50. In this case my program returns a simple NaN instead of a row vector with x-values. 
I am aware that you wrote that this case never occurs in your application, but I believe that a program should always be able to handle all possible cases whenever possible (provided that this can be achieved with reasonable programming effort).

 

EDIT: Added a version where multiple identical values such as the duplicate 500 in the last row are removed.

 

Hope that the attached Prime 11 file will help.

8-Gravel
October 3, 2025

Thanks. This is awesome!! Your solution not only solved the problem as per my request, but also provided a refinement that corrected other issues I had not mentioned in my earlier post regarding the removal of duplicates. I appreciate the fact you fixed my attempt as well as providing a better version.

If you have time, would you please add a function to the end of your solution that would determine for each constant y value in Cvals matrix, the area under the discretized curve between y = 0 and y = the constant in the corresponding Cvals row? For example, when a row in Cvals = 24, then the area calculated would be as shaded in purple in the figure posted with this additional request. Obviously I will need to add units to the values. Mostly interested in how to write the loop so that it knows when to use the trapezoidal rule based on the coordinates of the discretized curve and when to use the coordinates that the earlier function interpolated based on intersecting the y = constant lines.  

8-Gravel
October 3, 2025

I tried to add units and ran into some difficulty with the NaN values, which I subsequently filtered but it is still a bit of a mess. I'd appreciate any help you can provide in correcting it.