tomleslie

13841 Reputation

20 Badges

15 years, 1 days

MaplePrimes Activity


These are replies submitted by tomleslie

@AHSAN 

will be my last contribution to this thread. Mapleprimes is meant to be a site for fixing problems, not for general teaching purposes, or somewhere posters can have theor code written for them.

The attached worksheet, for one particular set  of parameters (beta, k, wi, ), shows a graph of the analytic solution superimposed on a graph of the numeric solution. In addition, you have result matrices for both and an error matrix.

Since you given me no idea what the appropriate values of lambda, and Q should be, I used an optimizer to determine values which minimized the error between the numeric and analytic solutions.

odeplots6.mw

@nahid200 

BM2.mw  I presented previously, you can change the entry

 In:-CubicInterpolation(t, W), convert(At:-Concatenate(1, t, W), Matrix)^%T:

to

 In:-SplineInterpolation(t, W), convert(At:-Concatenate(1, t, W), Matrix)^%T:

or

 In:-LinearInterpolation(t, W), convert(At:-Concatenate(1, t, W), Matrix)^%T:

to change the interpolation methd. I don't care, and neither do you, because

  1. you don't even know what the difference is, and
  2. for this applicaton you won't be able to see any difference.

You now appear to want to implement some different code from a textbook. Why don't you just ignore all the code which has been presented on this site and type in the textbook code, if that is what you actually want?  Or do you expect me to do it for you?

@AHSAN 

but since pretty much every execution group in it generates an error, some of the calculations appear illogical and I do not understand what algorithm you are trying to implement, there is very little I can do with it

My best suggestion: you state that you have " solved this non linear ode manually". Why don't yu just type this manual solution (not its derivation) into Maple? It can then be evaluated for the appropriate parameter values and compared with the numerical solutions

that I'm going to regret this contribution.

First of all I agree with Carl - I can't really see how you can have two independent variables controlling Brownian motion.

The independent variable in 1-D Brownian motion is best thought of as "time". Using this time (and a random process) one computes the "distance"from the starting point. Since the problem is one-dimensional, you can think of this "distance" as being along the x-axis, the y-axis or any other (one-dimensional) line. So, for example, you could interpret this "distance" as being "position" along the x-axis, or position along the y-axis

Since the random process involved is discrete,  the "distance" function is only computed at discrete time points - so in general you cannot obtain a value for "distance" for arbitrary time values, only at discrete time points. However if you combine th discrete random process with an interpolaton function, then you can obtain a value for "distance" at any time

The independent variable in 2-D Brownian motion is still best thought of as "time". Using this time (and two independent discrete random processes) one computes two independent "distance" functions. The only connection between these distance functions is that they are computed at the same time-values.  The obvious interpretation for these two independent distancce functions,  is then distance along the x-axis and distance along the y-axis or [x-position, y-position]

Again, since the random processes involved are discrete,  the "distance" functions are only computed at discrete time points - so in general you cannot obtain values of these "distance" functions for arbitrary time values, only at discrete time points. However if you combine each discrete random process with an interpolaton function, then you can obtain a values for both "distance" functions at any time.

Essentiallly the 2D problem is just two instances of the 1D problem and can use pretty much the same code, which I have done in the attached. I have made a few changes ot the underlying 1D code - it now returns the complete [time, distance] matrix as well as the interpolation object. This is mainly for convenience when plotting. Since there is now need to use the interpolation object when plotting, it does't really cost much to use a cubic interpolator instead of a linear one, so I have changed this as well.

  restart:

#
# Preliminaries
#
  interface(version);
  Wiener1D:= proc( T::range(realcons), N::And(posint, Not(1)), W0::realcons:= 0)
                   description "Discrete simulation of 1D Wiener process":
                   uses In= Interpolation, St= Statistics, At=ArrayTools:
                   local  a:= lhs(T), b:= rhs(T), dt:= evalf((b-a)/(N-1)),
                          t:= rtable(1..N, i-> a+(i-1)*dt, datatype= hfloat),
                          W:= Array(evalf(W0) +~ St:-CumulativeSum(St:-Sample(Normal(0, sqrt(dt)), N))):
                   W[1]:= W0:
                   In:-CubicInterpolation(t, W), convert(At:-Concatenate(1, t, W), Matrix)^%T:
             end proc:
  randomize():

`Standard Worksheet Interface, Maple 2021.2, Windows 7, November 23 2021 Build ID 1576349`

(1)

#
# 1-D Brownian motion
#
  B, pldata:=Wiener1D(0..2, 10^5):
#
# Example: value of distance at time=0.7
#
  B(0.7);
#
# Plot distance as a function of time
#
  plots:-pointplot( pldata,
                    style=line,
                    color=red,
                    axes=boxed,
                    labels=["time", "distance"],
                    labelfont=[times, bold, 14]
                 );

HFloat(-0.308990478515625)

 

 

#
# 2-D Brownian motion
#
  RW:= (a,b)->[Wiener1D(a, b), Wiener1D(a, b)]:
  RW:= RW(0..2, 10000):
#
# Example: value of [x-distance, y-distance] at time=0.7
#
  B:=[ RW[1], RW[3]]:
  B~(0.7);
#
# Plot x-distance as a function of time
#
  plots:-pointplot( RW[2],
                    style=line,
                    color=red,
                    axes=boxed,
                    labels=["time", "x-distance"],
                    labelfont=[times, bold, 14]
                  );
#
# Plot y-distance as a function of time
#
  plots:-pointplot( RW[4],
                    style=line,
                    color=red,
                    axes=boxed,
                    labels=["time", "y-distance"],
                    labelfont=[times, bold, 14]
                  );
#
# Plot [x-distance, y-distance]
#
  plots:-pointplot( <RW[2][..,2]| RW[4][..,2]>,
                    style=line,
                    color=red,
                    axes=boxed,
                    labels=["x-distance", "y-distance"],
                    labelfont=[times, bold, 14]
                  );
  
 

[HFloat(2.254360616207123), HFloat(-0.8136270344257355)]

 

 

 

 

 

 

Download BM2.mw

 

@mmcdara 

It's just so much "neater" than any other proposed solution

But - I guess it depends on how "safe" you feel using undocumented capabilities: if it is documented, then it pretty much has to be supported for the future; and if it is undocumented, the feature *might* disappear, and you would have no right to complain :-(

@nahid200 

The ArrayTools:-ScanAlongDimension() was only included in Maple 2021. The closest alternative which works in Maple 2020 is (probably) Statistics:-CumulativeSum().

See the attached, running in Maple 2020

  restart;
  interface(version);
  Wiener1D:= proc( T::range(realcons), N::And(posint, Not(1)), W0::realcons:= 0)
                   description "Discrete simulation of 1D Wiener process":
                   uses In= Interpolation, St= Statistics:
                   local  a:= lhs(T), b:= rhs(T), dt:= evalf((b-a)/(N-1)),
                          t:= rtable(1..N, i-> a+(i-1)*dt, datatype= hfloat),
                          W:= Array(evalf(W0) +~ St:-CumulativeSum(St:-Sample(Normal(0, sqrt(dt)), N))):
                   W[1]:= W0:
                   In:-LinearInterpolation(t, W):
             end proc:
  randomize():
  B:= Wiener1D(0..2, 10^5):
  B(0.2);
  plot(B, 0..2, numpoints= 1000);

`Standard Worksheet Interface, Maple 2020.2, Windows 7, November 11 2020 Build ID 1502365`

 

HFloat(0.7321040049063195)

 

 

 

Download BM.mw

@nahid200 

You can calculate a function B(t) for any value of the argeument 't', provided that B(t) is defined for that value off 't'

As I keep repeating, and you keep failing to understand, if you define a function B(t) only at certain values of the argument 't', then you cannot calculate the va;ue of B(t) at some (other) value

If you really need a simple example, I'm going to define some values of the function 'f()' as f(1)=234, f(2)=1.5e-27, f(3)=8.9e23.

Now you tell me what the value of f (17) is

If you think this is a stupid question, it is the one that you keep asking witholut realising it is a stupid question!

@mmcdara 

since I posted my original response - something about this has started to nag at me. Aliases substitute "literally" without regard to values of expressions. Maybe what I have suggested could be incorrect: for example if you asked for (say) 100 sampled of the random variable A - would you get 100 vallues from the same distirbution or 1 value from each of 100 identical distributions? I'm not evan sure how to demonstrate the difference between these two possibilities (if one exists). However the distiction bothers me (and yes I'm crap at statistics).

Since one can never get the alias() command to evaluate its arguments - the following (although clunkier) might be a "better" soltuon for your requirement

restart:
with(Statistics):
A = RandomVariable(Uniform(0, 1));
alias(%):
B = RandomVariable(Uniform(0, 2));
alias(%):
C = RandomVariable(Uniform(1, 2));
alias(%):
F :=  (A+2*B)/C;
Mean(F);

 

@nahid200 

You have now specified the values of an undefined function ( 'W()' ) for the arguments 0,1,2,3,4,5,6 and by some unspecified process you wish to know the values of the function at 0, 0.2, 0.4, 0.6, 0.5 , 1. What mathematical process  do you imagine can do this??

It is ignorant nonsense!

@nahid200 

Maple distinguishes between square brackets ( ie '[]' ) and round brackets (ie '()' ) - they mean two entirely different things!T

The for loop in the above code constructs a Maple table 'W' with indices running from 1 to N (ie 500). Each entry in this table is defined in terms of  an unknown table value W[0]. Having geneated this table, you can access any any of its entries, by supplying the appropriate index value - for example W[17], W[123] or W[456] - but this table is not subsequently used by your code

Your code also uses the function definitions W(0) and W(x) which of course bear no relation to the table entries defined above. Given that the function W() is defined nowhere in your code, what would do you expect the value of, say, W(-0.2154646060) to be?

It is genenerally considered rather risky to habe both an indexable and a non-indexable quantity with the same name in the same worksheet - it can cause confusion (and occasionally) hard-to-spot errors

@AHSAN 

In the attached I have modified my previous code so that the range of the plots exactly corresponds with the range of the boundary conditions in the ODE system - this means that iof you set x=0, then plot and output matrix ranges will be y=-1..1. If you set x=0.5, then the plot and matrix ranges will be y=-1,125..1.125. Feel free to set any value for 'x' which you desire - the plot and matrix ranges will adjust automatically

I have added the capability to set the number of points in the range of 'y'  If you don't set this as a final argument, then the default is to produce 21 values in the output matrix. The fifth example in the attached is the dame as the fourth, except that it producs 41 points in the output matrix, rather than 21.

Since the attached will produce pretty much and graph and table you desire then comparing them with "analytic" solutions would be trivial. Since you do not provide these "analytic" solutions, I have no idea what their format mght be, and hence cannot provide any code to perform the comparison.

odeplots5.mw

@nahid200 

what your original question ( ie i need code to calculate brownian motion in maple ) means!!!

It now appear that you have a simple expression   f=tanh(arctanh(1/10)+B*1/30)  - and for supplied values of  'f' you need to find the corresponding value of 'B', which is trivially achieved in the following

  restart:
  eqn:= f=tanh(arctanh(1/10)+B*1/30):
  fVals:= [0.10000000, 0.09288468, 0.07774290, 0.08661213, 0.10784523, 0.09521178]:
  seq( evalf(rhs(isolate(eqn, B))), f in fVals);

for which the output is

0., -0.215464606, -0.673057569, -0.405169781, 0.237927774, -0.145028561

What exactly is difficult about this?

What the hell does it have to do with Browninan motion?

When you have a simple question, ask it simply! Don't try to be clever, you will only confuse yourself and (possibly?) anyone responding

@nahid200 

is given by the code

  restart:
  randomize():
  numpoints:=100000:
#
# Decision time! Is the "do not move" option allowed?
# If it is then set allowNoMove to true, otherwise
# set it to false
#
  allowNoMove:=true:
  if   allowNoMove
  then moves:=[-1, 0, 1]:
  else moves:=[-1, 1]:
  fi:
  r:=rand(1..numelems(moves)):
  M:=Matrix(numpoints, 2):
  M[1,..]:= <1, 3>:
  for j from 2 to numpoints do
      M[j, ..]:=<j, M[j-1,2]+moves[r()]>:
  od:
  plots:-pointplot(M, style=line);

which produces the plot below (although yours will differ because I'm using the randomize() command to change the seed for the random process)

walk1D.mw

 

 

@AHSAN 

You state

In My first question i upload the  graph of my analytical solution in maple worksheet and in which i draw the graph at the value of k=0.1,0,3,0.5,07,0.9 so i am interested to compare my analytical solution with numeric graph at the same points. 

whilst it is true that the the figure in this worksheet is for k=0.1,0,3,0.5,07,0.9, the textual comment in the same worksheet states (emphasis added)

The purpose of my problem is to check the effect of  k=0.1,0.2,0.3,,,,,,0.9 and fixed wi=0.1 and then check the effect of wi=0.1,0.2...0.9 and fixed k=0.1 on velocity  at x=0. Again repeat the step for k and wi at x=0.5.

Am I suppposed to guess which of the two options you actually want?

You state

secondly i upload a worksheet of FDM algorithm and in question statemnet i clealry mentioned that the algorithem is working with adjusting thr range of y but i am interested to fixed the range of y=-1..1. 

This makes no sense. Your algorithm generates a list of points - you cannot plot a list of points over a range, all you can do is plot the list of points

The attached puts the relevant calculation into a procedure. which accepts three arguments, either

  1. a list of values for 'wi'
  2. a value for 'k'
  3. a value for 'x'

or

  1. a value for wi
  2. a list of values for k
  3. a value for x

The procedure returns a matrix of numerical value with appropriate row and column headers, and a list of plots for use with the display() command. The final execution group in this worksheet shows several examples of usage which are

  1. wi=0.1, k=0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, x=0.0
  2. wi=0.1  k=0.1, 0.3, 0.5, 0.7, 0.9,  x=0.0
  3. wi=0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,  k=0.1, x=0.5
  4. wi=0.1, 0.3, 0.5, 0.7, 0.8, 0.9,   k=0.1 x=0.5,
  5. wi=0.1,   k=0.3 ,  x=0.5
  6. A lot of values for k: I chose wi=0.1, k=0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, x=0.5. Note that since only ten plot colors are defined and there are more than 10 curves to be plotted, the dolors will be reused
  7. Some more or less random values: I chose wi=0.3, ki= 0.1, 0.2, 0.9, x=0.25

odeplots4.mw

 

@AHSAN 

you asked a question and got the perfect;ly functional answer in  odeplots.mw

Then you changed the question, because you decided that the original answer is inadequate , so you got  a new perfect;ly functional answer in  odeplots2.mw

Then you changed the question again, because you decided that this latest answer is inadequate  and you got (yet another) perfectly functional answer in  odeplots3.mw  

Now you change the question again, because you decided that t heprevious (perfectly functional) answer are inadequate in some way

Can you actually recognise the pattern in this process?

Every time you ask a question, you get an answer.

And then you decide - errrr no that wasn't the question you meant - you need the answer to a different question.

Free advice: if you are incapable of accurately speciffying your question - your chances of getting an answer are close to nil.

So for me you have one more chance: now what is the question?

First 21 22 23 24 25 26 27 Last Page 23 of 207