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

subtract two functions


subtract two functions

Hello dear community,

I actually have a simple question. I'd like to subtract two functions in Mathcad that work but not the way I want.


Here is my problem as an example:




At the beginning I defined the range of the function.
In the function C(x1) I want to calculate the difference between the two functions. If I do this, the vector C(x1) does not have 5 sizes, but 25.

What do I have to do, so that C(x1) is only the difference of the individual sizes of the function?


Thank you very much!



Re: subtract two functions

Please post your Mathcad worksheet. !
Further: your definition of x1 defines it as a range , NOT a vector.
Ranges used on the worksheet act as a kind of iteration. You can use ranges to define a vector.
In many cases, when working with functions where you supply a vector as the argument , you need to vectorize the expression to get the desired result (element by element).


Re: subtract two functions

Other than that (above). Here is what I get from your description:


Where is the problem?

In other words: Post your worksheet (or a worksheet that at least shows your problem).




Re: subtract two functions

Maybe the following screenshots helps. Otherwise you should attach your worksheet (and state which version of Mathcad or Prime you are using).


Re: subtract two functions


Although I have used Mathcad 15, the results are the same as those obtained with Prime 6. See below:

Two vectors difference.png

Re: subtract two functions


thank you very much for your quick and good feedback!


Of course you are absolutely right with your result. I had the problem with the work and then I just described it similarly from my head. Sorry.


Here is the original problem:




The first two contain 27 elements, the last 729. But I don't understand why? I want to subtract every single element so that I get 27 elements in the result.


I sent the file with it. The problem is at the  end.

Thanks a lot!


Re: subtract two functions

So the difference is that the first few examples in this thread created a new function f(x2) = D1(x2) - D2(x2).  This means that when the function is called, it passes the "same" value of x2 into both sub-functions, D1 and D2, at the same time.   For clarity, this function should be written using another dummy parameter,


     f(z) = D1(z) - D2(z)


     f(x2) = [27 element vector, 1 subtraction operation for each value of the x2 values passed in]

If you calculate the difference explicitly, D1(x2) - D2(x2) = , then D1(x2) is passed the entire range of x2 values (27 results) and D2(x2) is given the entire range of x2 values independently (27 results).  You get a final result that contains every combination of results between the two terms, or 27*27 = 729 discrete results.


To keep this from happening, either

  1. Put the difference in a function (which gets passed a single scalar value).  When the function is called with x2 as the parameter, it passes in each value of x2, 27 times, once for each value of x2.  Or...
  2. Vectorize the equation
    • Select the difference equation
    • Select the "Vectorize" operator from the toolbar
    • an arrow will appear over the entire expression
    • This performs the expression for each matching pair of x2 values in each function

Hope that helps.


Re: subtract two functions

I don't think that's where the cause of the problem lies. See here:


No difference when I define a function (C) to do the subtraction or when I subtract the two functions.


Note that in the problem case x1, x2 etc are NOT a vector, but they are ranges.

My suspicion is that in one (ore more) of the functions an x1 is not the same x1 as all the others. The labels are different. That causes the iteration (of the range variable).

This should become clear when the function parameters are chosen with a name that is not known on sheet level. So use x, or z or anything else, but not one of x0, x1, x2 etc.



Re: subtract two functions

The real and main problem is that x2 in your sheet is NOT a vector but just a range. Thats the reason for the 729 values you get.  Unfortunately ranges and vector look the same in Prime.

You may want to take a look at the attachment I posted here - it still applies!


You may already have noticed that you would not be able to assign the result of your difference to a variable and can not access individual values.

A range is not a series of values, its rather kind of a loop, an iteration. And two nested loops running from 5 to 31 each result in 729 calculations.


Ranges should only used for 3 purposes:

1) To index the elements of a vector or matrix (like the range i in my picture

2) To define a range of abscissa values for plotting

3) As a loop in a Mathcad program


For all other purposes you should use vectors!


See the picture for possible solutions to your problem:


EDIT: Sorry! It seems that at the moment we are not able to embed pictures in the text (the editor freezes). So I attach it to the post.


Re: subtract two functions

@LucMeekes wrote:


My suspicion is that in one (ore more) of the functions an x1 is not the same x1 as all the others. The labels are different. That causes the iteration (of the range variable).

You are perfectly right about this!

One argument is labelled "(Variable)", the other "Variable". So Prime correctly has decided that the first argument should have the label "Variable" but it still treats the variables as different in some way even though both arguments use the same variable (there is only one x2 defined).
Once again I know why I dislike this Prime "Feature"!

See the attached picture (at the moment I seem no to be able to embed pics.


The main problem of course still is that ranges are used where vectors should be used.