Skip to main content
4-Participant
December 16, 2023
Solved

Calculate multiple values using the same equation

  • December 16, 2023
  • 1 reply
  • 2452 views

I have a simple equation but want to use different input values and calculate the results without repeating the equation.  How do you do this?

 

I am using MathCAD Prime 9.0

Best answer by Werner_E

@cfelice wrote:

I have a simple equation but want to use different input values and calculate the results without repeating the equation.  How do you do this?

You simply turn the calculation/equation into a function of the input values.

 

For example

Instead of

Werner_E_0-1702748307574.png

You define a function and use it with different input values

Werner_E_1-1702748332360.png

 

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
December 16, 2023

@cfelice wrote:

I have a simple equation but want to use different input values and calculate the results without repeating the equation.  How do you do this?

You simply turn the calculation/equation into a function of the input values.

 

For example

Instead of

Werner_E_0-1702748307574.png

You define a function and use it with different input values

Werner_E_1-1702748332360.png

 

cfelice4-ParticipantAuthor
4-Participant
December 21, 2023

I also found that using a X by 1 matrix will also work.

 

cfelice_0-1703160043396.png

 

25-Diamond I
December 21, 2023

@cfelice wrote:

I also found that using a X by 1 matrix will also work.

 

cfelice_0-1703160043396.png

 

 


This method is a bit limiting compared to a functional approach, but if thats what you need - why not.

But use it carefully - depending on the calculations used the method may fail.

This works:

Werner_E_1-1703181169422.png

But written this way the result is not what you may expect.

Werner_E_2-1703181243981.png

Reason is that x*x is calculated as vector dot product.

You can fix it by applying vectorization - which is advisable to use always when you want a calculation written for a single scalar should be applied elementwise at the elements of a vector.

Werner_E_3-1703181418119.png

 

The first example (using x^2) worked because Prime applies automatic implicit vectorization if an operation (like squaring) is not defined for vectors.