"Cases" function and for loop

"Cases" function and for loop - Сообщения

#1 Опубликовано: 21.11.2023 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 Опубликовано: 21.11.2023 01:23:53
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: 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 Опубликовано: 21.11.2023 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 Опубликовано: 21.11.2023 03:56:36
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: User

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

j.png

Best regards.
Alvaro.
#5 Опубликовано: 21.11.2023 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 Опубликовано: 21.11.2023 04:13:42
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: User

It seems that your office lacks adequate modern equipment.

coffe.png

Best regards.
Alvaro.
#7 Опубликовано: 21.11.2023 04:17:08
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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 Опубликовано: 21.11.2023 13:55:27
overlord

overlord

547 сообщений из 1330 понравились пользователям.

Группа: Moderator

2023-11-21_085413.png
1 пользователям понравился этот пост
Oscar Campo 21.11.2023 18:29:00
#9 Опубликовано: 21.11.2023 20:59:56
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: 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 Опубликовано: 21.11.2023 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 23.11.2023 04:02:00
#11 Опубликовано: 21.11.2023 22:22:20
overlord

overlord

547 сообщений из 1330 понравились пользователям.

Группа: 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 КиБ) скачан 27 раз(а).

cases_simplified.png
1 пользователям понравился этот пост
Davide Carpi 23.11.2023 04:02:00
#12 Опубликовано: 22.11.2023 06:34:59
overlord

overlord

547 сообщений из 1330 понравились пользователям.

Группа: 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 Опубликовано: 22.11.2023 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 Опубликовано: 22.11.2023 19:39:57
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

Attach the Smath document for doctoring !


Give your data set of points
That will be enough to fit
#15 Опубликовано: 22.11.2023 22:53:08
overlord

overlord

547 сообщений из 1330 понравились пользователям.

Группа: Moderator

Don't attach your smath document.
Issue is already solved, no need for demands.
1 пользователям понравился этот пост
Oscar Campo 23.11.2023 02:28:00
#16 Опубликовано: 23.11.2023 20:16:26
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: 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.

  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений