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

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Avoid "division by zero" doing sym math

niacopetti
1-Newbie

Avoid "division by zero" doing sym math

Hi,

first of all sorry for my poor English.

I've a little trouble with mathcad usage; for sure is a silly question but this is my very first approach to function programming.

i've three vector (not range variables) filled with symbolics value only in some position and zero value

in others.

This came because i've specified some value of interest of the vectors like for example :

vect_1(13):= v1+v2           (of course with right  mathcad syntax..here i can't write pedix - not literal pedix of course)

vect_1(38):=v1-v2           (in general each element is a function of v1 v2.. vn coordinates)

so mathcad create a vector "vect_1" filling the place at 13 and 38 with my desidered symbolic values ; set zero in other place.

With these 3 vector now i've to cycle and , for each element ,i  want to store in other new vector the result of this formula:

result(i) = 0.5 * vect_1(i)^2  * vect_2(i)  /  vect_3(i) ;

But because vect_3 is partially filled with zero, my for cycle stop obviously when the first "div by zero" is executed.

I try with if construct to skip zero element but the syntax :


if (   vect_3(i)  <> 0)   dosent' work, because i suppose mathcad can't valutate symbolic condition with logical boolean equal.



Please see the attached file; i've included all the code, but the error is in only last little block .


Thanks in advance.





11 REPLIES 11

Mathcad 15 has an "on error" programming feature that you might use. . .

But I have a question:

Why, oh why, are you using a very powerful and capable numeric solver with a fairly weak symbolic engine to do symbolic math??

Why aren't you doing this numerically?

Thanks for reply Fred,

sorry but i'm not sure how to use on error . I've tried like in this screenshot, simple adding "on error 0" to the line of formula.

hoping that in this way when div by zero appear 0 is stored instead of 0.5 * vSect(i)*Dl(i)^2 but it dosen't work.

Could you suggest the right usage ?

I've to prepare an exam ; in the writed test i'm forced to use symbolic calc  to resolve a structural dynamic problem;

after found a symb solution i've also to substitute numerical value to obtain numerical solution.

onERRor.jpg

Stuart showed you the correct syntax for "on error".

Sometimes red is just the numeric processor complaining that a variable hasn't bee defined; but the symbolic processor can carry on.

As Fred Said ...

Stuart

Sorry Stuart,

your code is very clear.

But when i write in sheet, Dl vector is red (undefined);

the formula after on error is all black , different from your screenshoot where is all red and Dl vector is blue (and not red).

As a result : U_tot variable is always zero.

It's clear i'm doing some big mistake but i don't understand.Screenshot 2016-06-20 18.01.19.png

Nicola Iacopetti wrote:

Sorry Stuart,

your code is very clear.

But when i write in sheet, Dl vector is red (undefined);

the formula after on error is all black , different from your screenshoot where is all red and Dl vector is blue (and not red).

As a result : U_tot variable is always zero.

It's clear i'm doing some big mistake but i don't understand.

I can't see at the moment why that should be, as all I did was modify one tiny part of your worksheet.  However, an alternative to using the on error statement is to simply use an if statement.    Life isn't simple - even this has problems.

Unfortunately, I am now Mathcadless until tomorrow so can't look at it any further.

Stuart

Thanks for your honest effort Stuart, you seems  very "in depth" with software mathcad logic.

I confirm that , at least with my version of mathcad ( Mathcad 14.0 M020 (14.0.2.5 [802141434]))

your first code (that as you said it's only a little mod to my) dosen't work ( U_tot.. var is always zero).

Seems curious that even without divide by vLen, i can't retrieve the sum (symbolic) of element :

Screenshot 2016-06-20 22.04.47.png

If i return n_eff_cycle then Us->802   (seems ok 'cause last element is truss 813)

but if i return sum then Us-> 0 and not the sum of the (symbolic) element ->  EA2 +3* EA3  terms.

Instead your last code (vLen/L) seems work perfectly and, at least for this simple case, i check the sum result ( of this 4 terms) and it seems equal to the "manual sum of each term".

Screenshot 2016-06-20 22.03.04.png

So it's for sure curious why your prev code output a not equal result.

I've check also if the trouble is the use of reserved letter "L" but ,substistuing with generic "a" , the code work only if vLen is divided by "a" ( again as like as "L" case).

Sorry but i can't add  any suggest to resolve the mistery; i've done a lot of various attempt (and debug print) but i can't see a logic behind the strange behaviour.

I hope your code is correct in all cases, it's not so simple to make test case for a full debug.

Thanks a lot again, i hope in your some other answer.

RichardJ
19-Tanzanite
(To:StuartBruff)

"Interestingly, the result is different ... not sure why".

Because your version using "on error" is wrong

Good eye Richard, thanks for signaling.

It's strange anyway i can't obtain the sum :

Screenshot 2016-06-20 23.03.28.png

RichardJ
19-Tanzanite
(To:niacopetti)

on the RHS of on error you have no assignment operator. It should be sum<--sum+vSec[i

StuartBruff
23-Emerald II
(To:RichardJ)

Richard Jackson wrote:

"Interestingly, the result is different ... not sure why".

Because your version using "on error" is wrong

Doh!  And this month's recipient of the Homer Award is .....

What I meant to write was

Stuart

Top Tags