tomleslie

13876 Reputation

20 Badges

15 years, 175 days

MaplePrimes Activity


These are answers submitted by tomleslie

The test file I have used for this exercise is a braindead, simple 'csv' containing

2,4
4,16
6,36
8,64
10,100

This data is stored on my machine at C:/Users/TomLeslie/Desktop/testdat.csv. You will need to change this fllepath/name to something appropriate for your machine before executing the attached

  restart;
#
# Default storage format for a 'csv' file seems to
# be a dataframe
#
  mydat := Import("C:/Users/TomLeslie/Desktop/testdat.csv"):
#
# Plotting the data with 'dataplot' seems to plot
# all columns versus the row 'index', which is a
# pain if you want to use column1 as x-values and
# column2 as y-values
#
  dataplot(mydat);
#
# But by playing some tricks, it can be done!
#
  plots:-pointplot(convert(mydat, Matrix), style=line);
#
# Although it might have been easier to to specify
# the storgae format as Matrix, rather than a dataframe
# in the 'import' command
#
  mydat := Import("C:/Users/TomLeslie/Desktop/testdat.csv", output=Matrix):
  plots:-pointplot(mydat, style=line);

 

 

 

 

Download plotData.mw

OP sent the following to my personal email

I am getting such expression because of substitution.
I am getting a first result in the Form of {a°b} (° can be of any operator). which is later substituted.
is it possible to change {a°b} to a°b it would be very helpful. I have tried for that but could exactly get it.

To which my response would be

You are doing the substituion incorrectly

It would be so much easier to fix this problem properly if you uploaded your original worksheet using the big green up-arrow in the MaplePrimes toolbar. That way we could see what is going wrong and fix it.

It is generally bad practice to create cr*p then fix it - a better alternative is not to create the cr*p in the first place!

 

 

 

 

how the original expression was created, in particullar the term {{(b*y)}/3}!

Strongly suggest that you fix whatever created the original expression. If that is impossible for some reason, then you could try the attached

restart;
expression:= {{a*x}, {{(b*y)}/3}, {a*b}, {log(x+y)}, {3*x}};
{parse(StringTools:-Subs( {"{"="", "}"=""}, convert(expression, string)))};

{{a*b}, {a*x}, {3*x}, {(1/3)*{b*y}}, {ln(x+y)}}

 

{a*b, a*x, 3*x, (1/3)*b*y, ln(x+y)}

(1)

 

Download unSet.mw

You need multiplication sign (implied or otherwise), between the 1/2 and the rest of the expression in the definition of th function TEST.

See the attached

TEST := proc (n) options operator, arrow; (1/2)(sum(MultiplicityList(n)[u]^2+MultiplicityList(n)[u]+2, u = 1 .. nops(MultiplicityList(n)))) end proc

MultiplicityList := proc (n) options operator, arrow; [seq(ifactors(n)[2][k][2], k = 1 .. nops(ifactors(n)[2]))] end proc

TEST := proc (n) options operator, arrow; (1/2)*(sum(MultiplicityList(n)[u]^2+MultiplicityList(n)[u]+2, u = 1 .. nops(MultiplicityList(n)))) end proc

seq(TEST(i), i = 1 .. 100)

0, 2, 2, 4, 2, 4, 2, 7, 4, 4, 2, 6, 2, 4, 4, 11, 2, 6, 2, 6, 4, 4, 2, 9, 4, 4, 7, 6, 2, 6, 2, 16, 4, 4, 4, 8, 2, 4, 4, 9, 2, 6, 2, 6, 6, 4, 2, 13, 4, 6, 4, 6, 2, 9, 4, 9, 4, 4, 2, 8, 2, 4, 6, 22, 4, 6, 2, 6, 4, 6, 2, 11, 2, 4, 6, 6, 4, 6, 2, 13, 11, 4, 2, 8, 4, 4, 4, 9, 2, 8, 4, 6, 4, 4, 4, 18, 2, 6, 6, 8

(1)

 

Download twoDissue.mw

The terms 'combination' and 'permutation' have very precise definitions in probability/statistics.

  1. Given a 57-element list, this has precisely 1 (57-element) combination
  2. Given a 57 element list, with the repetitions you specify, there are a *lot* of (57-element) permutations, as about 264 million! (to be pedantic, 264,385,836).

The above numbers can be verified with

L:=[1$7, 0$50]:
LC:=combinat:-numbcomb(L, numelems(L));
LC:=combinat:-numbperm(L, numelems(L));

So generating one combination is trivial, but generating all 264,385,836 permutations is something you probably don't want to do!

 

Assuming it is true that dataplot() does not accept dataframes, the the help file (Maple2018) is incredibly misleading - not to say downright cr*p

Excerpts from the dataplot() help page - my emphasis

dataplot(data, plottype, opts);
data-an rtable, list, or set representing one or more datasets, a DataSeries or a DataFrame

 

because I don't think you can ever make this appear as desired with any kind of "active" 2D Math.

As inert text, see the attached

The OP's wants to write

 

"`S__N`(x+1)=S[N+1]=(∑)(x+1)^(n+1)=(x+1)+(x+1)^(2)+(x+1)^(3)+...+(x+1)^(N+1)"

 

which is a pretty odd requirement

Download InertEq.mw

A few pointers

  1. I don't think that you can meaningfully include "derived" units (eg kN*m, or kN/M) in a system of units. All you can do is to replace one 'base' unit, eg N or Pa, with different 'base' units - eg kN or MPa
  2. According to the Maple help pages, you should set the units 'environment' (eg Simple, Natural, etc) before loading the Units() package, or a UseSystem() command

So wih the above provisos, the attached code *ought* to set up the environment and units system which you want.

The error you get from the solve() command *may* be related to the way you have performed the units set-up, but it actually looks to me as if you have incompatible units in the expression supplied to solve(). Obviously since you do not upload your worksheet, I can't check this. If the problem persists, please upload the worksheet illustrating the problem using the big green up-arrow in the MaplePrimes toolbar.

  restart:
#
# Load the 'natural' units environment first
# and then the Units() package - simply because
# the help pages say to do it this way round!
#
  Units[UseMode](natural):
  with(Units):
#
# Check the available units in the SI
# package
#
  GetSystem(SI);
#
# Define a new system of units (NewSi)
# which replace 'newton' with 'kilonewton'
# and 'pascal' with 'megapascal'
#
# NB I don't think it makes much sense to
# define 'combined unit quantities' such
# as kN/m or kN*m, so (for the moment?) I
# have removed these
#
  AddSystem(NewSI, GetSystem(SI), kN, MPa);
#
# Check the units in the system 'NewSI'.
# Note that (compared with the output of
# GetSystem(SI) above, the units 'newton'
# and 'pascal' have been replaced by
# 'kilonewton' and 'megapascal'
#
  GetSystem(NewSI);
#
# Specify that thhis sheet will use the new
# system of units 'NewSI'
#
  UseSystem('NewSI');

Automatically loading the Units[Natural] subpackage

 

volt[SI], neper[contexts:-SI], siemens[contexts:-SI], second[SI], joule[contexts:-SI], lumen[contexts:-SI], tesla[SI], radian[contexts:-SI], steradian[contexts:-SI], pascal[contexts:-SI], dollar[contexts:-US], kilogram[contexts:-SI], kelvin[contexts:-SI], farad[contexts:-SI], ampere[contexts:-SI], henry[contexts:-SI], candela[contexts:-SI], meter[contexts:-SI], weber[contexts:-SI], coulomb[contexts:-SI], ohm[contexts:-SI], newton[contexts:-SI], mole[contexts:-SI], watt[contexts:-SI], lux[contexts:-SI]

 

volt[SI], neper[contexts:-SI], siemens[contexts:-SI], second[SI], joule[contexts:-SI], lumen[contexts:-SI], tesla[SI], radian[contexts:-SI], steradian[contexts:-SI], megapascal[contexts:-SI], dollar[contexts:-US], kilogram[contexts:-SI], kelvin[contexts:-SI], farad[contexts:-SI], ampere[contexts:-SI], henry[contexts:-SI], candela[contexts:-SI], meter[contexts:-SI], weber[contexts:-SI], coulomb[contexts:-SI], ohm[contexts:-SI], kilonewton[contexts:-SI], mole[contexts:-SI], watt[contexts:-SI], lux[contexts:-SI]

(1)

 

Download unitProb.mw

I'm not sure what you expect to achieve, other than some (pretty general) programming advice. The following is a (non-exhaustive) list of stuff in your code which I probably wouldn't do.


################################################################################

1) You use the command MTM[mldivide] a few times. This is command is more-or-less equivalent to the native Maple command LinearAlgebra:-LinearSolve(). Available options and output format may differ slightly, but the basic functionality is the same, so I'd prefer to stick with the 'native' Maple command, rather than call some kind of Maple implementation of a Matlab command. The latter is just unnecessary complication.

############################################################################

2) I would make more use of simple range specifiers when doing Matrix manipulations. Nothing wrong with using DeleteColumn(), but it is actually easier(>) to remove the first (or last) columns in a matrix using something like

A[..,2..-1]; # remove the first column
A[..,1..-2]; # remove the last column
A[2..-1,..]; # remove the first row
A[1..-2,..]; # remove the last row

DeleteColumn() and DeleteRow() are more useful when eliminating rows/columns somewhere in the 'middle' of a matrix

###########################################################################

3) You use 'for' loops to assign vector entries rather often, as in

for i from 1 to N do
     X0[i] := gval[i]:
od:

This should never be necessary (and it is inefficient!). The above could be achieved with

X0[1..N]:=gval[1..N], or

If both X0 and gval have dimension N, then even more simply with

X0:=gval

Similarly with more complicated assignments such as

for i from 1 to N do
     X0[i] := X0[i] - (1-beta)*(fval[i] - QRg[i]);
od:

Why isn't this just

X0 := X0- (1-beta)*(fval- QRg);

NB Maple does not (generally) allow you to assign to a passed argument. If this is an issue then create a local copy of the passed argument and do all reassignments to the latter

#####################################################################

There are probably other 'improvements' - but it's past my bedtime!
 

 

I tried this a few ways as well, and couldn't make it work :-( One of these cases where the 'help' page isn't very helpful!

There is however a fairly simple "workaround" - see the attached. The plot "renders" much better in a Maple worksheet than it does here - honest!!

x := [5, 10, 15, 20];

y := [4.22, 7.49, 12.24, 19.60];  

yui := [2.48, 3.76, 6.11, 14.60];

yli := [2.27, 3.34, 6.09, 10.90];

yerr:= Array([ 2.48, 2.27, 3.76, 3.34, 6.11, 6.09, 14.60, 10.90]);

with(Statistics): with(plots):

#p1 := ErrorPlot(y, xcoords = x, yerrors = yli);
#
# Workaround
#
  display( [ seq
             ( pointplot
               ( [ [x[j], y[j]],
                   [x[j], y[j]-yli[j]],
                   [x[j], y[j]+yui[j]]
                 ],
                 style=pointline
               ),
               j=1..4
             )
           ]
         );

x := [5, 10, 15, 20]

 

y := [4.22, 7.49, 12.24, 19.60]

 

yui := [2.48, 3.76, 6.11, 14.60]

 

yli := [2.27, 3.34, 6.09, 10.90]

 

Array(%id = 18446744074206320510)

 

 

 

 

 

 


 

Download errorPlot.mw

 which don't make much sense.

You should consider the comments I have inserted in the following. I have modified your code in a couple of places, but have no idea whether these changes are actually what you want!?

restart; with(student)

n := 2

2

(1)

v := sum(u[i]*p^i, i = 0 .. 2)

p^2*u[2]+p*u[1]+u[0]

(2)

NULL

#
# 'e' in above should be exp()?
#
  f := x -> exp(x)+(1/2)*x*(exp(2*x)-1);

proc (x) options operator, arrow; exp(x)+(1/2)*x*(exp(2*x)-1) end proc

(3)

"k(x,t):=x;"

proc (x, t) options operator, arrow, function_assign; x end proc

(4)

NULL

#
# Above definition will convert passed
# arguments to 'functions' - is this desired??
#
  F := u-> u^2;

proc (u) options operator, arrow; u^2 end proc

(5)

u[0] := f(t)

exp(t)+(1/2)*t*(exp(2*t)-1)

(6)

for i to 2 do u[i] := expand(subs(x = t, int(coeff(p*k(x, t)*F(v), p^i), t = 0 .. x))) end do

#
# Check values for u[1] and u[2] are these expected?
#
  u[1];
  u[2];

-(1465/1152)*t+(1/16)*(exp(t))^4*t^3-(1/32)*(exp(t))^4*t^2+(1/128)*(exp(t))^4*t+(1/3)*(exp(t))^3*t^2-(1/4)*(exp(t))^2*t^3-(1/9)*(exp(t))^3*t+(1/4)*(exp(t))^2*t^2+(1/12)*t^4+(3/8)*(exp(t))^2*t-exp(t)*t^2+exp(t)*t

 

(792086351/311040000)*t-(1/72)*t^7+(1/6)*exp(t)*t^5+(1/3)*exp(t)*t^4-2*exp(t)*t^3+(17/41472)*(exp(t))^6*t+(2809/72000)*(exp(t))^5*t^2+(1/48)*(exp(t))^2*t^5-(2809/360000)*(exp(t))^5*t+(31/27)*(exp(t))^3*t^3-(1/6)*(exp(t))^2*t^4-(5/64)*(exp(t))^4*t^5+(1/24)*(exp(t))^2*t^6-(11/18)*(exp(t))^3*t^4-(7/576)*(exp(t))^6*t^4+(17/2304)*(exp(t))^6*t^3-(17/6912)*(exp(t))^6*t^2-(323/3600)*(exp(t))^5*t^3+(19/128)*(exp(t))^4*t^4+(1/96)*(exp(t))^6*t^5+(11/120)*(exp(t))^5*t^4+(1465/3456)*t^4+(839/576)*exp(t)*t^2-(839/576)*exp(t)*t+(113/768)*(exp(t))^4*t^3-(595/4608)*(exp(t))^4*t^2+(595/18432)*(exp(t))^4*t-(167/324)*(exp(t))^3*t^2-(3625/2304)*(exp(t))^2*t^3+(167/972)*(exp(t))^3*t+(5929/2304)*(exp(t))^2*t^2-(5929/4608)*(exp(t))^2*t

(7)

 

s := value(sum(Eu[k], k = 0 .. 2*e))

sum(Eu[k], k = 0 .. 2*e)

(8)

#
# what is the variable Eu[k] in the above??
# E*u[k], maybe?? But then what is 'E'.
#
# Should the range 'k=0..2e' in the above
# actually be 'k=0..2*exp(1)' ???? But the
# summation index ought to be an integer!!
#
# Since the sum cannot be computed, unknown
# summand and meaningless range specification
# everything from this poitn "breaks"
#

"U(x) :=collect(s, x);"

proc (x) options operator, arrow, function_assign; collect(s, x) end proc

(9)

with*plots

p1 := plot*(exact*solution, t = 0 .. T, style = point)

p2 := plot*(U(t), t = 0 .. T, style = line)

plots*([display])(p1, p2)

plots*[display(plot*(exact*solution, t = 0 .. T, style = point), plot*(sum(Eu[k], k = 0 .. 2*e), t = 0 .. T, style = line))]

(10)

NULL

NULL


 

Download primes.mw

The default output from DirectSearch:-GlobalOptima() contains three 'fields', the interpretation of which is as follows

  1. The first field is the desired 'optimum' value of the supplied function - either a maximum or a minimum, depending on what you requested
  2. The second field, contains the values of the target function arguments at which the optimum' is obtained
  3. The third field contains the 'number of iterations', although this requires some interpretation - see later

So the basic output, supplies one piece of information which you request - ie the 'number of iterations', although as noted above - this may not mean what you think it means (see later).

#####################################################################

The concept of "residuals" is not meaningful for a global optimisation problem. "Residuals" are only meaningful when the actual solution is "known". For example, with SolveEquations() or DataFit(). For these two commands, one can determine the 'deviation' between the result returned, and the actual equations/data, which is essentially the definition of 'residual'. For these commands, residuals are returned. However, in any "optimisation" commands, the actual result is not (generally) known, so there is no sensible definition of 'residual'

######################################################################

One can use showstat(GlobalOptima) to actually read the original code: scanning this quickly (always risky, and I may be wrong!), my interpretation of the overall operation is as follows

  1. GlobalOptima() calls Search()
  2. If the user supplies ranges for the variables, then (random) Uniform Sampling of these ranges is performed, to generate (by default) 30 possible initial values. (You can change this value with the option 'number'.) The routine Search() is called with each of these start values, to obtain a 'local' optimum. The best of these 'local optima' is retained as the 'global optimum'
  3. If the user supplies an initial point, then Search() is called and a local optimum is computed. The position of this first optimum is used as the centre of ranges for all variables. Again random uniform sampling is applied with point ranges set by centre-100, centre+100, to generate 30 possible starting values. Then the process continues as described in (2) above
  4. If the user does not supply any initial value specification, then Search() is called (with all argument values set to 0.9!?!) and a local optimum is computed. Then continue as in (3) above

So far as I can tell, the thrid field returned by GlobalOptima() is actually the number of iterations used in the call to Search() which returned the best 'local' optimum described above, rather than sum of all the iterations used across all 30 (by default) calls to Search()

#############################################################################

You can 'watch' the operation of GlobalOptima(), by setting 'printlevel' prior to calling it. Start with

printlevel:=10

which is a reasonable trade-off between 'useful information' and 'get swamped by output'

I've checked OP's worksheet in

Maple 18
Maple 2015
Maple 2016
Maple 2017
Maple 2018

and cannot reproduce the problem. Maybe (s)he has a pretty early version??

Alhough (even in an early version) I cannot think why S1(uu) would return quantities expressed in terms of the name 'psi'. The function call uses the parameter 'uu', and the function itself uses the (local) argument 'phi' - so where the **** did 'psi' come from???

I mean what is wrong with,

restart;
with(ScientificConstants):
Digits:=20:
En:=n->simplify(-evalf(Constant(c, units)*Constant(h, units)*Constant(R[infinity], units)))/n^2;
En(1);
En(4);

 

You need to check

  1. is the test variable is a matrix?
  2. Does the matrix have dimensions 1x1?

which can be achieved with

restart;
R1 := Matrix([2]);
if   type(R1, Matrix) and op(1, R1)='1,1'
then R1:=R1[1,1];
fi:
R1;

 

First 123 124 125 126 127 128 129 Last Page 125 of 207