for work I have to solve the Optical Bloch Equations for a 2-Level System and I appear to be really stuck on my code in Mathematica:
O=1;
g=1;
d=0;
sol3=NDSolve
[
{
x'[t]==g y[t] + I/2 (O* b[t] - O a[t]),
y'[t]==-g y[t]+ I/2 (O a[t]-O* b[t]),
a'[t]==-(g/2+I d) a[t] + I/2 =O* (y[t]-x[t]),
b'[t]==-(g/2-I d) b[t] + I/2 O* (x[t]-y[t]),
x[0]==1,
y[0]==0,
b[0]==0,
a[0]==0
},
{x,y},{t,0,100}
]
The Error I get is: Syntax::tsntxi: "whole DE-System" is incomplete; more input is needed.
I would be very grateful if you could point out my error(s)
Thank you all :)
w = 1;
g = 1;
d = 0;
swl3 = NDSolve[{
x'[t] == g y[t] + I/2 (w*b[t] - w a[t]),
y'[t] == -g y[t] + I/2 (w a[t] - w*b[t]),
a'[t] == -(g/2 + I d) a[t] + I/2 w*(y[t] - x[t]),
b'[t] == -(g/2 - I d) b[t] + I/2 w*(x[t] - y[t]),
x[0] == 1, y[0] == 0, b[0] == 0, a[0] == 0},
{x, y, a, b},
{t, 0, 100}]
Plot[Evaluate[{x[t], y[t], a[t], b[t]} /. swl3], {t, 0, 100}]
a[t] and b[t] are complex so they don't appear in the plot. You can plot the real and imaginary part separately.
Related
Today I have two sequences,
s1 = CCGGGTTACCA
s2 = GGAGTTCA
The Mismatch Score is -1, the Gap Score is -2.
The Optimal Sequence Alignment has two answers (miniumn penalty is -8).
ans1 = G - G A G T T - C - A
C C G G G T T A C C A
ans2 = - G G A G T T - C - A
C C G G G T T A C C A
ans3 = G - G A G T T - - C A
C C G G G T T A C C A
ans4 = - G G A G T T - - C A
C C G G G T T A C C A
If any algorithm can calculate the number of Optimal Sequence Alignment (it will return "4") ?
Or what can I do to solve this problem?
Thanks
My score system is on the picture.
I do the Needleman-Wunsch algorithm (dynamic program) to complete the table.
Finally, I give up to only find the number of Optimal Sequence Alignment.
I trackback to find all the possible answers and insert the set, so the the size of set is my answer.
set<pair<string, string>> st;
void findAll(string A, string B, int gap, int mis, int i, int j, string s1, string s2) {
if (s1.size() == max(A.size(), B.size()) && s2.size() == max(A.size(), B.size())) {
reverse(begin(s1), end(s1));
reverse(begin(s2), end(s2));
st.insert({s1, s2});
return;
}
if (i != 0 || j != 0) {
if (i == 0) {
findAll(A, B, gap, mis, i, j - 1, s1 + "-", s2 + B[j - 1]);
} else if (j == 0) {
findAll(A, B, gap, mis, i - 1, j, s1 + A[i - 1], s2 + "-");
} else {
if ((A[i - 1] == B[j - 1] && dp[i][j] == dp[i - 1][j - 1]) || (A[i - 1] != B[j - 1] && dp[i][j] == dp[i - 1][j - 1] + mis)) {
findAll(A, B, gap, mis, i - 1, j - 1, s1 + A[i - 1], s2 + B[j - 1]);
}
if (dp[i][j] == dp[i - 1][j] + gap) {
findAll(A, B, gap, mis, i - 1, j, s1 + A[i - 1], s2 + "-");
}
if (dp[i][j] == dp[i][j - 1] + gap) {
findAll(A, B, gap, mis, i, j - 1, s1 + "-", s2 + B[j - 1]);
}
}
}
}
I want to sum the matrices which is computed before.
For r=1, n=3; Subscript[P, i] are 3x3 matrices like P1,P2,P3.
My codes are like this :
'Y = Inverse[S];
Print["Y=", MatrixForm[Y]];
For[i = 1, i <= n, i++,
Subscript[P, i] = MatrixForm[Outer[Times, S[[All, i]], Y[[i]]]];
Print["CarpimS=", MatrixForm[S[[All, i]]]];
Print["CarpimY=", MatrixForm[Y[[i]]]];
Print["P=", MatrixForm[Outer[Times, S[[All, i]], Y[[i]]]]];
];
toplamP = MatrixForm[ConstantArray[0, {n, n}]];
For[i = 2. r + 1, i <= n, i++,
toplamP = toplamP + Subscript[P, i];
];
Print["ToplamP=", toplamP];'
But mathematica gives me only P3 and and P3 doesn't have a matrix form.
I am solving a project in Mathematica 10 and I think that the best way to do it is using a loop like For or Do. After build it I obtain the results I looking for but with a to much big multiplicity. Here is the isolated part of the code:
(*Initializing variables*)
epot[0] = 1; p[0] = 1; \[Psi][0] = HermiteH[0, x] E^(-(x^2/2));
e[n_] := e[n] = epot[n];
(*Defining function*)
\[Psi][n_] := \[Psi][n] = (Sum[p[k]*x^k,{k,0,4*n}]) [Psi][0];
(*Differential equation*)
S = - D[D[\[Psi][n], x], x] + x^2 \[Psi][n] + x^4 \[Psi][n - 1] - Sum[e[n-k]*\[Psi][k],{k,0,n}];
(*Construction of the loop*)
S1 = Collect[E^(x^2/2) S, x, Simplify];
c = Coefficient[S1, x, 0];
sol = Solve[c == 0, epot[n]]; e[n] = epot[n] /. sol;
For[j = 1, j <= 4 n, j++,
c = Coefficient[S1, x, j];
sol = Solve[c == 0, p[j]];
p[j] = p[j] /. sol;];
(*Results*)
Print[Subscript[e, n], "= ", e[n] // InputForm];
Subscript[e, 1]= {{{3/4}}}
Print[ArrayDepth[e[n]]];
3 (*Multiplicity, it should be 1*)
Print[Subscript[\[Psi], n], "= ", \[Psi][n]];
Subscript[\[Psi], 1]= {{E^(-(x^2/2)) (1-(3 x^2)/8-x^4/8)}}
Print[ArrayDepth[\[Psi][n]]];
2 (*Multiplicity, it should be 1*)
After this calculation, the question remaining is how do i substitute this results in the original functions. Thank you very much.
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.
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]