As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [conditional]

Questions about programming or mathematical constructs that enable verification, selection, or branching behavior when one or more criteria are determined to be True.

Filter by
Sorted by
Tagged with
3 votes
1 answer
248 views

How can I use WhenEvent with multiple conditions inside NDSolve?

I would like to use WhenEvent but I'm not getting what I want: I set the initial condition var\epsilon=0; I want var\epsilon to change continuously over time, from to 0.1 if s'[t]>=0 or to -0.1 if s'[...
Mariana Sá's user avatar
1 vote
2 answers
143 views

How do I loop my "If" statement?

I have list given by: d= {{{1,2},{1,1},{2,3}}, {{1,4},{3,3},{2,1}}, {{1,1},{3,1},{3,3}}} Each row in the list is a different generation of the ...
D'Angelo's user avatar
  • 433
1 vote
0 answers
41 views

Meaning of a condition $\left(\Re\left(m^2\right)\geq 0\lor m^2\notin \mathbb{R}\right)$ [closed]

I'm new to using Mathematica. I asked Mathematica to perform the following integration, ...
fogof mylife's user avatar
3 votes
3 answers
372 views

count conditional items in list

I have a list of the form: {1,1, (1 if x>y, -1 if y>=x), -1} I want to count the number of 1's in the list. The output I'm looking for is something of the ...
naomig's user avatar
  • 343
0 votes
0 answers
63 views

Integrals with conditional result

I am trying to get Mathematica to evaluate integrals such as the well-known Fourier orthogonality relations $\int_0^L \sin \frac{2 m \pi x}{L} \sin\frac{2 n \pi x}{L}\,\mathrm{d}x=\begin{cases}0& ...
avid's user avatar
  • 101
3 votes
2 answers
214 views

How to keep Switch default case as original function?

For example of below Switch function. In[]:= Module[{f}, f[x_] := Switch[x, 1, 2, 3, 4]; {f[1], f[3], f[x]} ] Out[]= {2, 4, Switch[x, 1, 2, 3, 4]} ...
liang's user avatar
  • 589
3 votes
2 answers
102 views

Conditional statement in function

It seems the equality condition does not work as expected. ...
Smilia's user avatar
  • 496
2 votes
2 answers
112 views

Vanishing function and Manipulate

Within Manipulate, I want to vanish certain controls if other controls satisfy a specific condition. The simplest case is to consider two checkboxes, ...
sam wolfe's user avatar
  • 4,233
0 votes
1 answer
147 views

Is using an If[] condition faster than KronckerDelta[]?

I have a code which has a term in a summation like: KroneckerDelta[M,0]*func[M] where func[M] is a time consuming numerical ...
odomosis's user avatar
1 vote
1 answer
59 views

Plot when multiple conditions are satified [closed]

I have a code which goes like this: ...
sreeraj t's user avatar
  • 193
2 votes
3 answers
246 views

Specifying condition with NIntegrate

I have an integral $$ P_{\frac12 + \mathrm i\mu}(\eta) = \frac{\sqrt{2}}{\pi} \cosh(\pi\mu) \int_{0}^{\infty} \frac{\cos(\mu\tau)}{\sqrt{\cosh\tau + \eta}} \mathrm d\tau, \quad \eta \geq 1, \quad \mu ...
rami_salazar's user avatar
4 votes
1 answer
196 views

Why does D ignore conditions specified on a custom function?

I define a function with a condition like so. ClearAll[foo]; foo[expr_] /; FreeQ[expr, bar] := expr Such that foo[12] returns ...
Sam's user avatar
  • 185
5 votes
3 answers
229 views

Among the first 10,000 multiples of 17

Hello I'm new with Mathematica and I can't find a way where I can do this condition. Among the first 10,000 multiples of 17 how many have the sum of their digit multiple of 17? The part for the ...
alan delachair's user avatar
1 vote
0 answers
38 views

Are the conditions in ConditionalExpression necessary or sufficient?

I computed a limit and obtained it in the form of a ConditionalExpression (see below). Substituting the variables with numerical values in the resulting formula ...
Lucas Alanis's user avatar
1 vote
0 answers
130 views

What does a single vertical bar mean in ConditionalExpression? [closed]

I got the condition below as part of a ConditionalExpression. Those single vertical bars before and after $\frac{1}{k1}$ appear to be an absolute value but I don't ...
Lucas Alanis's user avatar
1 vote
1 answer
162 views

Defining a conditional statement for a function

I am trying to define a function that is zero when it is evaluated on real numbers and on a preset list of symbols. I am doing this by using ...
physics_researcher's user avatar
4 votes
1 answer
107 views

Use Lookup on associations that contain conditional statements

I am using conditional statements in a list of rules to replace values in a vector of data. Since the same operation can be performed faster using Lookup on associations I would like to use this ...
Joe W.'s user avatar
  • 41
1 vote
1 answer
3k views

Multiple conditions in If function

I wrote a function which returns 1 if x^2 + y^2<=1: f[x_, y_] := If[x^2 + y^2 <= 1, 1] Now I'm trying to continue this function to take in multiple ...
Ray's user avatar
  • 155
5 votes
4 answers
330 views

Recursive function, if and which statements

I am given this question: Let a function $ T_n(x) $ be defined by: $$ T_{n+1}(x) = \frac{1}{x} T_{n-1}(x) - \frac{2}{7} T_n(x), $$ where $ T_0(x) = 1, T_1(x) = x $. I need to construct a recursive ...
12345's user avatar
  • 199
1 vote
1 answer
54 views

WhenEvent when using NDSolve doesn't work properly

...
Gabriel Trigo's user avatar
2 votes
2 answers
124 views

Automate table to display figures

With reference to the post Automate Poisson Football Scores Prediction, I succeded in defining the Poisson probability density function for home (μh=A) and away (<...
Nate's user avatar
  • 401
2 votes
1 answer
151 views

How to use conditional clause inside NDSolve

I am trying to solve a differential equation that changes its behaviour depending on the value of the dependent variable. I've tried as shown below, but the computer does not understand my If clause ...
Gabriel Trigo's user avatar
3 votes
3 answers
192 views

Is there a way to speed up the following calculation Table[If[TrueQ[],k,Nothing],{k,10^5,10^7}]

I've the following code: Table[If[TrueQ[2+3^k>2^k+(-1+2^k)*Floor[(3/2)^k]],k,Nothing],{k,10^5,10^7}] Is there a way to speed the calculation up in Mathematica ...
Jan Eerland's user avatar
  • 1,941
2 votes
1 answer
65 views

Aligning Conditional Controls in Manipulate

Consider the following code ...
sam wolfe's user avatar
  • 4,233
16 votes
0 answers
197 views

Reduce[Sin[x]==0,x] splitting odd and even multiples of Pi

as from Title, I'm wondering why Mathematica does not automatically give a single, simple solution for the problem Reduce[Sin[x] == 0, x] for which I obtain the ...
Luca Ferroglio's user avatar
5 votes
2 answers
684 views

How to optimize the following function?

Consider the following function: function[x_, y_] = If[x < Exp[y], Sqrt[1 - x/Exp[y]], 0]; functionTotal1[x_] = Sum[function[x, 0.02*j], {j, 0, 4*10^5, 1}]; ...
John Taylor's user avatar
  • 4,866
1 vote
1 answer
72 views

Identifying transitions in an array

I have two arrays of elements. The second array is generated by the transfer of one or more element to another position. I want to know which element is transferred to which position. Following is the ...
user49535's user avatar
  • 1,103
7 votes
2 answers
322 views

Randomly generated coordinates

I want to code that will do the following: Write a Mathematica function called randomHop that takes as its argument a list containing two coordinates x and y; ...
Ganyo10's user avatar
  • 71
1 vote
2 answers
59 views

How to choose model parameters using `Piecewise` function

The following code (developed by m_goldberg) works as expected. The model parameters can be selected by using Button, being ...
Tugrul Temel's user avatar
  • 4,865
1 vote
1 answer
255 views

Conditionally returning a constant if function returns Undefined

Often times functions might return Undefined in Mathematica, e.g. when the Volume@RegionIntersection is called for 2 objects ...
user avatar
5 votes
3 answers
333 views

How to kick out points that are specific to a function

I have a list of xi,yi points. I want to remove any points from the list where x^2+y^2<1. I probably have to write a for loop of some sorts, not sure how to begin. ...
subrinarafiq's user avatar
3 votes
1 answer
84 views

Condition in SparseArray does not check the condition for some matrix elements

Consider the following MWE: SparseArray[{{i_, j_} /; (IntegerDigits[i - 1, 2, 2][[1]] == 0) :> 1}, {4, 4}] This should give a $4\times 4$ matrix in which the ...
glS's user avatar
  • 7,513
3 votes
1 answer
98 views

Changing ContourStyle based on an inequality

I would like to create a bifurcation diagram with ContourPlot. Suppose I want to plot the contour 1-2*a*Q+3*Q^3==0 with ...
Physicist's user avatar
  • 957
2 votes
1 answer
383 views

Conditional expectation

Suppose I have a bivariate normal random vector $(Y,Z)'$ where $Y\sim N(μ1,\sigma_1^2)$, $Z\sim N(μ2,\sigma_2^2)$, and $Cov[Y,Z]=\sigma_{12}$. If I want Mathematica to compute the conditional ...
Benno's user avatar
  • 123
1 vote
1 answer
51 views

How do I get my function to output only true case for conditions on its arguments

I am quite new to Mathematica and have an issue regarding which statement. cut[x_, y_] := which[x>y, doFunctionX[], y>x, doFunctionY[]] cut/@{X,Y}] I wish ...
Siddeshwar Raghavan's user avatar
0 votes
3 answers
80 views

How to find the root of g[x_] using If?

How can get roots only saved in the list? Input g[x_] := x^2 + x - 6; g /@ Table[i, {i, -10, 10}] If[g[x] == 0, i, Nothing] Output {-3,2} How can I achieved ...
kile's user avatar
  • 1,621
0 votes
0 answers
38 views

Mathematica Function Taxicab Numbers [duplicate]

I have spent days trying to compile this code and I cannot generate a list of taxicab numbers for the Hardy-Ramanujan Theory. Here is the module I wrote earlier: ...
Timothy Aaronn Norris's user avatar
4 votes
1 answer
249 views

How to include conditional statements in NIntegrate?

I have a function g which is the result of another integrated function f. This function (f) ...
Gummala Navneeth's user avatar
0 votes
1 answer
55 views

Getting unexpected results form If expressions [closed]

I'm mystified by this: g[a_, b_, c_] := Module[{d}, d = If[(b < 1), b, b + 1]; If[a < b, d = 0.]; d*c] g[a, 20, 40] ...
frankeye's user avatar
2 votes
0 answers
146 views

Is Mathematica computing the conditional expectation correctly?

Given a simple case of say 2 independent trials each with probability of success 1/3. Consider a random variable that is gives the 3 times the total successes. We can define this random variable in ...
user13892's user avatar
  • 8,779
3 votes
1 answer
99 views

Conditional formatting of `Table` with `Join` and `Transpose`

A simple example of what I'm trying to do (but I want to be able to apply the same principle to much larger data sets and with more complicated functions for low ...
Richard Burke-Ward's user avatar
1 vote
2 answers
132 views

Combining "Product" and "If"

...
Hulycez Cruz's user avatar
1 vote
1 answer
146 views

Checking if an edge is an element of a list of edges

I have graph whose edge list is given by ...
Joe's user avatar
  • 165
1 vote
2 answers
69 views

How do I modify my code to detect cycles in this Collatz-like recursion?

A popular variant of the Collatz recursion is the following. Given a positive integer $n_i$, If $n_i$ is even, $n_{i+1}=n/2$, If $n_i$ is odd, $n_{n+1}=3n-1$. The Mathematica code I'm using to ...
Klangen's user avatar
  • 957
2 votes
3 answers
117 views

Highlight largest value in each column of `Table // TableForm`

I am exploring colossally abundant numbers; in particular the set of specific 'critical epsilon values'. I table the function DivisorSigma[1, n]/n^(1 + \[Epsilon]) ...
Richard Burke-Ward's user avatar
5 votes
4 answers
721 views

List, map function based on a condition

I have the following list: ...
Michiel van Mens's user avatar
3 votes
1 answer
70 views

Combining `With`, `If` and SetDelayed

I just noticed that With[{x = 1}, If[x == 0, 0, 1]] returns 1 (as I expected) but ...
A.G.'s user avatar
  • 4,312
0 votes
0 answers
39 views

Why does NumberForm disturb the output of Rationalize? [duplicate]

I am currently using Rationalize after NumberForm in order to check if the some values of my function are in the ...
Klangen's user avatar
  • 957
5 votes
3 answers
640 views

Arrange a list in ascending order by deleting list elements

I am new to mathematica and currently learning it by solving some basic problems. I am trying to arrange a list in ascending order using a detailed IF loop. For instance if we start out with $\{1,65,...
primefinder's user avatar
2 votes
1 answer
81 views

Compare values in specific times between several days

I have imported csv file which is a Daily Load Diagram consisting of 3 months (1st dec 2018 to 28th feb 2019) and the values of current are taken every 10 seconds. ...
Vocis's user avatar
  • 185

1 2 3
4
5
11