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

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

Why different solutions for same problem?

nbitla
1-Newbie

Why different solutions for same problem?

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

1 ACCEPTED SOLUTION

Accepted Solutions
Werner_E
24-Ruby V
(To:nbitla)

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.

View solution in original post

2 REPLIES 2
Werner_E
24-Ruby V
(To:nbitla)

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.
nbitla
1-Newbie
(To:nbitla)

Thank you so much

-nb

Top Tags