mmcdara

7891 Reputation

22 Badges

9 years, 54 days

MaplePrimes Activity


These are answers submitted by mmcdara

Many errors.

choosesystem := Maplet(.....):
Maplets[Display](maplet):

is not correct, this is probably what you wanted to do ?

maplet := Maplet(.....):
choosesystem := Maplets[Display](maplet):



Next, (look the construction of InputDialog in the help pages)

 Maplet(InputDialog['ID1']("Choose system?", 'Yes1' = Shutdown(['Yes']), 'No1' = Shutdown(['No']))):

is not correct : neither 'Ye1' nor'No1' are options of an InputDialog element. The correct syntax is (for instance, for you can exchange onapprove and oncancel)

 Maplet(InputDialog['ID1']("Choose system?", 'onapprove' = Shutdown(['Yes']), 'on cancel' = Shutdown(['No']))):

 

 

More of this : 

Shutdown(['Yes'])

means : "return the content of element named 'Yes'", which doesn't exist, so another mistake.
Mybe you wanted to return "Yes" ? In this cas write 

Shutdown(["Yes"])





Let me be more constructive now: personnaly I wouldn't have used InputDialog but RadioButton to do this.
Here is the way I would do that (but it's just my opinion and you could prefer to do it otherwise)

 

restart:

with(ListTools):

with(Maplets[Elements]):

f := proc(text)
  local maplet, choice:
  maplet := Maplet(
    [
      Label(text),
      [
        RadioButton['RB1']("yes", 'value'=true, 'group'='BG1'),
        RadioButton['RB2']("no", 'value'=false, 'group'='BG1')
      ],
      [
        Button("OK", Shutdown(['RB1', 'RB2']))
      ]
    ],
    ButtonGroup['BG1']()
  ):
  choice := Maplets[Display](maplet):
  `if`(parse(choice[1]), "yes", "no");
end proc:

result1 := f("Choose system?");      #select the "yes" button
result2 := f("Like and it works?")   #select the "no" button

"yes"

 

"no"

(1)

 


 

Download Maybe_this.mw

To complete Preben's reply,
if you only want to plot a set of points whose co-ordinates are contained in 2 vectors, possibly with undefined elements, plot or ScatterPlot can do the job without prior selection of the points with both real co-ordinates.
 

restart;

with(RealDomain):

roll := rand(-3.0..5.0):
p := <seq(roll(), i=1..10)>:
q := sqrt~(p):
p,q;

Statistics:-ScatterPlot(p, q, labels=['p', `sqrt(p)`], axes=normal);

Vector(10, {(1) = 4.418130698, (2) = 1.413665415, (3) = .838744609, (4) = -.244301281, (5) = 3.676432597, (6) = -1.645261861, (7) = 4.190905574, (8) = -2.412421905, (9) = 4.528147374, (10) = 1.572161447}), Vector(10, {(1) = 2.101934989, (2) = 1.188976625, (3) = .9158300110, (4) = Float(undefined), (5) = 1.917402565, (6) = Float(undefined), (7) = 2.047170138, (8) = Float(undefined), (9) = 2.127944401, (10) = 1.253858623})

 

 

plot([ seq([p[i], q[i]], i=1..10) ], style=point, labels=['p', `sqrt(p)`])

 

# points whose 2nd component is real

pq := [ seq(<p[i], q[i]>, i=1..10) ]:
Statistics:-Select(t->is(t[2], real), pq)

[Vector(2, {(1) = 4.418130698, (2) = 2.101934989}), Vector(2, {(1) = 1.413665415, (2) = 1.188976625}), Vector(2, {(1) = .838744609, (2) = .9158300110}), Vector(2, {(1) = 3.676432597, (2) = 1.917402565}), Vector(2, {(1) = 4.190905574, (2) = 2.047170138}), Vector(2, {(1) = 4.528147374, (2) = 2.127944401}), Vector(2, {(1) = 1.572161447, (2) = 1.253858623})]

(1)

 


 

Download undefined.mw

@mlheldwein 

Here is another way using convolution and Fourier transform.
(notional examples...adjust the length of the window and the period in example 2).


 

restart;

with(plots):

x:=10+1.5*t+3*sin(2*Pi*(5)*t+2);


X     := unapply(x, t);
H     := t -> 2*(Heaviside(t+0.5)-Heaviside(t));
X_avg := t -> int(X(tau)*H(t-tau),tau=-infinity..+infinity);


plot([X(t), X_avg(t)],t=0..3, legend=["x","X_avg"], axes=boxed);
 

10+1.5*t+3*sin(10*Pi*t+2)

 

proc (t) options operator, arrow; 10+1.5*t+3*sin(10*Pi*t+2) end proc

 

proc (t) options operator, arrow; 2*Heaviside(t+.5)-2*Heaviside(t) end proc

 

proc (t) options operator, arrow; int(X(tau)*H(t-tau), tau = -infinity .. infinity) end proc

 

 

XF    := eval(inttrans:-fourier(X(t), t, xi));
HF    := eval(inttrans:-fourier(H(t), t, xi));
FF    := XF*HF:
X_avg := eval(inttrans:-invfourier(FF, xi, t));

plot([x, X_avg],t=0..3, legend=["x","X_avg"], axes=boxed);

Pi*((3*I)*Dirac(xi+10*Pi)*exp(-2*I)-(3*I)*Dirac(xi-10*Pi)*exp(2*I)+(3*I)*Dirac(1, xi)+20*Dirac(xi))

 

(2*I)*(-exp(((1/2)*I)*xi)+1)/xi

 

(1/40)*(60*Pi*t+415*Pi+48*cos(10*Pi*t+2))/Pi

 

 

X    := t -> piecewise(t<0, 0,  t<1, t, t<2, 2-t, 0):
Xper := sum(X(t-2*k), k=0..10):

plot(Xper, t=0..5);

XF    := eval(inttrans:-fourier(Xper, t, xi)):
FF    := XF*HF:
X_avg := eval(inttrans:-invfourier(FF, xi, t)):

plot([Xper(t), X_avg],t=0..10, legend=["x","X_avg"], axes=boxed);

 

 

 


 

Download Using_Convolution.mw

I don't know, maybe the expression becomes too complex with the original form of ode2 and Maple is trapped somewhere???

In such a case I always try to use a simpler form of the ode to make sure to avoid this kind of preoblem.
For instance try this

sol := dsolve({diff(Q(t),t)=A*t^B*Q(t)+C, Q(0)=S}, Q(t))

The last line replaces A, B and S by their values in the original form of ode2
 

restart

sol := dsolve({diff(Q(t),t)=A*t^B*Q(t)+C, Q(0)=S}, Q(t))

Q(t) = (C*(A/(B+1))^(-1/(B+1))*((B+1)^2*t^(B/(B+1)+1/(B+1)-B-1)*(A/(B+1))^(1/(B+1))*(B^2*t^(B+1)*A/(B+1)+B^2+2*t^(B+1)*A*B/(B+1)+3*B+t^(B+1)*A/(B+1)+2)*(t^(B+1)*A/(B+1))^(-(1/2)*(B+2)/(B+1))*exp(-(1/2)*t^(B+1)*A/(B+1))*WhittakerM(1/(B+1)-(1/2)*(B+2)/(B+1), (1/2)*(B+2)/(B+1)+1/2, t^(B+1)*A/(B+1))/((B+2)*(2*B+3)*A)+(B+1)^2*t^(B/(B+1)+1/(B+1)-B-1)*(A/(B+1))^(1/(B+1))*(B+2)*(t^(B+1)*A/(B+1))^(-(1/2)*(B+2)/(B+1))*exp(-(1/2)*t^(B+1)*A/(B+1))*WhittakerM(1/(B+1)-(1/2)*(B+2)/(B+1)+1, (1/2)*(B+2)/(B+1)+1/2, t^(B+1)*A/(B+1))/((2*B+3)*A))/(B+1)+S)*exp(t^(B+1)*A/(B+1))

(1)

eval(sol, [A=-alpha*beta, B=beta-1, C=-a*p^b]):

 


 

Download workaround.mw

You will find nothing with CurveFitting ar any other smoothing strategy.
The best thing to do is to refine your "x" points (or 'i"), for instance by writting :

seqi := [seq(-10..10, h)]:  # where h << 1
for i inseqi do
...
end do:
ptsN1 := [seq(..., i in seqi)
]:  # no need to use CodeTools:-Usage 


But if you do this you still won't be able to see anything for the peaks are concentrated in the ranges -4.35..-4.1 and 4.1..4.35
To verify this, just do
seqi :=  [seq(-4.35..4.1, 0.01)]:  # for the peak at negative location.

 

PS : Some remarks:

  • There is no need to define d[i]:=i
    Replace all occurrences of d[i] by i
  • There is no need to define all inner variables  under the form variable[i].
    For instance write lambda1 instead of lambda[i]
  • The only exception is for f.
    Before the loop write ptsN1:=NULL:
    And inside the loop replace 
    f[d[i]]:=map(fnormal, evalf(h1[i]*(no*J1mod[i]+Omega^2*J2mod[i]))):
    by
    ptsN1 := ptsN1, [i, h1*Omega^2*J2mod ]:
    (for you have n0:=0 ; more of this map and fnormal are useless).
    After end do write 
    ptsN1 := [ptsN1]:
    This will avoid the further definition of ptsN1
  • The for loop contains a lot of things that are constant and should displaced outside (a, b, ...)
    In particular 

    J2[i]:=sum(g1[i]*J1[i]+g1[i]*F2[i],k=1..1);
    Should be replaced by J2:=g1*(J1+F2); for k=..1 means k=1.

  • The command 
    g1[i]:=0.5*sqrt(Pi)*tau0*exp(c2^2) *exp(-conjugate(a)*((k-1)*xr)^2)/(sqrt(conjugate(a)));

    Can be displaced outside of the loop because g1 is a constant.
    More of this, as k=1 (see above), it should be written
    g1 :=0.5*sqrt(Pi)*tau0*exp(c2^2) /(sqrt(conjugate(a)));
     
  • etc
     

I do not know if this code is yours or if it is somebody's else, but I believe you must do a huge effort to (re)write it in a more clever way. 
As it is I doubt you will find someone patient enough to do this job in your place and provide you more advices than I already did.

 

 

 

Firstly the abscissas of your 12 points are 1, 2, ... 12, so I don't understand how you can say the period is 12, you need to add either the point x=0 or either the point x=13 to be able to say that.

Next: you say nothing about the type of function you want to draw. Dharr gave you a solution which is just one amid an infinity of solutions.
Do you wanr to interpolate the points or approximate them?

Here is a solution using interpolation.
The global function is not constructed but it is (formally) very simple to do if you need it, see here Dirac_comb
 

``

restart:

p := [[1, 353913], [2, 286917], [3, 1494917], [4, 644834], [5, 399736], [6, 549314], [7, 963371], [8, 786619], [9, 328973], [10, 645844], [11, 1198539], [12, 302957]]:

# periodisation (period=12) by ading the point [13, p[1][2]]

P         := [p[], [13,p[1][2]]];

# spline interpolation (you can change the degree)

si := unapply( CurveFitting:-Spline(P, t, degree=2, endpoints='natural'), t):

# basic spline pattern

pattern := t -> piecewise(t >=1 and t <= 13, si(t), 0);

# basic spline pattern

plots:-display(
  plot(P, style = point, color = red, symbol = solidcircle, symbolsize = 15),
  plot(pattern(t), t= -1 .. 15, color = blue, discont=true)
)

[[1, 353913], [2, 286917], [3, 1494917], [4, 644834], [5, 399736], [6, 549314], [7, 963371], [8, 786619], [9, 328973], [10, 645844], [11, 1198539], [12, 302957], [13, 353913]]

 

proc (t) options operator, arrow; piecewise(1 <= t and t <= 13, si(t), 0) end proc

 

 

# Now draw translated replicates of the pattern

plot([seq(piecewise(t >=1+12*k and t <= 13+12*k, pattern(t-12*k), 0), k=-4..4)], t=-48..49, discont=true, size=[1000, 400])

 

NULL

 

Download Pattern_periodisation.mw

If you prefer Dharr solution AND you 12 points, then consider the period is 11 (not 12) and modify Dharr's code this way (note n=1..5 instead of n=1..3 to obtain an interpolator, and that the period is 11. and not 11 to get a faster output).
WATCHOUT:if you do this y[1] MUST BE EQUAL to y[12]

f := (1/2)*a[0]+add(a[n]*sin(2*n*Pi*t/(11.))+b[n]*cos(2*n*Pi*t/(11.)), n = 1 .. 5);
 
fn := LeastSquares(p, t, curve = f);
fnplot := plot(fn, t = -12 .. 24, color = blue)
plots:-display(P, fnplot);


Fit_2.mw

restart:
interface(version)
Standard Worksheet Interface, Maple 2015.2, Mac OS X, December 21 2015 Build ID 1097895

with(Units[Natural]):
cos(45*Unit(degrees));
sin(90*Unit(degrees));

And in more concise way :
 

with(Units[Natural]):
d := 1*Unit(degrees): 
cos(45*d); 
sin(90*d);

 

Hi, 

Here is a way you maybe consider as more elegant (but I rarely use it and I prefer doing what you do yourself, that is generating uniform random numbers and "inverting" an empirical cdf...).

The UseHardwareFloats := false: instruction is usefull to convert S into a sequence of integers (look the Sample help page).
What I do not like hereare the multiple conversions this solution needs (use of round and conversion as list). I let you judge this by yourself
 

restart

with(Statistics):

Probs := [0.10, 0.30, 0.20, 0.25, 0.15]:
Bins  := [50, 60, 70, 80, 90]:

X := RandomVariable(ProbabilityTable(Probs));

_R

(1)

UseHardwareFloats := false:

N := 4:
S := round~(Sample(X, Vector(N, datatype=float))):

Bins[convert(S, list)]

[80, 60, 60, 80]

(2)

 

 

Download A_way.mw

Here is another way where I use "EmpiricalDistribution" instead of "Probability Table"
Download Another_way.mw

Try to do a few prior simplifications, for instance by setting  a = 2*h/e0, b=t/(2*(-h^2-omega^2)), and so on.
Next integrate x*expand(E) instead of x*E
Apply combine(.. trig) tio the result
...
Is it enough for you ?


 

restart

with(LinearAlgebra):

``

# next set 2*h/e0 = a

E := e0*(1+2*a*(sinh('beta')+a)/(1-a*sinh('beta')));

# with

'a' = 2*h/e0

e0*(1+2*a*(sinh(beta)+a)/(1-a*sinh(beta)))

 

a = 2*h/e0

(1)

# now beta ...

beta := c*(x+b);


# with

'b' = t/(2*(-h^2-omega^2));

# and

'c' = e0*sqrt(1+a^2);

c*(x+b)

 

b = t/(-2*h^2-2*omega^2)

 

c = e0*(a^2+1)^(1/2)

(2)

iE0 := int(x*E, x)

-(1/2)*e0*x^2-4*e0*b*arctanh((1/2)*(2*exp(c*(x+b))*a-2)/(a^2+1)^(1/2))*a^2/(c*(a^2+1)^(1/2))-4*e0*b*arctanh((1/2)*(2*exp(c*(x+b))*a-2)/(a^2+1)^(1/2))/(c*(a^2+1)^(1/2))-2*e0*ln((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))*b*a^2/(c*(a^2+1)^(1/2))-2*e0*ln((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))*b/(c*(a^2+1)^(1/2))-2*e0*ln((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))*x*a^2/(c*(a^2+1)^(1/2))-2*e0*ln((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))*x/(c*(a^2+1)^(1/2))+2*e0*ln((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))*b*a^2/(c*(a^2+1)^(1/2))+2*e0*ln((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))*b/(c*(a^2+1)^(1/2))+2*e0*ln((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))*x*a^2/(c*(a^2+1)^(1/2))+2*e0*ln((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))*x/(c*(a^2+1)^(1/2))-2*e0*dilog((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))*a^2/(c^2*(a^2+1)^(1/2))-2*e0*dilog((-exp(c*(x+b))*a+(a^2+1)^(1/2)+1)/(1+(a^2+1)^(1/2)))/(c^2*(a^2+1)^(1/2))+2*e0*dilog((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))*a^2/(c^2*(a^2+1)^(1/2))+2*e0*dilog((exp(c*(x+b))*a+(a^2+1)^(1/2)-1)/(-1+(a^2+1)^(1/2)))/(c^2*(a^2+1)^(1/2))

(3)

iE := simplify(algsubs(1+a^2=A^2, iE0)) assuming A > 0

(1/2)*e0*(4*A*b*c*ln(1+A)+4*A*c*x*ln(1+A)-4*A*b*c*ln(-exp(c*(x+b))*a+A+1)-4*A*c*x*ln(-exp(c*(x+b))*a+A+1)-8*A*arctanh((exp(c*(x+b))*a-1)/A)*b*c+4*A*ln((exp(c*(x+b))*a+A-1)/(-1+A))*b*c+4*A*ln((exp(c*(x+b))*a+A-1)/(-1+A))*c*x-c^2*x^2-4*A*dilog((-exp(c*(x+b))*a+A+1)/(1+A))+4*A*dilog((exp(c*(x+b))*a+A-1)/(-1+A)))/c^2

(4)

map(collect, iE, [c, b])

(1/2)*e0*(-c^2*x^2+((4*A*ln(1+A)-4*A*ln(-exp(c*(x+b))*a+A+1)-8*A*arctanh((exp(c*(x+b))*a-1)/A)+4*A*ln((exp(c*(x+b))*a+A-1)/(-1+A)))*b+4*A*ln(1+A)*x-4*A*ln(-exp(c*(x+b))*a+A+1)*x+4*A*ln((exp(c*(x+b))*a+A-1)/(-1+A))*x)*c-4*A*dilog((-exp(c*(x+b))*a+A+1)/(1+A))+4*A*dilog((exp(c*(x+b))*a+A-1)/(-1+A)))/c^2

(5)

expand(E);
map(int, x*%,x)

e0+2*e0*a*sinh(c*b)*cosh(c*x)/(1-a*sinh(c*b)*cosh(c*x)-a*cosh(c*b)*sinh(c*x))+2*e0*a*cosh(c*b)*sinh(c*x)/(1-a*sinh(c*b)*cosh(c*x)-a*cosh(c*b)*sinh(c*x))+2*e0*a^2/(1-a*sinh(c*b)*cosh(c*x)-a*cosh(c*b)*sinh(c*x))

 

(1/2)*x^2*(e0*x+2*e0*sinh(c*b)*ln(tanh((1/2)*c*x)-1)/(c*(sinh(c*b)+cosh(c*b)))-2*e0*sinh(c*b)*ln(tanh((1/2)*c*x)+1)/(c*(sinh(c*b)-cosh(c*b)))-4*e0*sinh(c*b)^2*arctan((1/2)*(2*(sinh(c*b)*a+1)*tanh((1/2)*c*x)+2*cosh(c*b)*a)/(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2))/(c*(sinh(c*b)-cosh(c*b))*(sinh(c*b)+cosh(c*b))*(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2))+2*e0*cosh(c*b)*ln(tanh((1/2)*c*x)-1)/(c*(sinh(c*b)+cosh(c*b)))+2*e0*cosh(c*b)*ln(tanh((1/2)*c*x)+1)/(c*(sinh(c*b)-cosh(c*b)))+4*e0*cosh(c*b)^2*arctan((1/2)*(2*(sinh(c*b)*a+1)*tanh((1/2)*c*x)+2*cosh(c*b)*a)/(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2))/(c*(sinh(c*b)-cosh(c*b))*(sinh(c*b)+cosh(c*b))*(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2))-4*e0*a^2*arctan((1/2)*(2*(sinh(c*b)*a+1)*tanh((1/2)*c*x)+2*cosh(c*b)*a)/(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2))/(c*(sinh(c*b)^2*a^2-cosh(c*b)^2*a^2-1)^(1/2)))

(6)

map(combine, %, trig)

(1/2)*x^2*(-4*e0*a^2*arctan((tanh((1/2)*c*x)*sinh(c*b)*a+cosh(c*b)*a+tanh((1/2)*c*x))/(-a^2-1)^(1/2))+(-a^2-1)^(1/2)*c*e0*x-2*(-a^2-1)^(1/2)*ln(tanh((1/2)*c*x)+1)*e0+2*(-a^2-1)^(1/2)*ln(tanh((1/2)*c*x)-1)*e0-4*e0*arctan((tanh((1/2)*c*x)*sinh(c*b)*a+cosh(c*b)*a+tanh((1/2)*c*x))/(-a^2-1)^(1/2)))/(c*(-a^2-1)^(1/2))

(7)

 


 

Download Is_it_enough.mw

A solution among other 
`y'`

 

The simplest way to eliminate de O(...) term in a Taylor expansion (for instance) is to convert the result into polynom.
For instance
T := taylor(sin(t), x):
convert(T, 'polynom'):

Hi, 
I think you did something unnecessary complicated.
More of this generating 10000 subscribers on a single proc can be done in 11 to 12 seconds only.

 

restart:

with(Statistics):

n_draw      := 10000:
prior_rate  := Sample(Uniform(0, 1), n_draw):
n_trials    := 16:
subscribers := CodeTools:-Usage( Vector[row](n_draw, i -> Sample(Binomial(n_trials, prior_rate[i]), 1)[1]) ):

memory used=0.67GiB, alloc change=492.01MiB, cpu time=11.24s, real time=10.27s, gc time=1.61s

 

Histogram(subscribers, discrete=true, view=[-1..n_trials+1, default], thickness=10);

 

post_rate := map(i -> if subscribers[i]=6. then prior_rate[i] end if, [$1..n_draw]):
Histogram(post_rate, view=[0..1, default]);

 

 

 

Download subscribers.mw


But the true thing is that one can easily determine the exact distribution of post_rate:
 

restart:

with(Statistics):

n_draw      := 10000:

n_trials    := 16:

# One can prove subscribers is a discrete uniform random variable over {0, ..., 16}

X := RandomVariable(Binomial(16, p));
P := Probability(X=K);
S := int(P, p=0..1) assuming K >= 0;  # probability that "subscribers" take the value K

X := _R10004

 

P := piecewise(K = 0, (p-1)^16, K = 1, -16*p*(p-1)^15, K = 2, 120*p^2*(p-1)^14, K = 3, -560*p^3*(p-1)^13, K = 4, 1820*p^4*(p-1)^12, K = 5, -4368*p^5*(p-1)^11, K = 6, 8008*p^6*(p-1)^10, K = 7, -11440*p^7*(p-1)^9, K = 8, 12870*p^8*(p-1)^8, K = 9, -11440*p^9*(p-1)^7, K = 10, 8008*p^10*(p-1)^6, K = 11, -4368*p^11*(p-1)^5, K = 12, 1820*p^12*(p-1)^4, K = 13, -560*p^13*(p-1)^3, K = 14, 120*p^14*(p-1)^2, K = 15, -16*p^15*(p-1), K = 16, p^16, 0)

 

piecewise(K = 0, 1/17, K = 1, 1/17, K = 2, 1/17, K = 3, 1/17, K = 4, 1/17, K = 5, 1/17, K = 6, 1/17, K = 7, 1/17, K = 8, 1/17, K = 9, 1/17, K = 10, 1/17, K = 11, 1/17, K = 12, 1/17, K = 13, 1/17, K = 14, 1/17, K = 15, 1/17, K = 16, 1/17, 0)

(1)

post_rate := Probability(X=6);
plot(%, p=0..1)

8008*p^6*(1-p)^10

 

 

# If you really want to generate a sample of subscribers

subscribers := CodeTools:-Usage( LinearAlgebra:-RandomVector(n_draw, generator=0..n_trials) );
Histogram(subscribers, discrete=true, view=[-1..n_trials+1, default], thickness=10):

memory used=81.07KiB, alloc change=0 bytes, cpu time=1000.00us, real time=0ns, gc time=0ns

 

subscribers := Vector(4, {(1) = ` 1 .. 10000 `*Vector[column], (2) = `Data Type: `*anything, (3) = `Storage: `*rectangular, (4) = `Order: `*Fortran_order})

(2)

 


 

Download subscribers_exact.mw

@hajnayeb 

Hi again, 

This reply doesn't account for your last one (I had prepared mine and I was about to send it when I read yours).
So do not take it as a final answer to your problem but as a quick survey of what can be done.

PS: I did not use a white noise but a slightly colored one : the third argument in procedure KG (choosen to 0.1) is the correlation length of the random process [should be 0 for a true white noise but this choice will not do correct results with the attached file]).
 

restart:

with(LinearAlgebra):
with(plots):
with(Statistics):

# Step 1: compute the "impulse response function" (IRF) defined as the solution of
# the ode with a Dirac source term at time t.
# To simplify I assume here the ic for the original problem are x(0)=D(x)(0)=0.
#
# It can be shown this IRF is the solution of an homogeneous ode complemented with
# non homogeneous ic:

homogeneous_edo    := m*diff(x(t),t$2)+c*diff(x(t),t)+k*x(t)=0;
non_homogeneous_ic := x(0)=0, D(x)(0)=1/m;
IRF                := unapply( rhs(dsolve([homogeneous_edo, non_homogeneous_ic],x(t))), t);

m*(diff(diff(x(t), t), t))+c*(diff(x(t), t))+k*x(t) = 0

 

x(0) = 0, (D(x))(0) = 1/m

 

proc (t) options operator, arrow; exp((1/2)*(-c+(c^2-4*k*m)^(1/2))*t/m)/(c^2-4*k*m)^(1/2)-exp(-(1/2)*(c+(c^2-4*k*m)^(1/2))*t/m)/(c^2-4*k*m)^(1/2) end proc

(1)

# Step 2: Let F(t) the random excitation and let mu__F(t) its mean and R__F(t__1, t__2) it's
# autocorrelation function.
# Here are some results

# Response X(t) of the system under the random loading F(t)

X(t) = Int(F(tau)*'IRF'(t-tau), tau=-infinity..+infinity);

X(t) = Int(F(tau)*IRF(t-tau), tau = -infinity .. infinity)

(2)

# Mean value of the response

mu__X(t) = Int(mu__F(tau)*'IRF'(t-tau), tau=-infinity..+infinity);
mu__X(t) = Int(mu__F(t-tau)*'IRF'(tau), tau=-infinity..+infinity);  #equivalently

# if F(t) is stationnary so is X(t)

mu__X = mu__F*Int('IRF'(tau), tau=-infinity..+infinity);

mu__X(t) = Int(mu__F(tau)*IRF(t-tau), tau = -infinity .. infinity)

 

mu__X(t) = Int(mu__F(t-tau)*IRF(tau), tau = -infinity .. infinity)

 

mu__X = mu__F*(Int(IRF(tau), tau = -infinity .. infinity))

(3)

# Autocorrelation function of X(t)

R__X(t__1, t__2) = Int(Int(R__X(tau__1, tau__2)*'IRF'(t__1-tau__1)*'IRF'(t__2-tau__2), tau__1=-infinity..+infinity), tau__1=-infinity..+infinity);

R__X(t__1, t__2) = Int(Int(R__X(tau__1, tau__2)*IRF(t__1-tau__1)*IRF(t__2-tau__2), tau__1 = -infinity .. infinity), tau__1 = -infinity .. infinity)

(4)

# Spectral density S__X(xi)
# Let H(xi) the Fourier transform of IRF(tau) and S__F(xi) the spectral density of F(t) then

S__X(xi) = conjugate(H(xi))*H(xi)*S__F(xi);

S__X(xi) = conjugate(H(xi))*H(xi)*S__F(xi)

(5)

# How to draw trajectories: a notional example
# F(t) is a stationnary gaussian random process (look how its point realizations F are constructed),
# with gaussian correlation function (look the expression of the variable K in procedure KG)
# (be free to ask me all precisions you would need).

# Procedure KG generates a random realization of F(t).
# For each such realization there exist a trajectory X(t), solution of the ode with random
# excitation KG(...).
# Nb_traj=10 such trajectories are drawn


KG := proc(X, Y, psi, sigma)
  local NX, DX, K, mu, k, y:
  NX := numelems(X);
  DX := < seq(Vector[row](NX, X), k=1..NX) >^+:
  K  := (sigma, psi) -> evalf( sigma^2 *~ exp~(-((DX - DX^+) /~ psi)^~2) ):
  mu := add(Y) / NX;
  k  := (t, sigma, psi) -> evalf( convert(sigma^2 *~ exp~(-((t -~ X ) /~ psi)^~2), Vector[row]) ):
  y  := mu + k(t, sigma, psi) . (K(sigma, psi))^(-1) . convert((Y -~ mu), Vector[column]):
  return y
end proc:

NX := 10:
T  := [$1..NX]:

Nb_traj := 10:

for traj from 1 to Nb_traj do
  F  := convert( Statistics:-Sample(Normal(0, 1), NX), list):

  X := dsolve(
         {
           m*diff(x(t),t$2)+c*diff(x(t),t)+k*x(t)=KG(T, F, 0.1, 1), x(0)=1, D(x)(0)=0
         },
         numeric, parameters=[m, c, k]
       ):

  X(parameters=[1, 1, 1]):
  pl||traj := odeplot(X, [t, x(t)], t=0..10, color=ColorTools:-Color([rand()/10^12, rand()/10^12, rand()/10^12]))
end do:

display(seq(pl||traj, traj=1..Nb_traj))
 

 

 


 

Download RandomSourceTerm.mw

@Rouben Rostamian  @Earl

A slight simplification which avoids using the parallel axis theorem
Ic := int(int(((x-x__c)^2+y^2), y=-f(x)..f(x)), x=0..1);

And a funny (maybe?) different point of view 


 

restart;

with(Statistics):

f := sqrt(x^(2/3) - x^2)

(x^(2/3)-x^2)^(1/2)

(1)

# Normalization constant

C := int(x, x=0..1);

1/2

(2)

# identify f/C to the pdf of some random variable X

pdf := unapply(piecewise(x<0, 0, x < 1, f/C, 0), x);
X   := Distribution(PDF = pdf);

proc (x) options operator, arrow; piecewise(x < 0, 0, x < 1, 2*(x^(2/3)-x^2)^(1/2), 0) end proc

 

_m4548517376

(3)

# Note that the position of the center of mass doesn't change if you consider only
# the part of the lamina defined by y >=0.
# So, in Rouben's notations

x__c := Mean(X);
evalf(x__c);

(2/5)*GAMMA(3/4)^2*2^(1/2)/Pi^(1/2)

 

.4792560936

(4)

# The variance of X is equal to the inertia of the lamina around the vertical axis
# defined by x = x__c

I__x__c := Variance(X);
evalf(%)

(1/800)*(-256*GAMMA(3/4)^4+75*Pi^2)/Pi

 

0.6483790821e-1

(5)

# To obtain the inertia around the center of mass of the lamina, whose position is (x__c, 0)
# you need to add a term I__y__c wich corresponds to Rouben's int(int(y^2, y=-f(x)..f(x)), x=0..1).
# In the statistical framework int(y^2, y=-f(x)..f(x)) is the variance V__y(x) of a random variable
# uniformly distributed with support [-f(x), f(x)].
# This variance can be interpreted as a function of the random variable whose distribution is X.
# Takink its mean just gives the value of I__y__c


g       := unapply(f, x);
V__y    := eval(Variance(Uniform(-g(x), g(x))), x=RandomVariable(X)):
I__y__c := Mean(V__y);

proc (x) options operator, arrow; (x^(2/3)-x^2)^(1/2) end proc

 

(1/32)*Pi

(6)

I__c := I__x__c + I__y__c;
evalf(%);

(1/800)*(-256*GAMMA(3/4)^4+75*Pi^2)/Pi+(1/32)*Pi

 

.1630126786

(7)

 


 

Download A_statistical_point_of_view.mw

Hi, 

Customize the code below as you want

restart:

interface(rtablesize=10):

MyErrorPlot := proc(x, y, Ey, symb, symbsize, symbcol, errwidth, errcol)
  # symb     : symbol corresponding tocouples (x[i], y[i])
  # symbsize : its size
  # symbcol  : its color
  # errwidth : width of the horizontal bars
  # errcol   : color of the error bars
  plots:-display(
    Statistics:-ScatterPlot(x, y, symbol=symb, symbolsize=symbsize, color=symbcol),
    seq(plot([[x[n], y[n]-Ey[n]], [x[n], y[n]+Ey[n]]], color=errcol), n=1..numelems(x)),
    seq(plot([[x[n]-errwidth, y[n]-Ey[n]], [x[n]+errwidth, y[n]-Ey[n]]], color=errcol), n=1..numelems(x)),
    seq(plot([[x[n]-errwidth, y[n]+Ey[n]], [x[n]+errwidth, y[n]+Ey[n]]], color=errcol), n=1..numelems(x))
  )
end proc:

A := [$1..10]:
B := [seq(sqrt(i), i = 1..10)]:
C := [seq(i/10, i = 1..10)]:

MyErrorPlot(A, B, C, circle, 15, red, 0.2, green)

 

 


 

Download MyErrorPlot.mw

 

 

 

First 50 51 52 53 54 55 56 Last Page 52 of 65