"Cases" function and for loop

"Cases" function and for loop - 消息

#1 已发布: 2023/11/21 00:06:52
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

Hi all, new SMath user here (transitioning from Mathcad). I am trying to recreate some of my old Mathcad sheets and have run into an issue for a scenario involving a for loop and multiple if statements. Hopefully this image works:



Any ideas as to what might be going on?

Thanks,
FT
#2 已发布: 2023/11/21 01:23:53
Davide Carpi

Davide Carpi

1,443个赞,共2,936个帖子。

组: Moderator

Hello [userlink]gth802s[/userlink]

Definitions inside cases() arguments should be avoided.

Wrote

I have to point out once again that in general you shouldn't put definitions inside cases().
Unlike if(), when a condition or a case fails instead of throwing an error the whole function is returned like if doesn't exists. The consequence is that every argument is exposed and preprocessed, with potential unwanted behaviors respect to the expected logic. This is on the edge of SMath possibilities, but is what makes possible to use cases() whereas if() usually fails.



In your case it should look like in the screenshot below:

2023-11-20 18_19_33-SMath Solver - [Worksheet1_].png


Notice that I initialize a local variable to avoid unintentional inheritance of existing vectors (if you call it twice and the second time h has less values than the first time, the second s.c_ctr will be the first vector with the initial values updated)





If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#3 已发布: 2023/11/21 03:19:42
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

Thanks for the responses so far. I think my problem is that the Boolean is somehow incomplete. I ran the iterations manually and the for loop works fine until j=4, but then the conditions change at j=5 saying that the variable is not defined. But I am not quite sure what the missing condition is; I guess Mathcad is more tolerant of this?

Thanks again.
#4 已发布: 2023/11/21 03:56:36
Alvaro

Alvaro

1,139个赞,共1,805个帖子。

组: User

Hi. In the last expression before the otherwise, isn't a subscript j missing?

j.png

Best regards.
Alvaro.
#5 已发布: 2023/11/21 03:56:44
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

I figured it out; it was a simple case of a missing subscript (as Alvaro pointed out) and parentheses in the wrong place... Should have stepped away from the computer sooner for that coffee; it was pretty clear what I had done wrong when I came back to my desk :-)

Thanks all; appreciate the quick responses!
#6 已发布: 2023/11/21 04:13:42
Alvaro

Alvaro

1,139个赞,共1,805个帖子。

组: User

It seems that your office lacks adequate modern equipment.

coffe.png

Best regards.
Alvaro.
#7 已发布: 2023/11/21 04:17:08
Jean Giraud

Jean Giraud

983个赞,共6,866个帖子。

组: User

Wrote

Thanks for the responses so far. I think my problem is that the Boolean is somehow incomplete.

.
Attach the Smath document for doctoring !

#8 已发布: 2023/11/21 13:55:27
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

2023-11-21_085413.png
1位用户喜欢此帖子
Oscar Campo 2023/11/21 18:29:00
#9 已发布: 2023/11/21 20:59:56
Davide Carpi

Davide Carpi

1,443个赞,共2,936个帖子。

组: Moderator

There is room for improvements in the conditions management.

Since the first case catches all the conditions where OCR[j] <= 1, the 2nd and third "(OCR[j] > 1) AND" checks are redundant.

Also, since Sigma'vf_ctr[j] <= pc[j] case is captured in the second condition, the third case can be written in the otherwise placeholder.

2023-11-21 13_53_09-bari.docx - Word.png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#10 已发布: 2023/11/21 22:12:30
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

Good catch :-) Here is the conceptual model for the soil consolidation problem which is what this sheet is written to solve. When the final stress exceeds p_c, the soil undergoes additional settlement in the second region whose slope is denoted by C_c. The reason I coded it like that is it makes it easier for me to explain to students that it is an increment of stress (beyond the pressure denoted by p_c) that causes soil settlement in that region.



This is also the reason why I try to code things as explicitly as possible (including in the if statements) so that the students can follow along easier.

Really appreciate all the thoughtful responses from everyone; loving the SMath community already!

Wrote

2023-11-21_085413.png

1位用户喜欢此帖子
Davide Carpi 2023/11/23 04:02:00
#11 已发布: 2023/11/21 22:22:20
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

Wrote

There is room for improvements in the conditions management.


If I didn't made a mistake, this is most simplified I could get.
Here is the file contains logarithmic simplification steps.

Regards

cases_simplified.sm (35.71 KiB)已下载 1483 次。

cases_simplified.png
1位用户喜欢此帖子
Davide Carpi 2023/11/23 04:02:00
#12 已发布: 2023/11/22 06:34:59
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

Wrote

... it makes it easier for me to explain to students ...


Wait, are you a teacher too?
How is everyone in this forum a teacher, academic, professor?
Am I the only simpleton here?
#13 已发布: 2023/11/22 06:40:41
Fikret Atalay

Fikret Atalay

2个赞,共24个帖子。

组: User

I am a practicing engineer with a PhD but I also teach a graduate level course at the local university (Georgia Institute of Technology). I am also living proof that having a PhD or being a professor doesn't make one any better/smarter than someone who is not an academic :d

Wrote

Wrote

... it makes it easier for me to explain to students ...


Wait, are you a teacher too?
How is everyone in this forum a teacher, academic, professor?
Am I the only simpleton here?



#14 已发布: 2023/11/22 19:39:57
Jean Giraud

Jean Giraud

983个赞,共6,866个帖子。

组: User

Wrote

Attach the Smath document for doctoring !


Give your data set of points
That will be enough to fit
#15 已发布: 2023/11/22 22:53:08
overlord

overlord

600个赞,共1,381个帖子。

组: Moderator

Don't attach your smath document.
Issue is already solved, no need for demands.
1位用户喜欢此帖子
Oscar Campo 2023/11/23 02:28:00
#16 已发布: 2023/11/23 20:16:26
Jean Giraud

Jean Giraud

983个赞,共6,866个帖子。

组: User

Wrote

Give your data set of points
That will be enough to fit


Your log fit comes from Cat in the Hat.
A fitting session might be otherwise realistic,
while being more universal wrt to data collection.
As well more profitable for Students.

  • 有新帖
  • 无新帖