"Cases" function and for loop

"Cases" function and for loop - Messages

#1 Posted: 11/21/2023 12:06:52 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: 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 Posted: 11/21/2023 1:23:53 AM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: 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 Posted: 11/21/2023 3:19:42 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: 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 Posted: 11/21/2023 3:56:36 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

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

j.png

Best regards.
Alvaro.
#5 Posted: 11/21/2023 3:56:44 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: 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 Posted: 11/21/2023 4:13:42 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1674 posts.

Group: User

It seems that your office lacks adequate modern equipment.

coffe.png

Best regards.
Alvaro.
#7 Posted: 11/21/2023 4:17:08 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: 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 Posted: 11/21/2023 1:55:27 PM
overlord

overlord

547 likes in 1330 posts.

Group: Moderator

2023-11-21_085413.png
1 users liked this post
Oscar Campo 11/21/2023 6:29:00 PM
#9 Posted: 11/21/2023 8:59:56 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: 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 Posted: 11/21/2023 10:12:30 PM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: 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 users liked this post
Davide Carpi 11/23/2023 4:02:00 AM
#11 Posted: 11/21/2023 10:22:20 PM
overlord

overlord

547 likes in 1330 posts.

Group: 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 KiB) downloaded 27 time(s).

cases_simplified.png
1 users liked this post
Davide Carpi 11/23/2023 4:02:00 AM
#12 Posted: 11/22/2023 6:34:59 AM
overlord

overlord

547 likes in 1330 posts.

Group: 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 Posted: 11/22/2023 6:40:41 AM
Fikret Atalay

Fikret Atalay

2 likes in 24 posts.

Group: 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 Posted: 11/22/2023 7:39:57 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Attach the Smath document for doctoring !


Give your data set of points
That will be enough to fit
#15 Posted: 11/22/2023 10:53:08 PM
overlord

overlord

547 likes in 1330 posts.

Group: Moderator

Don't attach your smath document.
Issue is already solved, no need for demands.
1 users liked this post
Oscar Campo 11/23/2023 2:28:00 AM
#16 Posted: 11/23/2023 8:16:26 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: 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.

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