Skip to main content
1-Visitor
February 9, 2012
Solved

Combining numeric and symbolic computation

  • February 9, 2012
  • 14 replies
  • 9203 views

I'm a new Mathcad user. I'm trying to do something that's intuitively simple on paper, but potentially very tricky for a computer program (so tricky, in fact, that I'm not sure how to explain it in rigorous terms; thus the long post). Please tell me if there's a straightforward way to do this.

From a high-level point of view, I can say that I want my program to "know" both the symbolic value and the numerical value of my expression at the same time. Let me define the fictitious operators 's=' for symbolic evaluation and 'n=' for numerical evaluation. Let the assignment operator be ':=', same as in Mathcad. Equipped with these fictitious operators, I could say:

a := b^2 - 1

b := c + 2

c := 5

a s= (c + 2)^2 - 1

a n= 48

From a slightly lower-level point of view, I can say that I want to be able to "hold" or "delay" substitution of numerical values into my expressions. Let me define a different set of fictitious operators:

  • ':=' - regular assignment, like we all know
  • 'd:=' - delayed assignment
  • '=' - full evaluation (evaluates both regular and delayed assignments)
  • 'd=' - delayed evaluation (or "weak", if you will; evaluates regular assignments only)

The same example from above, recast in these terms, might look like:

a := b^2 - 1

b := c + 2

c d:= 5

a d= (c + 2)^2 - 1

a = 48

Background information (with more specific examples)

I'm coming at this from a Mathematica background. Mathematica allows symbolic manipulation with variables that haven't been defined. A purely numerical version of my example calculation might look like this (using ':=' for assignment and '->' for evaluation, which isn't quite the way Mathematica works):

a := b^2 - 1

b := c + 2

a -> (c + 2)^2 - 1

c := 5

a -> 48

This much is easy in Mathcad. The trouble is that once I've defined a value for 'c', I can no longer recall the expression on the third line in symbolic form. But Mathematica has an additional wrinkle in that it allows variables to have numerical values that are suppressed in symbolic manipulations. It all works using the function/operator 'N'. The same calculation in combined numeric-symbolic form would look like this:

a := b^2 - 1

b := c + 2

N(c) := 5

a -> (c + 2)^2 - 1

N(a) -> 48

Both the values of 'a' and 'N(a)' exist simultaneously in the workspace. By assigning a value to 'N(c)', I haven't lost the symbolic expression for 'a'.

(Syntax sticklers unfamiliar with Mathematica: the operator 'N' is overloaded. When it appears on the left-hand side of an assignment, it modifies the assignment. When it appears in an expression, it modifies the evaluation.)

Best answer by PhilipOakley

ivg,

If you want to have a symbolic answer for a symbol that also has a numeric value, then you add Symbol:=Symbol after its numeric definition., and before it is used in the solve.

Mathcad searches back through the sequence of definitions until it either finds a numeric value for a symbol (Integer or decimal) and would use that value. In this Symbol:=Symbol case mathcad sees the definition as recursive and stops searching, so just uses the symbol rather than its numeric value.

I mentioned Integer and decimal values - if a symbol is defined using integers then you get 'integer' solutions to the solve, otherwise you will get a floating point approximation to the number of digits precision requested, e.g. 400 (or the default) Such floating point is different to the 'normal' 64 bit double precison computer arithmetic (about 15 decimal places max).

In Summary: stick b:=b immediately after b:=5.

Philip

14 replies

1-Visitor
February 9, 2012

Have you looked at using the "explicit" keyword under the symbolic menu. With the explaination, there is a tutorial link when you navigate to the "explicit calculation" page under the help menu.

I hope this helps.

Norm

ivg1-VisitorAuthor
1-Visitor
February 9, 2012

I tried it, but I couldn't get it to behave the way I wanted it to. Maybe the key is to pass it the right arguments? Suppose I have the following two statements:

c := a^2 + b^2

a := 2*b

How can I use explicit to tell me that c = 5*b^2?

24-Ruby IV
February 9, 2012

substitute.png