Solve the ODE with NDSolve and dependet variables - wolfram-mathematica

I have intialconditions:
sf = 200;
sm = 100;
p = 40;
betaf = 0.15;
betam = 0.15;
mums = 0.02;
mufs = 0.02;
sigma = 0.20;
mum = 0.02;
muf = 0.02;
and the ODE:
sf' := -muf*sf + (betaf + mums + sigma)*p - HarmonicMean[sf, sm];
sm' := -mum*sm + (betam + mufs + sigma)*p - HarmonicMean[sf, sm}];
p' := p - (mufs + mums + sigma)*p + HarmonicMean[{sf, sm}];
That i want is an abstract solution (sf(t),sm(t),p(t)) with NDSolve to plot it later.
My problem is that all variables are dependet in all 3 equations, so i don't know how to write the NDSolve call.

I could not manage to get an analytic solution, but the numerical one goes like this. Note that not all symbols you listed are variables of the system: those not being dependent of the independent variable t are parameters. (Also note that there are some typos in the OP's code).
variables = {sf[t], sm[t], p[t]};
parameters = {betaf -> 0.15, betam -> 0.15, mums -> 0.02,
mufs -> 0.02, sigma -> 0.20, mum -> 0.02, muf -> 0.02};
equations = {
sf'[t] == -muf*sf[t] + (betaf + mums + sigma)*p[t] -
HarmonicMean[{sf[t], sm[t]}],
sm'[t] == -mum*sm[t] + (betam + mufs + sigma)*p[t] -
HarmonicMean[{sf[t], sm[t]}],
p'[t] ==
p[t] - (mufs + mums + sigma)*p[t] + HarmonicMean[{sf[t], sm[t]}],
sf[0] == 200,
sm[0] == 100,
p[0] == 40
};
sol = NDSolve[equations /. parameters, variables, {t, 0, 100}];
Plot[Evaluate[variables /. sol], {t, 0, 100}]

Related

How can I optimise my code using "For-Loop"?

I am trying to develop a code which calculates Local Density of states of electrons in a material. For which I am using a multiple for loops and multiple tables. it takes 45sec to complete, I need less time for that. any suggestions how to optimize this code.
AbsoluteTiming[Ns=2; \[Eta] = 0.001;
Nx=15;
Ny=15;
NN=Nx*Ny;
Nband=8;
kkmx = Ns*Nx;
kkmy = Ns*Ny;
wmax = 0.2; nw = 800; p = 0;
Print["starting ldos calc"];
nsite = 2;
ldos = 0;
For[kx = 0, kx <= (Ns - 1.)*2*(Pi/kkmx), kx += 2*(Pi/kkmx),
For[ky = 0, ky <= (Ns - 1.)*2*(Pi/kkmy), ky += 2*(Pi/kkmy),
ES = Eigensystem[H];
elist = Table[ES[[1,l]], {l, 1, Nband/2*4*NN}];
ulist = Table[Abs[ES[[2,l,i]]]^2, {l, 1, Nband/2*4*NN}, {i, 388+1, 388+(nsite - 1)*Nband/2 + Nband/2}];
vlist = Table[Abs[ES[[2,l,i + Nband/2*NN*2]]]^2, {l, 1, Nband/2*4*NN}, {i, 388+1, 388+(nsite - 1)*Nband/2 + Nband/2}];
ldossc = Table[Im[Total[Table[ulist[[l,1 ;; All]]*(1/(-wmax + wmax*2*(w/nw) - elist[[l]] + I*\[Eta])) +
vlist[[l,1 ;; All]]*(1/(-wmax + 2*wmax*(w/nw) + elist[[l]] + I*\[Eta])), {l, 1, Nband/2*4*NN}]]], {w, 0, nw}]; ldos = ldos + ldossc;
Export["ldosorb_up_P.dat", Table[{-wmax + wmax*2*(\[Omega]/nw), (-Pi^(-1))*(ldos[[\[Omega] + 1,i]]/Ns^2)}, {\[Omega], 0, nw}, {i, 1,8}]];
(* Export["ldostot.dat", Table[{-wmax + wmax*2*(\[Omega]/nw), (-Pi^(-1))*((ldos[[\[Omega] + 1,i]] + ldos[[\[Omega] + 1,i + 1]] + ldos[[\[Omega] + 1,i + 2]] + ldos[[\[Omega] + 1,i + 3]] + ldos[[\[Omega] + 1,i + 4]])/Ns^2)}, {\[Omega], 0, nw}, {i, 1, (nsite - 1)*Nband/2 + Nband/2 - 4}]]; *)
Print["kx=", kx, " ky=", ky, " nsx=", (kx/(2*Pi))*kkmx + 1.]; ]; ]; ]```

Create matrix and fill it in wolfram

i want to translate my C++ code to wolfram, to improve my calcs.
C++ code
for(int i = 0; i < N - 1; ++i){
matrix[i][i] += L / 3 * uCoef - duCoef / 2 - (double)du2Coef/L;
matrix[i][i+1] += L / 6 * uCoef + duCoef / 2 + (double)du2Coef/L;
matrix[i+1][i] += L / 6 * uCoef - duCoef / 2 + (double)du2Coef/L;
matrix[i+1][i+1] += L / 3 * uCoef + duCoef / 2- (double)du2Coef/L;
}
all this coef are const, N - size of my matrix.
In[1]:= n = 4; uCoef = 2; duCoef = 3; du2Coef = 7; L = 11.;
matrix = Table[0, {n}, {n}];
For[i = 1, i < n, ++i,
matrix[[i, i]] += L/3*uCoef - duCoef/2 - du2Coef/L;
matrix[[i, i+1]] += L/6*uCoef - duCoef/2 - du2Coef/L;
matrix[[i+1, i]] += L/6*uCoef + duCoef/2 + du2Coef/L;
matrix[[i+1, i+1]] += L/3*uCoef - duCoef/2 + du2Coef/L];
matrix
Out[4]= {
{5.19697, 1.5303, 0, 0},
{5.80303, 11.6667, 1.5303, 0},
{0, 5.80303, 11.6667, 1.5303},
{0, 0, 5.80303, 6.4697}}
Each character that has been changed from your original is hinting there is a fundamental difference between C++ and Mathematica
You should use SparseArray for such banded arrays in mathematica:
n = 5; uCoef = 2; duCoef = 3; du2Coef = 7; L = 11.;
matrix = SparseArray[
{{1, 1} -> L/3*uCoef - duCoef/2 - du2Coef/L,
{i_ /; 1 < i < n, i_} -> -duCoef + 2 L uCoef/3 ,
{n, n} -> ( L/3 uCoef - duCoef/2 + du2Coef/L ),
Band[{1, 2}] -> L/6 uCoef - duCoef/2 - du2Coef/L,
Band[{2, 1}] -> L/6 uCoef + duCoef/2 + du2Coef/L}, {n, n}];
MatrixForm#matrix
Even if you insist on the For loop, initialize the matrix as :
matrix = SparseArray[{{_, _} -> 0}, {n, n}];

Mathematica 8 Using Heun's Method/Improved Euler's Method

Clear[x, y, h, k, FirstSlope, SecondSlope];
h = [Pi]; y[[Pi]] = 0;
dy[x_, y_] = (Cos[x] - 3 x^2 y)/x^3;
Do[{x[k] = [Pi] + h*(k - [Pi]),
FirstSlope = dy[x[k], y[k]],
SecondSlope = dy[x[k] + h, y[k] + h*FirstSlope],
y[k + [Pi]] = y[k] + (h*(FirstSlope + SecondSlope))/2}, {k, [Pi],
5[Pi]}] Table[{x[k], y[k]}, {k, [Pi], 5[Pi]}];
MatrixForm[%]
Above image is my error. I'm trying to use Heun's method and my problem is:
1) I want it to stop at y[5 Pi] but it keeps going. I can manipulate it so that it goes to y[5 Pi], but I want to know why exactly it's doing this.
2) y[k] is not evaluating at k=pi,2pi,3pi, etc.

1)a workaround for "NMaximize" error "function unbounded." but don't know why 2) more importantly, how to speed up this 3d region plot (see update2)

When I was trying to find the maximum value of f using NMaximize, mathematica gave me a error saying
NMaximize::cvdiv: Failed to converge to a solution. The function may be unbounded.
However, if I scale f with a large number, say, 10^5, 10^10, even 10^100, NMaximize works well.
In the two images below, the blue one is f, and the red one is f/10^10.
Here come my questions:
Is scaling a general optimization trick?
Any other robust, general workarounds for the optimizations such
needle-shape functions?
Because the scaling barely changed the shape of the needle-shape of
f, as shown in the two images, how can scaling work here?
thanks :)
Update1: with f included
Clear["Global`*"]
d = 1/100;
mu0 = 4 Pi 10^-7;
kN = 97/100;
r = 0.0005;
Rr = 0.02;
eta = 1.3;
e = 3*10^8;
s0 = 3/100;
smax = 1/100; ks = smax/s0;
fre = 1; tend = 1; T = 1;
s = s0*ks*Sin[2*Pi*fre*t];
u = D[s, t];
umax = N#First[Maximize[u, t]];
(*i=1;xh=0.1;xRp=4.5`;xLc=8.071428571428573`;
i=1;xh=0.1;xRp=4.5;xLc=8.714285714285715;*)
i = 1; xh = 0.1; xRp = 5.5; xLc = 3.571428571428571`;
(*i=1;xh=0.1`;xRp=5.`;xLc=6.785714285714287`;*)
h = xh/100; Rp = xRp/100; Lc = xLc/100;
Afai = Pi ((Rp + h + d)^2 - (Rp + h)^2);
(*Pi (Rp-Hc)^2== Afai*)
Hc = Rp - Sqrt[Afai/Pi];
(*2Pi(Rp+h/2) L/2==Afai*)
L = (2 Afai)/(\[Pi] (h + 2 Rp));
B = (n mu0 i)/(2 h);
(*tx = -3632B+2065934/10 B^2-1784442/10 B^3+50233/10 B^4+230234/10 \
B^5;*)
tx = 54830.3266978739 (1 - E^(-3.14250266080741 B^2.03187556833859));
n = Floor[(kN Lc Hc)/(Pi r^2)] ;
A = Pi*(Rp^2 - Rr^2);
b = 2*Pi*(Rp + h/2);
(* -------------------------------------------------------- *)
Dp0 = 2*tx/h*L;
Q0 = 0;
Q1 = ((1 - 3 (L tx)/(Dp h) + 4 (L^3 tx^3)/(Dp^3 h^3)) Dp h^3)/(
12 eta L) b;
Q = Piecewise[{{Q1, Dp > Dp0}, {Q0, True}}];
Dp = Abs[dp[t]];
ode = u A - A/e ((s0^2 - s^2)/(2 s0 )) dp'[t] == Q*Sign[dp[t]];
sol = First[
NDSolve[{ode, dp[0] == 0}, dp, {t, 0, tend} ,
MaxSteps -> 10^4(*Infinity*), MaxStepFraction -> 1/30]];
Plot[dp''[t] A /. sol, {t, T/4, 3 T/4}, AspectRatio -> 1,
PlotRange -> All]
Plot[dp''[t] A /10^10 /. sol, {t, T/4, 3 T/4}, AspectRatio -> 1,
PlotRange -> All, PlotStyle -> Red]
f = dp''[t] A /. sol;
NMaximize[{f, T/4 <= t <= 3 T/4}, t]
NMaximize[{f/10^5, T/4 <= t <= 3 T/4}, t]
NMaximize[{f/10^5, T/4 <= t <= 3 T/4}, t]
NMaximize[{f/10^10, T/4 <= t <= 3 T/4}, t]
update2: Here comes my real purpose. Actually, I am trying to make the following 3D region plot. But I found it is very time consuming (more than 3 hours), any ideas to speed up this region plot?
Clear["Global`*"]
d = 1/100;
mu0 = 4 Pi 10^-7;
kN = 97/100;
r = 0.0005;
Rr = 0.02;
eta = 1.3;
e = 3*10^8;
s0 = 3/100;
smax = 1/100; ks = smax/s0;
f = 1; tend = 1/f; T = 1/f;
s = s0*ks*Sin[2*Pi*f*t];
u = D[s, t];
umax = N#First[Maximize[u, t]];
du[i_?NumericQ, xh_?NumericQ, xRp_?NumericQ, xLc_?NumericQ] :=
Module[{Afai, Hc, L, B, tx, n, A, b, Dp0, Q0, Q1, Q, Dp, ode, sol,
sF, uF, width, h, Rp, Lc},
h = xh/100; Rp = xRp/100; Lc = xLc/100;
Afai = Pi ((Rp + h + d)^2 - (Rp + h)^2);
Hc = Rp - Sqrt[Afai/Pi];
L = (2 Afai)/(\[Pi] (h + 2 Rp));
B = (n mu0 i)/(2 h);
tx = 54830.3266978739 (1 - E^(-3.14250266080741 B^2.03187556833859));
n = Floor[(kN Lc Hc)/(Pi r^2)] ;
A = Pi*(Rp^2 - Rr^2);
b = 2*Pi*(Rp + h/2);
Dp0 = 2*tx/h*L;
Q0 = 0;
Q1 = ((1 - 3 (L tx)/(Dp h) + 4 (L^3 tx^3)/(Dp^3 h^3)) Dp h^3)/(
12 eta L) b;
Q = Piecewise[{{Q1, Dp > Dp0}, {Q0, True}}];
Dp = Abs[dp[t]];
ode = u A - A/e ((s0^2 - s^2)/(2 s0 )) dp'[t] == Q*Sign[dp[t]];
sol = First[
NDSolve[{ode, dp[0] == 0}, dp, {t, 0, tend} , MaxSteps -> 10^4,
MaxStepFraction -> 1/30]];
sF = ParametricPlot[{s, dp[t] A /. sol}, {t, 0, tend},
AspectRatio -> 1];
uF = ParametricPlot[{u, dp[t] A /. sol}, {t, 0, tend},
AspectRatio -> 1];
tdu = NMaximize[{dp''[t] A /10^8 /. sol, T/4 <= t <= 3 T/4}, {t,
T/4, 3 T/4}, AccuracyGoal -> 6, PrecisionGoal -> 6];
width = Abs[u /. tdu[[2]]];
{uF, width, B}]
RegionPlot3D[
du[1, h, Rp, Lc][[2]] <= umax/6, {h, 0.1, 0.2}, {Rp, 3, 10}, {Lc, 1,
10}, LabelStyle -> Directive[18]]
NMaximize::cvdiv is issued if the optimum improved a couple of orders of magnitude during the optimization process, and the final result is "large" in an absolute sense. (To prevent the message in a case where we go from 10^-6 to 1, for example.)
So yes, scaling the objective function can have an effect on this.
Strictly speaking this message is a warning, and not an error. My experience is that if you see it, there's a good chance that your problem is unbounded for some reason. In any case, this warning is a hint that you might want to double check your system to see if that might be the case.

RSolve not solving discrete Rossler system

I'm working with chaotic attractors, and testing some continuous-> discrete equivalences. I've made a continuous simulation of the Rossler system this way
a = 0.432; b = 2; c = 4;
Rossler = {
x'[t] == -y[t] - z[t],
y'[t] == x[t] + a*y[t],
z'[t] == b + x[t]*z[t]-c*z[t]};
sol = NDSolve[
{Rossler, x[0] == y[0] == z[0] == 0.5},
{x, y, z}, {t,500}, MaxStepSize -> 0.001, MaxSteps -> Infinity]
Now, when trying to evaluate a discrete equivalent system with RSolve, Mma doesn't do anything, not even an error, it just can't solve it.
RosslerDiscreto = {
x[n + 1] == x[n] - const1*(y[n] + z[n]),
y[n + 1] == 1 - a*const2)*y[n] + const2*x[n],
z[n + 1] == (z[n]*(1 - const3) + b*const3)/(1 - const3*x[n])}
I want to know if there is a numerical function for RSolve, analogous as the NDSolve is for DSolve.
I know i can make the computation with some For[] cycles, just want to know if it exists such function.
RecurrenceTable is the numeric analogue to RSolve:
rosslerDiscreto = {
x[n+1] == x[n] - C[1]*(y[n] + z[n]),
y[n+1] == (1 - a*C[2])*y[n] + C[2]*x[n],
z[n+1] == (z[n]*(1 - C[3]) + b*C[3]) / (1 - C[3]*x[n]),
x[0] == y[0] == z[0] == 0.5
} /. {a->0.432, b->2, c->4, C[1]->0.1, C[2]->0.1, C[3]->0.1};
coords = RecurrenceTable[rosslerDiscreto, {x,y,z}, {n,0,1000}];
Graphics3D#Point[coords]

Resources