Community Tip - You can change your system assigned username to something more personal in your community settings. X
I am trying to define a piecewise function in Mathcad to move around singularities, but the program breaks down at the singularities even when they are not called.
I created a program that evaluates one function if "w" is greater than or equal to "r", and another function if "w" is less than "r". The second function has a singularity if "w" is equal to "r" but theoretically it shouldn't need to evaluate that function in that case, as "w" isn't less than "r". The program still returns an error in that case though.
Is there a way to set up the program such that MathCad will not try to evaluate the term in the false if statement
Example:
function = if w >= r
pi/2
if w < r
atan(1/(w-r))
Solved! Go to Solution.
I second what Luc wrote about the worksheet. It should be possible in most cases to narrow down the problem and create and post a worksheet which shows what you are looking after and what fails.
In case of your problem this should be fairly simple! Its still unclear for example if w and r are defined on top of your sheet or if they are provided as function arguments. And you always talk about functions but you don't show any function. So its unclear if you are just sloppy when typing in here the part of the expression or if you misunderstood the meaning of a function in math and confuse it with "term" or "expression". When you write
F2=atan(1/(w-r))
thats not a function but rather the definition of a new variable F2 which Prime tries to evaluate no matter what the values of w an r are. Of course, when the variables w and r are defined above with the same value, this calculation obviously must fail.
If you really insist in splitting this simple and straightforward calculation into a series of single calculations, you may do it with functions like this:
Note that if w and r are defined above, fun must not necessarily be a function but may be a simple variable. F2 has to be a function which is only called when w and r are different.
And here are different approaches without any functions:
You should attach your worksheet, not just a description what you had done. Its hard to determine what you may have done wrong just from the description. Furthermore you should state which version and which flavor of Mathcad (Prime or real Mathcad) you are using.
You may consider using the "otherwise" (= "else") statement instead of a second "if".
You may also use the if-function (similar syntax as in Excel) instead of the if-programming-statement.
BTW, while I would not suggest doing so, but it works OK even the way you describes it:
Unfortunately, I cannot post the worksheet due to security restrictions on the project. However, based on your feedback, I have narrowed the problem down further.
The program does execute if I type the entire formula into the program if statement. However, the worksheet is set up where I define a bunch of separate functions that the program selects from. In that case, the fact that the earlier function is undefined causes the program to crash.
Further clarification
F1 = pi/2
F2 = atan(1/w-r)
Function = if w>=r
F1
if w<r
F2
Program crashes at w=r in that case ( it also crashes if I set it up with an if-function or an else statement). This does give me a workaround if I directly type the function into the program, but the presentation is better if I can nest the functions, so I would like to still do that if possible.
Sadly, I no longer have access to real mathcad and am using Mathcad Prime 4.0
1. You don't need to submit your entire sheet. If you are allowed to publicise your construct in a clumsy textual way, you could also put that part in a (separate) worksheet and attach that, so: you should attach a (just a) sheet that describes your problem as concise as possible. (Yes, a picture may paint a thousand words, but a worksheet is superior to a picture!)
2. If properly formulated, the if statement WILL do the trick, as shown here
with w below, equal to and above r. No crash.
Success!
Luc
I second what Luc wrote about the worksheet. It should be possible in most cases to narrow down the problem and create and post a worksheet which shows what you are looking after and what fails.
In case of your problem this should be fairly simple! Its still unclear for example if w and r are defined on top of your sheet or if they are provided as function arguments. And you always talk about functions but you don't show any function. So its unclear if you are just sloppy when typing in here the part of the expression or if you misunderstood the meaning of a function in math and confuse it with "term" or "expression". When you write
F2=atan(1/(w-r))
thats not a function but rather the definition of a new variable F2 which Prime tries to evaluate no matter what the values of w an r are. Of course, when the variables w and r are defined above with the same value, this calculation obviously must fail.
If you really insist in splitting this simple and straightforward calculation into a series of single calculations, you may do it with functions like this:
Note that if w and r are defined above, fun must not necessarily be a function but may be a simple variable. F2 has to be a function which is only called when w and r are different.
And here are different approaches without any functions:
Apologies. I am new to asking for support, and forogt the importance of being extremely explicit with my terminology. I was using "function" in the general sense, and not the MathCad one. Keeping the "functions" as actually functions (dependent on the input variables) in the prgram has eliminated the crashing problem. I had to move a few lines around so the worksheet still flows logically, but it's producing the needed results now. Thank you very much for your assistance
@seandonahue wrote:
Sadly, I no longer have access to real mathcad and am using Mathcad Prime 4.0
If you have Prime 4.0, the full version that will allow programming, then you have a license. That license entitles you to a functioning copy of Mathcad 15.
Without a license you have Prime 4.0 Express, and much of what's been shown here you cannot use. The only if statement available to you is the "EXCEL" if statement.