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

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Combining numeric and symbolic computation

ivg
1-Newbie
1-Newbie

Combining numeric and symbolic computation

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.)

1 ACCEPTED SOLUTION

Accepted Solutions
PhilipOakley
5-Regular Member
(To:MikeArmstrong)

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

View solution in original post

14 REPLIES 14
nas0k
7-Bedrock
(To:ivg)

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

ivg
1-Newbie
1-Newbie
(To:nas0k)

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?

ValeryOchkov
24-Ruby IV
(To:ivg)

substitute.png

VladimirN
24-Ruby II
(To:ivg)

Please see the attachment (include worksheet for Mathcad 15).

Pic_1.png

MikeArmstrong
5-Regular Member
(To:VladimirN)

Do we need all those symbolic functions?

Clipboard01.jpg

Mike

ivg
1-Newbie
1-Newbie
(To:ivg)

Thanks, guys. Not really what I'm looking for, though (ask the wrong question, get the wrong answer).

The equality operator (Valery) is nice, but confines me to explicitly telling Mathcad which expressions to substitute where, as opposed to having global definitions. I want to consider that a last resort.

Vladimir's examples work perfectly for the simple situation in my second post. But when I define a numerical value for 'b', they collapse into purely numerical expressions.

- Ilya

MikeArmstrong
5-Regular Member
(To:ivg)

Vladimir's examples work perfectly for the simple situation in my second post. But when I define a numerical value for 'b', they collapse into purely numerical expressions.

Like so?

Clipboard01.jpg

Mike

PhilipOakley
5-Regular Member
(To:MikeArmstrong)

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

LucMeekes
23-Emerald III
(To:PhilipOakley)

ivg,

With the explanation of Philip, the attached would be your worksheet.

Success!

Luc

Aside:

works with Chenestry notation as well:

tmp.gif

Luc's worksheet looks good.

Let me try to clarify Philip's statement of the general principle. The symbolic processor stops at the recursive definition and treats the name as unbound. The numeric processor continues on and finds the "hidden" numerical value. Right?

rag
1-Newbie
1-Newbie
(To:ivg)

Let me try to clarify Philip's statement of the general principle. The symbolic processor stops at the recursive definition and treats the name as unbound. The numeric processor continues on and finds the "hidden" numerical value. Right?

Not having access to the source code of Mathcad we don't know how it is really implemeted. Maybe the therory about the symbolic processor recognizing the recursive loop and stopping is correct.

For me it most of the time helped thinking that every variable has a numeric AND a (sometimes different) symbolic value which are assigned at the time/place of the assignment and is not changed until a next assignement occurs. This was discussed in another context in http://communities.ptc.com/message/175571#175571.

I am thinking of a:=a as simply being the Mathcad way of clearing the symbolic value of a variable while preserving the numerical value. BTW there seems to be no way of clearing the numerivcal value, making the variable undefined. Sometimes this would be convinient, e.g. to be able to use that variable in a quickplot or to tell MC that t should not be treated as the unit tons (which is unfortunately the case in some (all?) non-english versions of MC.

mzeftel
1-Newbie
(To:rag)

For Mathcad Prime 2.0, we added the functions clear(x,y,z) to clear numeric values of multiple variables and clearsym (x,y,z) to clear symbolic values.

clearsym replaces x:=x in Mathcad 15.

Mona

wayne
3-Visitor
(To:mzeftel)

Mona, thanks

What about matrices, same? If so, will it free up the memory too?

Top Tags