Skip to main content
1-Visitor
October 16, 2014
Solved

Why different solutions for same problem?

  • October 16, 2014
  • 2 replies
  • 1187 views

Guys, I have solved a simple equation in two different methods. Can you please tell what I am doing wrong in the 2nd method thast i am getting a different result?

Thank you

Best answer by Werner_E

The reason is the way how multiple successive if-statements followed by an otherwise are evaluated. Otherwise only triggers if ALL if's had failed. So your variable h never gets a value and is zero - therefore the wrong result.

A workaround can be to separate the if's by a dummy line:

1.png

From Mathcad's help:

  • Each conditional evaluation is considered on its own. When Mathcad encounters several sequential if statements at the same level, it evaluates each if statement in turn, regardless of the results of the previous statement. If you wish to evaluate an expression based on whether a particular if statement evaluates as true or false, use the otherwise operator. otherwise operator
  • If you use more than one if statement before an otherwise statement, the otherwise statement is executed only when all previous conditions are false. However, all previous if statements continue to be evaluated regardless of the results of the previous if. In the example above fy(5) = 9 and fy(2) = 1. There is no "else if" or "case" statement in Mathcad that allows you to switch on multiple cases, other than to use nested if ... otherwise pairs.

2 replies

Werner_E25-Diamond IAnswer
25-Diamond I
October 16, 2014

The reason is the way how multiple successive if-statements followed by an otherwise are evaluated. Otherwise only triggers if ALL if's had failed. So your variable h never gets a value and is zero - therefore the wrong result.

A workaround can be to separate the if's by a dummy line:

1.png

From Mathcad's help:

  • Each conditional evaluation is considered on its own. When Mathcad encounters several sequential if statements at the same level, it evaluates each if statement in turn, regardless of the results of the previous statement. If you wish to evaluate an expression based on whether a particular if statement evaluates as true or false, use the otherwise operator. otherwise operator
  • If you use more than one if statement before an otherwise statement, the otherwise statement is executed only when all previous conditions are false. However, all previous if statements continue to be evaluated regardless of the results of the previous if. In the example above fy(5) = 9 and fy(2) = 1. There is no "else if" or "case" statement in Mathcad that allows you to switch on multiple cases, other than to use nested if ... otherwise pairs.
nbitla1-VisitorAuthor
1-Visitor
October 17, 2014

Thank you so much

-nb