tomleslie

13876 Reputation

20 Badges

15 years, 163 days

MaplePrimes Activity


These are replies submitted by tomleslie

@mmcdara 

These may be irrelevant, but I think you should be aware of a couple of points

First Issue

Much has been made by some respondents of the fact that you have to raise an a Matrix to an integer power, otherwise problems will arise. This appears to be true if you use P^n but this restriction does not apply if you use  the LinearAlgebra:-MatrixPower(P,n) command - so, for example, the following will work perfectly happily in Maple 2015

  restart;
  WM2:=proc( P, n)
              LinearAlgebra:-MatrixPower(P, n):
       end proc:
  U := Matrix(2$2, [0.8, 0.2, 0.4, 0.6]):
  WM2(U, 1);
  plots:-animate( plots:-matrixplot,
                  [ WM2(U,n),
                    heights=histogram
                  ],
                  n=1..

and you will get the animation for (the default 25 values) of n between 1 and 2. My only reason for restricting to integer powers was that this question arose from the earlier

https://www.mapleprimes.com/questions/232596-State-Transition-Diagram

where the only meaningful values of matrix powers are integers - true if you are considering Markov chain transitions, but not a restriction which applies to the LinearAlgebra-MatrixPower() command - for the latter, yiuo can pretty much raise to any (non-integer) power you want. So don't get hung up on the 'integer power' issue

 

Second Issue

There are pretty much always two ways to achieve an animation in Maple, either

plots:-animate( plotCommand, [plotArgs[including_something dependent on 'n']], animationVariable=animation range)

or

plots:-display( seq( plotCommand( plotArgs[including_something dependent on 'n'] ), n=animationRange), insequence=true)

In most circumstances these will give the same animation. However, I have occasionally come across cases where the first of these fails - usually for some rather "obscure" plotCommand. And you are correct in thinking that (at the moment) I can't come up with a specific case!

I don;t think I have ever come across a case where the second of the above options 'fails'. The two command options ought to be pretty much equivalent, but maybe you should bear in mind that (very occasionally) one will 'work' when the other just doesn't

@acer 

that the OP has a tendency to duplicate questions, not read/understand answers and never read Maple help, but in this case he has a serious point. As originally observed by user vv, anyone who thinks this isn't an issue with MAple is going to have to come up with a very good explanation for the following behaviour - becauase as I have said elsewhere on this thread it is a BUG. Feel free to convince me otherwise

restart;
type(x^a*y,  `&*`(identical(x^a), name));
type(x^2*y,  `&*`(identical(x^2), name));
                              true

                             false

 

 

@ 

If I strip this problem dowm to the basics, you have managed to come up with an "issue" I have never seen before. I'm going to ignore all aspects related to 2D math input because I think that just complicates the issue).

The interesting question is why the code in the attached (dig2.mw) gives two different results.

Major brownie points if you can spot the difference between the two execution groups in the code shown below. Obviously the answers are different - but why?

restart;
Digits := 1;
Digits := 5;
1.0/3;
                          Digits := 1

                          Digits := 5

                          0.3333333333

restart;
Digits := 1;
Digits := 5;
1.0/3;
                          Digits := 1

                          Digits := 5

                            0.33333

Now the answer appears to be that in the atttached worksheet, based on what the Maple toolbar "thinks"

  1. the first execution group is 'math' input
  2. the second execution group is 'text' input
  3. although if yu can spot the difference, you are better than I am

Obviouusly long experience with Maple has taught me never to use anything other than worksheet mode with text input, for the simple eason that every other combination "breaks" somewhere - but I would still be intersted in why this particular example has a problem

To solve the OP's immediate issue I can do nothing other than recommend that (s)he uses worskheet mode with 1-D input, which seems to be the least problematic of Maple moeds

When you are editing anything on this site, you should be able to see a toolbar which looks like the picture below.

The big green up-arrow is the third icon from the right on the top row

@ 

  1. Upload the worksheet which demonstrates this behaviour to this site using the big green up-arrow in the Mapleprimes toolbar
  2. Continue on your own to debug this problem

To encourage your cooperation check the worksheet attached (dig.mw) to show Maple working exactly as expected. This worksheet demonstrates the behaviour shown below

restart;
Digits := 1;
Digits := 5;
1.0/3
                          Digits := 1

                          Digits := 5

                            0.33333

 

@ 

illustrating this remaining  problem using the big green up-arrow in the Mapleprimes toolbar. As my previous response stated

  Digits:=1;
  Digits:=8;1.0/3;

definitely gives an answer to 8 digits, so I'd like to very carefully examine any worksheet where it doesn't!

previous responses carefully, and consider the following code

#
# OP has no problem with this
#
  Digits:=1;
  Digits:=8;
  1.0/3;
                          Digits := 1

                          Digits := 8

                           0.33333333

#
# OP *claims* that this evaluates to 1 digit
# which is obviously untrue
#
  Digits:=1;
  Digits:=8;1.0/3;
                          Digits := 1

                          Digits := 8

                           0.33333333

  Digits := 1;
  a := Digits;
  a := a + 7;
#
# In the following expression Maple's automatic
# simplification will simplify 1.0/3, using the
# currently active setting of Digits, which is 1.
#
# so 1.0/3 will "simplify" to 0.3
#
# Thus evalf[a](1/0.3) will become evalf[8](0.3)
# which will return 0.3
#
  evalf[a](1.0/3);
  evalf[8](0.3);
                          Digits := 1

                             a := 1

                             a := 8

                              0.3

                              0.3

 

@Anthrazit 

there are so many ways to do this, that the question doesn't make much sense unless one knows exactly the output format which you want. Try any of the following nine possibilities

[entries(x, pairs)];
for j in indices(x, nolist) do
    [j, x[j]];
end do;
for j in indices(x, nolist) do
    j, x[j];
end do;
for j in indices(x) do
    j[], x[j[]];
end do;
for j in indices(x) do
    [j[], x[j[]]];
end do;
for j in indices(x, nolist) do
    [j = x[j]];
end do;
for j in indices(x, nolist) do
    j = x[j];
end do;
for j in indices(x) do
    j[] = x[j[]];
end do;
for j in indices(x) do
    [j[] = x[j[]]];
end do;

 

probably have posted this as evidence

bigMat.mw

@vv 

just fine on Maple 2021 running on Windows 7,. The A^2 command takes about 3secs on my machine. Obviously difficult to check the result, but if I just randomly select A[1000..1000, 1500..1508], then everything *looks* plausible

is so tangled, that I cannot understand (most) of it.

The first complication is all the packages which you load, some of which should never be loaded together - eg linalg() has been deprecated and replaced by LinearAlgebra(), so why load both? Similarly I doubt if you need both LinearAlgebra() and Student[LinearAlgebra] (). The only reason for loading the latter appear to be to access the command PlanePlot(), but as I pointed out in my previous answer there is no way to access intermediate results from this command even with infolevel=1 - so why bother. The geom3d() package is loaded, but as far as I can tell no commands from this pakage are ever used - so why load it?

Your method of calculating spacecurves on a sphere of radius 1, is so convoluted that I cannot make sense of it. The sensible way to do this is to realise that the transformation between cartesian and spherical polars is given by

x=r*cos(theta)*sin(phi)
y=r*sin(theta)*sin(phi)
z=r*cos(phi)

In your case, r=1, and the angles theta and phi are functions of a single parameter say theta(t) and phi(t), so the spaccurve can be defined as

spacecurve( [ cos( theta(t) )*sin( phi(t) ), sin( theta(t) )*sin( phi(t) ), cos( phi(t) )],  t=startValue..stopValue)

The attached worksheet ( planeProb2.mw ), containing the code below

  restart:
#
# Define a procedure which accepts the equation
# of a plane in the form A*x+B*y+C*z=0 where A,
# B, C are numeric.
#
# The procedure returns six quantities which are
#
# 1. basis vector
# 2. the other basis vector
# 3. the normal vector
# 4. the plot of 1,2,3 above
# 5. the plot of the plane
# 6. the plot of a unit sphere
#
  getVecs:= proc( poly )
                  uses LinearAlgebra, VectorCalculus, plots, plottools:
                  local v1n, v2n, v3n, plt1, plt2, plt3;
                #
                # Get the orthonormal basis for the supplied plane
                #
                  v1n, v2n:= GramSchmidt
                             ( [ Vector
                                 ( [ coeff(lhs(poly), y)/coeff(lhs(poly), x),
                                     -1,
                                      0
                                   ]
                                 ),
                                 Vector( [ coeff(lhs(poly), z)/coeff(lhs(poly), x),
                                           0,
                                           -1
                                         ]
                                       )
                               ],
                               normalized=true
                             )[];
                #
                # get the (normalized) normal vector to the plane
                #
                  v3n:= Normalize
                        ( CrossProduct
                          ( v1n,
                            v2n
                          ),
                          Euclidean
                        );
                #
                # Produce three plots which are
                #
                # 1. The orthnormal basis vectors and the normal
                #    vector
                # 2. The plane
                # 3. A sphere of radius 1 centered at the origin
                #
                  plt1, plt2, plt3:= PlotVector
                                     ( [v1n, v2n, v3n],
                                       color=[blue, blue, black]
                                     ),
                                     implicitplot3d
                                     ( p1,
                                       x=-1..1,
                                       y=-1..1,
                                       z=-1..1,
                                       style=surface,
                                       color=red
                                     ),
                                     sphere
                                     ( [0,0,0],
                                       1,
                                       color=white,
                                       transparency=0.5
                                     );
                  return v1n, v2n, v3n, plt1, plt2, plt3;
            end proc:
#
# Equation of the plane (from OP's post)
#
  p1:=-.2497*x+.7343e-1*y+1.*z= 0;
#
# Return all the required answers as a list
#
# ans[1..2] are the orthormal basis
# ans[3] is the normal vector
# ans[4] is the plot of ans[1..3]
# ans[5] is the plot of the plane
# ans[6] is the plot of the sphere
#
  ans:= [getVecs(p1)]:
#
# Show the vectors (first two are the basis,
# third one is the normal
#
  ans[1..3][];
#
# Show all the plots in one Figure alon with
# the plot of an *arbitrary* spacecurve on
# the sphere, defined in terms of a single
# parameter
#
  plots:-display
         ( [ ans[4..6][],
             plots:-spacecurve
                    ( [ cos(t)*sin(t/2+Pi/4),
                        sin(t)*sin(t/2+Pi/4),
                        cos(t/2+Pi/4)
                      ],
                      t=0..Pi,
                      scaling=constrained,
                      color=green,
                      thickness=4
                    )
           ],
           axes=normal
         );

produces

  1. the orthonormal basis vectors for the plane
  2. the vector normal to the plane,
  3. the plot below, of the above vectors, the plane, the ubit sphere and an *arbitrary* spacecurve (because I culd figure out from your code whihc spacecurve you wanted

You should also be aware that there are an infinite number of pairs of (orthonormal) basis vectors. If you have one pair of basis vectors, then these can be rotated by an arbitrary angle about the axis defined by the normal vector. After such an arbitrary rotation they will still be orthonormal basis vectors for the plane. Similarly there are two normal vectors - think 'up' and 'down'

In any future response please use the bit green up-arrow in the Mapleprimes toolbar to upload  your worksheet - it makes life so much easier for any responders here.

 

 

@nm 

Maybe you should read the answer

select() looks at the operands of the expression - never the whole expression.

So if you write select(someBoolean, sin(x)*cos(x) ) then the function xomeBoolean will be applied to sin(x) and then to cos(x), biut never to sin(x)*cos(x)

@Fateh 

This site is not here to heklp you with the fact that you do not understand your own problem

It is here to help people who have at least attempted to solve their own  problem using Maple - something you seem remarkably reluctant to do.

My previous worksheet shows how to calculate values of 'pmax' for a given value of 'k'. If you wish to plot 'pmax' versus 'k' then why don't you produce a worksheet to do it? I can think of two reasons - laziness and/or stupidity. This site does not exist to correct with either of these issues.

Now just in case you think I don't know how to graph 'pmax' versus 'k', I spent about 5 minutes with my previous worksheet and came up with the graph below - the shape looks *roughly* like those in figure 4 in the pdf provided for reference, although the vertical scale is wrong. I have no interest in reading your reference to find out why.

 

@Joe Riel 

I'm a retired IC designer. So the first ten years of my working life involved writing/debugging/analysing spice netlists. One of the bigeest productivity boosts I ever had was around 1990 when I got access to decent shcematic capture tools.

I don't think I ever again typed a netlist until I retired - so doing it for this problem gave me a warm nostalgic glow !

@Fateh 
You asked (1)

explain the purpose of the command  

rhs(sol[j](0.5)[3])

 

In the code I supplied, I solve your ode for different entries of values n the list 'params' by looping through the entries in the list [0.25, 0.3, 0.4, 0.5, 0.7], for values of 'N', while holding 'M' and 'K' fixed at 0.2 and 2 respectively. The solution for each value of the loop index 'j' is assigned to sol[j] - so for example sol[4] is the solution corresponding to the parameter list [M=0.2, N=0.5, k=2].

What the dsolve() command returns is actually a procedure, to which one can supply an argument and obtain values - so for example sol[4](0.5) will return a list of values for your system when x=0.5. If you try this in my worksheet, it will return

[x = 0.5, p(x) = 0.108665365029661, lambda = 0.658681811222779]

In other words the value of 'x', the value of 'p(x)' (with x=0.5) and the value of lambda. sol[4](0.5)[3] will return the third entry of this list, which is lambda = 0.658681811222779; rhs(sol[4](0.5)[3]) just takes the right-hand-side of the supplied expression so will return 0.658681811222779. Thus

rhs(sol[j](0.5)[3])

is just a "short" way to get the value of lambda from the j-th solution, for the purposes of printing. Note that there is no particular reason for uing the value x= 0.5 in the above - any value between 0.0 and 1.0 would give the same answer, since lambda is constant for each solution. The value of lambda varies (slightly) when one changes the parameter set, so the command

 seq( printf( "%12.8f  %12.8f\n", par[j], rhs(sol[j](0.5)[3])), j=1..numelems(par));

prints out the j-th value of list [0.25, 0.3, 0.4, 0.5, 0.7], which is being used for 'N' and the corresponding value of lambda from the j-th solution

 

You asked (2)

How could i change my method from BVP to rk45?

You can't! Runge-Kutta methods such as rk45, can only be used on initial value problems; you have a boundary value problem for which differeent solution methods are necessary. It is generally possible to convert a boundary value problem to an initial value problem using something called the "shooting method", but there seems little point adding this extra complexity when Maple's BVP solution methods are working well

 

You asked (3)

I want to solve the given first order ode with mentioned boundary condition for the expression of p(x) then I need to plot p(x) for the different involved parameters.

This is exactly what my previous worksheet does, it uses the parameter lists

[M=0.2, N=0.25, k=2]
[M=0.2, N=0.3,   k=2]
[M=0.2, N=0.4,   k=2]
[M=0.2, N=0.5,   k=2]
[M=0.2, N=0.7,   k=2]

plots the curve p(x) for each of the above parameter lists, and prints the value of lambda. Obviously you can use any lists of parameters which you want. The above was just an arbitrary choice on my part

You asked (4)

I want to calculate the expression and table for lambda for different values of involved parameters by fixing some and changes one.

Since everyting is being calculated numerically you will not be able to obtain an "expression" for lambda. In the worksheet I supplied previously, you did get a "table" of lambda . So you will always be able to calculate a value for lambda for any supplied parameter list, ie any given value for M, N, k

You asked (5)

next step is to find the maximum value of p(x) and the point where it is maximum then the plot for pmax?

This one is interesting! It is actually quite trivial to obtain the maximum value of p(x) and then produce plots of p(x)/max(p(x)), whic is what *appears* to be plotted in Figure 2 of the pdf whixh you supplied. When I did this for the parameter sets listed above - the curves came out as identical. In other words the parameter N is basically a "scale factor" whose effect is completely cancelled when one rescales the curves using the appropriate max(p(x)) value.

For this reason in the attached new worksheet,I fixed the parameters 'M',  'N' and varied 'k' instead, so it produces outputs for the combinations

[M=0.2, N=0.4,   k=1.0]
[M=0.2, N=0.4,   k=2.0]
[M=0.2, N=0.4,   k=3.0]
[M=0.2, N=0.4,   k=4.0]
[M=0.2, N=0.4,   k=5.0]

This now produces different plots of p(x)/max(p(x)) for each parameter set - and they look (roughly!) like the curves in Figure 2 of the supplied pdf

You asked (6)

At the last again want to integrate p(x) from 0 to 1 and generate a table for load for different values of the involved parameter.

Not too difficult to produce these integrals, although I'm uncertain whether you want to produce the integral of p(x) or the integral of p(x)/max(p(x)), so the  following code and the attached worksheet odeProb3.mw does both

 

  restart;
  h:= k - (k - 1)*x;
  DE:= diff(p(x), x)
       = -2*M^2
             *
          ( -exp(-M*h/sqrt(N))*exp(M*h/sqrt(N))*sqrt(N)*cosh(M*h/sqrt(N))
            + M*sinh(M*h/sqrt(N))*lambda + exp(-M*h/sqrt(N))*exp(M*h/sqrt(N))*sqrt(N)
          )
          /
          (   exp(-M*h/sqrt(N))
            *
              (   M*exp(2*M*h/sqrt(N))*h 
                - 2*exp(M*h/sqrt(N))*sqrt(N)*cosh(M*h/sqrt(N))
                - M*h + 4*exp(M*h/sqrt(N))*sqrt(N)
                - sqrt(N)*exp(2*M*h/sqrt(N))
                - sqrt(N)
              )
          );
  BC := p(0) = 0, p(1) = 0;
#  par:=[0.25, 0.3, 0.4, 0.5, 0.7];
  par:= [ 2.0, 3.0, 4.0, 5.0, 6.0];
  colors:=[red, blue, green, cyan, black]:
  for j from 1 by 1 to numelems(par) do
      params := [M = 0.2, N = 0.4, k =par[j]];
    #
    # Solve the ode for parameter values in the
    # list called params above
    #
      sol[j] := dsolve
                ( eval
                  ( {BC, DE},
                    params
                  ),
                  numeric,
                  abserr=1e-03
                );
    #
    # Compute the x-value at which the maximum
    # value of p(x) occurs
    #
      xmax[j]:= fsolve
                ( evalf
                  ( rhs
                    ( eval
                      ( DE,
                        [ [M = 0.2, N = 0.4, k =par[j]][], sol[j](0.5)[3]]
                      )
                    )
                  ),
                  x=0..1
                );
    #
    # At the obtained x-value above, determine what
    # the maximum value of p(x) actually is
    #
      pmax[j]:= rhs(sol[j](xmax[j])[2]);
    #
    # Plot p(x) versus x
    #
      plt1[j]:= plots:-odeplot
                       ( sol[j],
                         [x, p(x)],
                         x = 0 .. 1,
                         color=colors[j]
                       );
   #
   # Plot p(x)/max(p(x)) versus x
   #
      plt2[j]:= plots:-odeplot
                       ( sol[j],
                         [x, p(x)/pmax[j] ],
                         x = 0 .. 1,
                         color=colors[j]
                       );

  od:
#
# Display all plots of p(x) versus x with some
# annotations
#
  plots:-display( seq
                  ( plt1[j],
                    j=1..numelems(par)
                  ),
                  title=typeset( p(x), "versus ", x),
                  titlefont=[times, bold, 16],
                  legend=[ seq
                           ( typeset("k= ", par[j]),
                             j=1..numelems(par)
                           )
                         ]
                ); 
#
# Display all plots of p(x)/max(p(x)) versus x with some
# annotations
#                       
  plots:-display( seq
                  ( plt2[j],
                    j=1..numelems(par)
                  ),
                  title=typeset( p(x)/pmax, "versus ", x),
                  titlefont=[times, bold, 16],
                  legend=[ seq
                           ( typeset("k= ", par[j]),
                             j=1..numelems(par)
                           )
                         ]
                );
#
# Print an output table of lambda as k varies
#
  printf( "\n\n%12s  %12.8s\n", "k", "lambda");
  seq( printf( "%12.8f  %12.8f\n", par[j], rhs(sol[j](0.5)[3])), j=1..numelems(par));
#
# Print an output table of the integral of p(x) and
# the integral of P(x)/max(p(x)) as k varies
#
  with( Student[Calculus1] ):
  printf( "\n\n%8s       %12s     %20s\n",
          "k",
          "Int( p(x) )",
          "Int( p(x)/max(p(x)) )"
         );
  
  f1:= proc(x, j)
            if   type(x, numeric)
            then return rhs(sol[j](x)[2]);
            else return 'procname(_passed)';
            fi;
       end proc:
  f2:= proc(x, j)
            if   type(x, numeric)
            then return rhs(sol[j](x)[2])/pmax[j];
            else return 'procname(_passed)';
            fi:
       end proc:
  seq( printf( "%12.8f   %12.8f        %12.8f\n",
               par[j],
               ApproximateInt( f1(x, j), x=0..1, partition=99, output=value),
               ApproximateInt( f2(x, j), x=0..1, partition=99, output=value)
             ),
       j=1..numelems(par)
     );

This produces the graphs

and the tables

           k               lambda
  2.00000000    0.65672680
  3.00000000    0.73526777
  4.00000000    0.78216130
  5.00000000    0.81286188
  6.00000000    0.83448106

       k                   Int( p(x) )      Int( p(x)/max(p(x)) )
  2.00000000     0.06184010          0.63537105
  3.00000000     0.05711187          0.59111658
  4.00000000     0.04762111          0.55113440
  5.00000000     0.03954397          0.51673786
  6.00000000     0.03322128          0.48724002

 

 

 

 

 

 

 

 

First 31 32 33 34 35 36 37 Last Page 33 of 207