Help with 'for' loops

Help with 'for' loops - Loading matrices with numbers using for loops - mathcad/smath - Messages

#1 Posted: 9/26/2012 4:26:07 PM
Laurence Weekes

Laurence Weekes

0 likes in 9 posts.

Group: User

Hi All

Hopefully a simple problem for someone clever to sort out.I have used mathcad for a while and I am now teaching Smath to my students on structural engineering courses at University. I have programmed a mathcad sheet which successfully solves any joint loaded pin-jointed framework using the stiffness method. However not all my students want to purchase mathcad and Smath is the obvious (and excellent) alternative. But I am still trying to get my head around some of the syntax issues.

So I am trying to sort out a similar stiffness procedure on Smath, and everything was fine until I reach the transformation matrices. Hopefully if I have attached the files correctly there should be a pdf of the mathcad sheet which shows how I have constructed the transformation matrices (sect 2.4). I have also attached my smath file which has reached this point then failed

For the frame I have used, there should be 5 transformation matrices - smath only outputs the last one (I was expecting 't' to be a column matrix of 2x8 matrices). There are two for loops, it appears the inner 'z' loop is ok but the outer 'j' loop isnt registering anything before the last number in the range. I am guessing my syntax is arwy, but I hope it is clear what I am trying to achieve?

Any help would be gratefully received - in the meantime I'll try to come at it from a different approach.

Cheers!

Laurence



stiffness.sm (22 KiB) downloaded 64 time(s).
#2 Posted: 9/27/2012 11:09:33 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Hi Laurence

I made something similar for my FEA classes. It could be done much simpler, but the target was to demonstrate how the global system is assembled and reduced based on constraints.
I found the det function unreliable. Thus I was not able to demonstrate the singularity of the unconstrained system and the regularity of the reduced one. You need to apply scaling which need to be adjusted to a very narrow range where the det function will work at all.

The sheet is in german but the code should be self documenting.

Best regards, Martin Kraska
Fachwerk HA1.sm (70 KiB) downloaded 65 time(s).
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#3 Posted: 9/27/2012 4:25:17 PM
Ioan

Ioan

1 likes in 26 posts.

Group: User

Hi Laurence,


Here attached is another way to solve your problem.
Hoping to be useful and hoping that my way is enough readable.
I do not use the penalty method for supports; equations are written only for the active DOFs.


Best regards,


Ioan

artic3dr.sm (72 KiB) downloaded 78 time(s).
#4 Posted: 9/27/2012 5:37:42 PM
Laurence Weekes

Laurence Weekes

0 likes in 9 posts.

Group: User

Hi Martin and Ioan

Very many thanks for your replies, the sheets are very informative and given me some great ideas for solving this - I can see how you have used the 'for' loops to achieve the transformations. (Also the graphics are a very useful addition, that's the next thing for me to implement now!).

From the teaching perspective, I tend to teach the hand calcs with reduced DOF's but wanted to show the students the logistics of programming the method in a general way, i.e keep all DOF's to show the singular global matrix then how to apply boundaries to the system to remove the singularity. Anyway, still not sure why my for loops don't work, but I won't lose too much sleep, I'll use your methods.

Thanks once again!

Laurence
#5 Posted: 9/29/2012 6:43:15 PM
Laurence Weekes

Laurence Weekes

0 likes in 9 posts.

Group: User

Hi again, new problme this time

So I have my transfrmation matrices sorted now, gone through the process of inverting the matrix, multiplying by the load vector to work out displacements. But now I have encountered a different problem, one which might be a bug?

Hopefully the attached sheet will explain my problem. When I evaluate (K^-1)*(P), the displacements are evaluated as expected - correct answer. However when I let a variable equal this, then evaluate the variable the answers are wrong.

So I have been playing with the numbers in the matrix and it appears that when I put all the small exponential numbers to zero then the variable assignment works. But why should the direct evaluation work and not the variable assignment in the first place. It strikes me that something in the maths during variable assignment is intrinsically different. I can understand how very small or large numbers could affect the calculations, but why does it work for one and not the other. Im quite confused now.

Any ideas? Is this an smath bug or am I doing something stupid?

Many thanks

Laurence


Check.sm (23 KiB) downloaded 47 time(s).
#6 Posted: 9/29/2012 7:53:31 PM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Hi Laurence

evaluation of expressions is one of the biggest mysteries of SMath. I have never seen (or better: understood) a consistent description of what the interaction of numeric and symbolic evaluation is meant to work.

In your case the assignment to the variable disp delays the evaluation to the point where disp is going to be displayed. Immediate evaluation of [MATH]KG^{-1}*P[/MATH] works but delayed does not (Why?). You can enforce immediate evaluation upon assignment to disp by using the function eval():

[MATH]disp:eval(KG^{-1}*P)[/MATH]

results in

[MATH lang=eng]disp=mat(0.183,-0.527,-0.078,-0.41,1*10^{-16},-3.91*10^{-17},-1*10^{-16},-6.09*10^{-17},8,1)[/MATH]

(mm unit refused to be displayed in the post preview, therefore I removed it from the result)

Equally well you can generate the precision problem by switching to symbolic optimization in the immediate evaluation of [MATH]KG^{-1}*P[/MATH]. You get something ugly like:

[MATH lang=eng]KG^{-1}*P=mat(-154029677706911/1227751447183550,371846259893873/1635302861611220,150994944000000/282161279734733,395987199384982/508161646518145,-1949477283233400/115154739031931000000000000000,71907646210181/9860498487058500000000000000,-1453125/2091995697675820000000,185335047104285/8598781694818370000000000000000,8,1)[/MATH]

Want to know what the decimal representation of this is? Do not hope for just switching back to numeric optimization. Instead copy the result and evaluate that numerically:

[MATH lang=eng]mat(-154029677706911/1227751447183550,371846259893873/1635302861611220,150994944000000/282161279734733,395987199384982/508161646518145,-1949477283233400/115154739031931000000000000000,71907646210181/9860498487058500000000000000,-1453125/2091995697675820000000,185335047104285/8598781694818370000000000000000,8,1)=mat(-0.13,0.23,0.54,0.78,-1.69*10^{-14},7.29*10^{-15},-6.95*10^{-16},2.16*10^{-17},8,1)@#[/MATH]

This is the garbage result that you had to face when displaying the variable disp.

Just to emphasize the problem: numeric evaluation of exactly the same expression results in

[MATH lang=eng]KG^{-1}*P=mat(0.183,-0.527,-0.078,-0.41,1*10^{-16},-3.91*10^{-17},-1*10^{-16},-6.09*10^{-17},8,1)[/MATH]

Seems like a severe bug in the symbolic engine. This makes me recommend to my students not to use symbolic features at all. However, than you would have to embrace every expression with eval() before assignment. This looks really ugly in otherwise nice smath sheets. Still the general rule seems to apply: The more questionable the results are, the more evals you should spread over your sheet.

Once I made the proposal to introduce different assignment operators for delayed and immediate evaluation, say :<- and := respectively.
Also, symbolic and numeric evaluation (aka "optimization" ) should be visually distinct, say = and -> (as in Mathcad)
You may have noted that many examples and bug reports have expressions with comments like "symbolic" just to indicate what could be visible from a consistent notation. However, this seems to be a matter of taste or not easy to implement.

It seems to me that the default assignment type is symbolic/delayed. How about switching the default to immediate evaluation? Half the traffic in the forum could be saved. Those who want trouble really hard, could perhaps be given a function symb() ;-)

Best regards, Martin Kraska
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#7 Posted: 9/30/2012 2:50:57 AM
Ioan

Ioan

1 likes in 26 posts.

Group: User

Hi Laurence,


I fully agree Martin's opinions.

For simplifying - if your switch disp expression from symbolical optimisation to numerical optimisation you have the right result (see attached file) with good units also.

I've seen also that often the symbolical optimisation give false numerical results.


Best regards,


Ioan
Check mid.sm (70 KiB) downloaded 64 time(s).
#8 Posted: 9/30/2012 3:56:06 AM
Martin Kraska

Martin Kraska

1222 likes in 2150 posts.

Group: Moderator

Hi Ioan,

thank you for pointing out the straight solution. Indeed this works without using eval. I guess you need eval if such assignments are done within procedure definitions where there is no other way to control optimization on a statement by statement base.

Would be interesting to know Andrey's priority level of this problem. I guess that debugging the symbolic engine is far from being fun. My hope is that there is at least a chance to have the visual markup of symbolic/numeric optimization resolved. Otherwise one cannot trust printed SMath sheets, as Laurence's example again demonstrated. I even would ask for a global preference setting to numeric evaluation.

Best regards, Martin Kraska
Martin Kraska Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 users liked this post
Radovan Omorjan 9/30/2012 7:10:00 AM
#9 Posted: 9/30/2012 3:59:26 PM
Laurence Weekes

Laurence Weekes

0 likes in 9 posts.

Group: User

Hi Ioan and Martin

Many thanks for your impressive replies, its clear to me what is happening now. It seems my days with Mathcad have been charmed, looks like I have quite a lot to learn with Smath still. Great that it works now though!

All the best

Laurence

(PS, been trying to press the 'thank' button on your posts but it's coming back with a web error at the moment - hopefully it will work shortly)

  • New Posts New Posts
  • No New Posts No New Posts