Trouble with using the command WhenEvent in Mathematica

In summary, the use of the WhenEvent command in Mathematica can lead to challenges, particularly regarding its event handling and timing aspects. Users often encounter difficulties with event triggers not functioning as expected, which may stem from the complexity of defining conditions or conflicts with other functions. Proper understanding of the event structure and careful implementation are essential for effective usage.
  • #1
Youssi
6
2
TL;DR Summary
Mathematic, error in using the command WhenEvent in Mathematica

Reference: https://www.physicsforums.com/forums/matlab-maple-mathematica-latex.189/post-thread
Hello, I'm trying to solve a heat differential equation to obtain the temperature distribution as a function of R. Since I have the coefficients of electrical and thermal conductivity that start from the temperature of 6500 K, I want the solution to stop when T reaches 6500. I'm using the command:

Code:
 WhenEvent[T[r] >= 6500, {RlimiteValue = r, "StopIntegration"}].

However, I'm encountering the following errors:

Code:
`Plot::plln: Limiting value RlimiteValue in {r, 1/10000000000, RlimiteValue} is not
a machine-sized real number.

Can someone help me resolve this issue?

Thank you in advance for your assistance

The program is listed below :
Code:
ClearAll["Global`*"]

(*Importation des coéfficient non linéaire : conductivité thermique et conductivité thermique*)

Lambda=Import["C:\\Users\\yousra\\Documents\\thèse\\Modélisation\\1_mathematica\\condu_therm_rer.xlsx"][[1]];
Sigma=Import["C:\\Users\\yousra\\Documents\\thèse\\Modélisation\\1_mathematica\\condu_elec.xlsx"][[1]];

(*Interpolation et création des fonctions de Lamdba et sigma*)

(*Lambda*)

λInterp=Interpolation[Lambda,InterpolationOrder->1];

(*Définir la fonction λ(t)*)

Clear[λ];
λ[t_]:=λInterp[t];

(*Vérifier la fonction λ(t) pour quelques valeurs de t*)

λ[3000];
λ[700];
λ[12000];

(*sigma*)

σInterp=Interpolation[Sigma,InterpolationOrder->1];

(*Définir la fonction σ(t)*)

Clear[σ];
σ[t_]:=σInterp[t];
σ[3000];

(*Dessin de lambda et sigma interpolé*)

(*Tracer la fonction λ(t)*)

Plot[λ[t],{t,Min[Lambda],Max[Lambda]},AxesLabel->{"t","λ(t)"},PlotLabel->"Plot of λ(t)"]

(*Tracer la fonction σ(t)*)

Plot[σ[t],{t,Min[Sigma],Max[Sigma]},AxesLabel->{"t","σ(t)"},PlotLabel->"Plot of σ(t)"]

(*l'équation de chaleur*)

Equ1=Div[λ[T[r]]*Grad[T[r],{r,θ,z},"Cylindrical"],{r,θ,z},"Cylindrical"]+σ[T[r]]*F^2==0//FullSimplify
F=6000;
Tmax=29000;

(*sol=NDSolve[{Equ1,T'[0]==0,T[0]==10000,T[10^-2]==6000},T,{r,0,10^-2}][[1]];
Plot[T[r]/. sol,{r,0,10^-2},AxesLabel->{"r","T(r)"},PlotLabel->"Graphique de T en fonction de r"]*)

(*Define the solution*)
(*Define the solution*)
(*Define the solution*)

sol=NDSolveValue[{Div[λ[T[r]]*Grad[T[r],{r,θ,z},"Cylindrical"],{r,θ,z},"Cylindrical"]+σ[T[r]]*F^2==0,
    T'[10^-10]==0,
    T[10^-10]==Tmax,
    WhenEvent[T[r]>=6500,{RlimiteValue=r,"StopIntegration"}]},
    T,{r,10^-10,10^-2}];

(*Print the result*)

Print["Rlimite corresponding to T >= 6500: ",RlimiteValue]

(*Plot the solution up to the RlimiteValue*)

Plot[sol[r], 
    {r, 10^-10, RlimiteValue}, 
    AxesLabel -> {"r", "T[r]"},
    PlotLabel -> "Solution up to RlimiteValue"]

Plot[sol[r],
    {r,10^-10,RlimiteValue},
    AxesLabel->{"r","T[r]"},
    PlotLabel->"Solution up to RlimiteValue"]
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I'm not familiar with Mathematica and until someone who is responds, I thought I'd provide something to help you along:
The error you're encountering seems to be related to the fact that RlimiteValue is not being assigned a numerical value before it's used in the plotting function. WhenEvent stores the value of r where the condition T[r]>=6500 is met in RlimiteValue, but if the condition is never met, RlimiteValue remains undefined and cannot be used as a plot limit.

Also check on the Mathematica website using your error message as a search term to see what pops up. I got the following:

http://forums.wolfram.com/mathgroup/archive/1997/Oct/msg00331.html

and

https://mathematica.stackexchange.com/questions/170024/is-not-a-machine-sized-real-number
 
Last edited:
  • Like
Likes Youssi
  • #3
Thank you very much! I simply provided an initial value for RlimiteValue, and it worked.
 
  • Like
Likes jedishrfu

FAQ: Trouble with using the command WhenEvent in Mathematica

Why isn't WhenEvent triggering at the expected time?

WhenEvent may not trigger at the expected time if the event condition is not formulated correctly or if the numerical solver does not detect the event due to insufficient precision. Ensure that the condition is well-defined and consider adjusting the precision and accuracy goals in your NDSolve function.

How can I handle multiple events using WhenEvent?

To handle multiple events, you can pass a list of event-action pairs to WhenEvent. Each pair consists of a condition and an action to be executed when the condition is met. This allows you to manage multiple events within a single NDSolve call.

What types of actions can I specify in WhenEvent?

In WhenEvent, you can specify various types of actions, including setting variables, stopping the integration, logging events, or any other valid Mathematica expression. Common actions include variable assignments, Print statements, and the StopIntegration command.

How do I debug WhenEvent if it's not working as expected?

To debug WhenEvent, you can use Print statements within the event actions to monitor the values of variables and check if the conditions are being met. Additionally, you can plot the variables involved to visualize their behavior and ensure the event conditions are correctly formulated.

Can WhenEvent be used with symbolic computations?

WhenEvent is primarily designed for use with numerical solvers like NDSolve. It is not typically used for symbolic computations, as the event detection mechanism relies on numerical evaluation. For symbolic event handling, consider using alternative methods such as conditional expressions or piecewise functions.

Similar threads

Replies
1
Views
1K
Replies
1
Views
2K
Back
Top