Is there a way in mathematica to have variable coefficients for NDSolve? I need to vary the coefficient values and create multiple graphs, but I cannot figure out a way to do it short of reentering the entire expression for every graph. Here is an example (non-functional) of what I would like to do; hopefully it is close to working:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] =
NDSolve[{\[Rho]eg'[t] ==
(I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t],
\[Rho]ge'[t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t],
\[Rho]ee'[t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[t],
\[Rho]gg'[t] == I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[t],
\[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] == 0, \[Rho]eg[0] == 0},
{\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[\[Rho]ee[t] /. X[5, 2, 6]], {t, 0, 10},PlotRange -> {0, 1}]
In this way I would only have to re-call the plot command with inputs for the coefficients, rather than re-enter the entire sequence over and over. That would make things much cleaner.
PS: Apologies for the awful looking code. I never realized until now that mathematica didn't keep the character conversions.
EDIT a nicer formatted version:
You should just use SetDelayed (":=") instead of Set in the function definition:
X[\[CapitalDelta]_, \[CapitalOmega]_, \[CapitalGamma]_] :=
NDSolve[{\[Rho]eg'[
t] == (I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ee[t] +
I*.5*\[CapitalOmega]*\[Rho]gg[t], \[Rho]ge'[
t] == (-I*\[CapitalDelta] - .5*\[CapitalGamma])*\[Rho]ge[t] +
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]ee[t] -
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]gg[t], \[Rho]ee'[
t] == -I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] +
I*.5*\[CapitalOmega]*\[Rho]ge[t] - \[CapitalGamma]*\[Rho]ee[
t], \[Rho]gg'[t] ==
I*.5*\[CapitalOmega]\[Conjugate]*\[Rho]eg[t] -
I*.5*\[CapitalOmega]*\[Rho]ge[t] + \[CapitalGamma]*\[Rho]ee[
t], \[Rho]ee[0] == 0, \[Rho]gg[0] == 1, \[Rho]ge[0] ==
0, \[Rho]eg[0] ==
0}, {\[Rho]ee, \[Rho]eg, \[Rho]ge, \[Rho]gg}, {t, 0, 12}];
Plot[Evaluate[{\[Rho]ee[t] /. X[5, 2, 6], \[Rho]ee[t] /.
X[2, 6, 17]}], {t, 0, 10}, PlotRange -> {0, 1}]
Related
This is my code.
def cb_objective(trial):
cb_param = {
'grow_policy' : trial.suggest_categorical('grow_policy',[
# 'SymmetricTree',
# 'Depthwise',
'Lossguide']),
'learning_rate' : trial.suggest_loguniform('learning_rate', 0.01, 0.8),
'n_estimators' : trial.suggest_int("n_estimators", 300,3000),
'max_depth' : trial.suggest_int("max_depth", 3, 16),
'random_strength' :trial.suggest_int('random_strength', 0, 100),
'l2_leaf_reg' : trial.suggest_loguniform("l2_leaf_reg",1e-6,3.0),
'max_bin' : trial.suggest_int("max_bin", 25, 300),
'od_type' : trial.suggest_categorical('od_type', ['IncToDec', 'Iter']),
'bootstrap_type' : trial.suggest_categorical("bootstrap_type", ["Bayesian", "Bernoulli", "Poisson"])}
if cb_param['grow_policy'] == 'Lossguide' or cb_param['grow_policy'] == 'Depthwise':
cb_param['min_child_samples'] = trial.suggest_int('min_child_samples',1,100)
if cb_param['grow_policy'] == 'Lossguide':
cb_param['num_leaves'] = trial.suggest_int('num_leaves',20,50)
if cb_param['bootstrap_type'] =='Bayesian':
cb_param['bagging_temperature'] = trial.suggest_loguniform('bagging_temperature', 0.01, 100.00)
elif cb_param['bootstrap_type'] =='Bernoulli' or cb_param['bootstrap_type'] =='Poisson':
cb_param['subsample'] = trial.suggest_discrete_uniform('subsample', 0.6, 1.0, 0.1)
_fit_params={'early_stopping_rounds':100,
'eval_set': [(X,y)],
'verbose':0}
cbr = cb.CatBoostRegressor(
random_state=42,
task_type = 'GPU',
**cb_param
)
I'm sorry the code is too long. When the growth_policy is Lossguide, it operates without any problems, but in the case of SymetricTree or Depthwise, the colab kernel is down without outputting an error message.(What's interesting is that the kernel goes down after a few moves. I checked that there is no problem with the memory.)
I think there's a parametric impulse or out of gpu memory . Do you know anyone well in catboost?
No matter how many times I look up the official document, it seems to be beyond my ability.
picture 1 : grow_policy = symmetricTree,Depthwise , stop at 6 step
picture 2 : grow_policy = Lossguide , working well (same parameters)
Hi Iam using mindwave to control with OSC some Ndefs. I need to write a function that chooses from a list (eg [x, f]) the next and stops the previous. Also I need to be able to play only the first Ndef (eg x) and not the second (eg f) until I get the appropriate message (eg msg[3]) and at the same time to stop x. Here is my try which doesnt work as wanted:
(
OSCdef.new(
\select,
{
arg msg, time, addr, port;
[msg, time, addr, port].postln;
if ((msg[3] > 200), {x.clear;} && {f.play;});
},'/neurovals'
);
SynthDef.new(\Syn, { arg freq = 440, amp = 1, sustain = 0.5;
var sig, env, mod;
env = EnvGen.kr(Env.linen(0.05, sustain, 0.5, 0.08), doneAction: 2);
mod = Saw.ar(freq * 0.1) * LFNoise1.kr(2).range(10,800);
sig = SinOsc.ar(freq + mod) * env;
Out.ar(0, RLPF.ar(sig, freq, 1.7)).dup;
}).add;
x= Ndef(\blinkpat1, Pbind(
\instrument, \Syn,
\dur, 0.03,
\freq, 440
));
OSCdef.new(
\base,
{
arg msg, time, addr, port;
[msg, time, addr, port].postln;
if ((msg[1] == msg[1]),
{x.set(\freq, msg[1].linlin(0, 100, 300, 1000);)});
},'/neurovals'
);
SynthDef.new(\fmsyn, { arg freq = 440, amp = 1, sustain = 1;
var sig, env, mod;
env = EnvGen.kr(Env.linen(0.05, sustain, 0.5, 0.08), doneAction: 2);
mod = SinOsc.ar(freq * 0.5) * LFNoise1.kr(2).range(10,800);
sig = SinOsc.ar(freq + mod) * env;
Out.ar(0, RLPF.ar(sig, freq, 1.7)).dup;
}).add;
f = Ndef(\blinkpat2, Pbind(
\instrument, \fmsyn,
\freq, 440,
\sustain, 2,
\octave, 3,
\amp, 0.4,
));
OSCdef.new(
\base2,
{
arg msg, time, addr, port;
[msg, time, addr, port].postln;
if ((msg[1] == msg[1]),
{f.set(\freq, msg[1].linlin(0, 100, 300, 1000);)});
if ((msg[1] == msg[1]),
{f.set(\sustain, msg[1].linlin(0, 100, 1, 2);)});
},'/neurovals'
);
)
Firstly, this if statement:
if ((msg[3] > 200), {x.clear;} && {f.play;});
is strange - it's not clear trying to get with that, but it's unlikely that it's doing what you want. The && is a boolean operator, so the general expectation is the left and right sides are true/false. In your use, it's actually performing function composition on the left and right hand side functions, but that still isn't doing anything coherent, given your functions. I think what you probably intended is:
if (msg[3] > 200) {
x.clear;
f.play;
}
....which will clear x and play f.
There are a number of ways you can do what I think you're trying to do. Here's one suggestion - Pdef's can be assigned to each other, and hot swapped during playback. This means, you can do:
( // set up \a, \b, and \player, and start playing \player
Pdef(\a, Pbind(
\dur, 1/4,
\degree, 0,
));
Pdef(\b, Pbind(
\dur, 1/3,
\degree, 2,
));
\\ Start playing an empty Pdef to use later.
Pdef(\player).play;
)
Then, you can set \player to one of \a or \b to transparently switch between them:
// Assign Pdef(\a) to player, to start it playing
Pdef(\player, Pdef(\a));
// Assign Pdef(\b) to player, which starts \b and stops \a
Pdef(\player, Pdef(\b));
I wasn't entirely sure what to search for, so if this question has been asked before, I apologize in advance:
I have two functions
R := Rref*mx(mx^(4/3) - C0)^(-1)
M := Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1))
where Rref, Mref, C0 and C1 are constants and mx is the variable. I need to plot R as a function of M. Surely there must be something available in Mathematica to do such a plot - I just can't seem to find it.
The comment is correct, in that what you have is a set of two "parametric equations". You would use the ParametricPlot command. However, the syntax of functions with parameters is sometimes tricky, so let me give you my best recommendation:
R[Rref_, C0_, C1_][mx_] = Rref*mx (mx^(4/3) - C0)^(-1);
M[Mref_, C0_, C1_][mx_] = Mref*(mx + C1*mx^(-1)*((1 - C0*mx^(-4/3))^(-3) - 1));
I like that notation better because you can do things like derivatives:
R[Rref,C0,C1]'[mx]
(* Output: -((4 mx^(4/3) Rref)/(3 (-C0 + mx^(4/3))^2)) + Rref/(-C0 + mx^(4/3)) *)
Then you just plot the functions parametrically:
ParametricPlot[
{R[0.6, 0.3, 0.25][mx], M[0.2, 0.3, 0.25][mx]},
{mx, -10, 10},
PlotRange -> {{-10, 10}, {-10, 10}}
]
You can box this up in a Manipulate command to play with the parameters:
Manipulate[
ParametricPlot[
{R[Rref, C0, C1][mx], M[Mref, C0, C1][mx]},
{mx, -mmax, mmax},
PlotRange -> {{-10, 10}, {-10, 10}}
],
{Rref, 0, 1},
{Mref, 0, 1},
{C0, 0, 1},
{C1, 0, 1},
{mmax, 1, 10}
]
That should do it, I think.
I run into this problem occasionally, and I haven't found a way around it.
It usually happens when I'm finding the root of an equation, and want to maximize/minimize/plot that root according to some parameter. So I try to wrap the the code in a module so it can all be executed with just an input number, but it won't work inside functions like Plot.
For example:
f[din_] := Module[{d = din},
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
t1 = Flatten[FindRoot[y[x] - d /. sol, {x, 1}]];
x /. t1
]
f[2]
f[2.5]
f[3]
Plot[f[x], {x, 2, 3}]
The calls to f with a number all work as expected, but the f in the Plot function seems to be evaluated with the symbol 'x' - or something and just gives a lot of error text.
Any way around this?
Looking around the forums I found some suggestions for similar problems - like making the definition like this:
f[din_?NumericQ]:=
and I tried everything I could but nothing seems to make a difference.
I'm using Mathematica 8.0
The main fix is to take the sol = NDSolve[... out of the module. The module itself can also be simplified as shown:-
sol = NDSolve[{y'[x] == y[x], y[0] == 1}, y[x], {x, 0, 10}];
f[din_] := x /. FindRoot[y[x] - din /. sol, {x, 1}]
Plot[f[x], {x, 2, 3}]
Try :
f[din_?NumericQ] := Module[{LocalDummy, Localy, LocalSol},
Localy = y /. NDSolve[{y'[LocalDummy] == y[LocalDummy], y[0] == 1}, y, {LocalDummy, 0, 10}][[1]];
LocalSol = FindRoot[Localy[LocalDummy] - din == 0, {LocalDummy, 1}][[1, 2]] ]
Consider :
dist = Parallelize[
Table[RandomVariate[NormalDistribution[]], {100000}]];
How could I create a recursive function such that :
Subscript[d, 1] = dist[[1]]
Subscript[d, 2] = .95 Subscript[d, 1] + dist[[2]]
Subscript[d, 3] = .95 Subscript[d, 2] + dist[[3]]
And do this till Subscript[d, 100000]
Thank You.
It is surprisingly the first time I run into this.
Consider this:
dist = RandomVariate[NormalDistribution[], {100000}];
dist2 = Rest#FoldList[0.95 # + #2 &, 0, dist];
Subscript[d, x_] := dist2[[x]]
I don't normally use Subscript this way; I don't know what may break doing this. If you describe more of your problem, I may have an alternate suggestion.
How about using something like
In[1]:= dist = ParallelTable[RandomVariate[NormalDistribution[]], {100000}];//Timing
Out[1]= {0.15601, Null}
In[2]:= folded = FoldList[.95 #1 + #2 &, First#dist, Rest#dist]; // Timing
Out[2]= {0.056003, Null}
which you can compare to
In[3]:= Subscript[d, 1] = dist[[1]];
Do[Subscript[d, n] = 0.95 Subscript[d, n - 1] + dist[[n]],
{n, 2, Length#dist}] // Timing
Out[3]= {1.09607, Null}
In[4]:= Table[Subscript[d, n], {n, 1, Length#dist}] === folded
Out[4]= True