Skip to main content
1-Visitor
November 19, 2019
Question

subtract two functions

  • November 19, 2019
  • 6 replies
  • 4504 views

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:


x1:=1..5
A(x1)=2*x1+3
B(x1)=4*x1+1

C(x1)=A(x1)-B(x1)

 

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!

 

6 replies

23-Emerald IV
November 19, 2019
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).

Success!
Luc
23-Emerald IV
November 19, 2019

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

LM_20191119_SubtractFunctions.png

Where is the problem?

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

 

Success!
Luc

 

25-Diamond I
November 19, 2019

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

B.png

21-Topaz II
November 20, 2019

Hi,

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

Two vectors difference.png

tomekk1-VisitorAuthor
1-Visitor
November 20, 2019

Hello,

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:

 

Unbenannt.PNG

 

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!

 

JeffH1
16-Pearl
November 20, 2019

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)

then

     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.

Jeff

23-Emerald IV
November 20, 2019

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

LM_20191120_SubtractFunction.png

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.

 

Success!
Luc

tomekk1-VisitorAuthor
1-Visitor
November 25, 2019

Thanks a lot!

 

Your answers helped me a lot. So I was able to solve the problem.