tomleslie

13876 Reputation

20 Badges

15 years, 175 days

MaplePrimes Activity


These are answers submitted by tomleslie

When solving for numerical roots of a polynomial the correct command would be fsolve(), not solve(). Using this command, all of your other problems seem to disappear. Try the code below

   restart;
   f:=5*x^3-5*x+1;
#
# Well since f is a simple polynomial
# fsolve will return all the real roots,
# as in
#
# s:=[solve(f)]; evalf(%);  # All the roots are real
#
   s:=[fsolve(f)];
#
# From this point, most commands just "work".
# The sort() command is probably superfluous
# fsolve() usually puts the roots in ascending
# order. (I'm not sure if ths is documented
# anywhere and it might be unwise to bet on it)
#
   max(s);
   type(s,list(realcons));
   sort(s):

 

The Grid Computing Toolbox is not bundled with Maple - it is an add-on which you have to pay for. So before considering anything else, you have bought the Grid package - right?

you are calling the function w2() with numeric arguments - ie 'x' and 'y' are numbers - now consider that within the w2() function you are differetniating with respect to a number - never a good idea. You need to perform the differentiations, and then substitute the passsed parameters in the result, as in

restart;
v2 := proc (x, y) options operator, arrow; sin(x)*cos(y) end proc;
u2 := proc (x, y) options operator, arrow; 3*sin(x)-cos(y) end proc;
w2 := proc (x, y) options operator, arrow; eval( diff(v2(u, v), u)-(diff(u2(u, v), v)),[u=x,v=y]); end proc;
L1 := proc (x, y) options operator, abs(w2(x, y)): end proc;
CodeTools:-Usage(evalhf(add(add(abs(w2(x, y)), x = 0 .. 10), y = 0 .. 10)));

 

the attached - Note many other plotting options are available

matPlot.mw

From the toolbar, access the Tools->Options pop-up, and set

Tools -> Options -> Display -> Input Display -> Maple Notation
                                                   Output Display -> 2-D Math Notation
                                                   Typesetting Level -> Extended

Tools -> Options -> Interface -> Default format for new worksheets -> Worksheet

Then click "Apply to session" on the Tools->Options pop-up window. Now from the toolbar use

File -> New -> Worksheet Mode

This *ought* to give you a brand new worksheet, in "worksheet mode" and accepting 1-D Maple input, which is the most basic mode possible. So just type

1+1;

at the prompt, which ought to be the '>' symbol. Note the semicolon at the end of the above, it is important.

Now what happens - if anything

 

Since the cot() function will always vary from -Infinity to +Infinity, you should never "FAIL" to find a root - there is alway another one!

If any sequential rootfinding process 'FAILS", then there is a problem with that process!

just to define h(z) as a piecewise() function - which means that one doesn't have to define h1, h2, h3, K1, K2, K3, lambda1, lambda2, lambda3! Just h(), K, and lambda, as in

  restart;
  F:=0.3: L1:=0.2: d1:=0.2:
  d2:=0.2: L2:=0.3: alpha:=Pi/6:
  h:=z->piecewise( z<=0.2,
                                1-(delta2/2)*(1 + cos(2*(Pi/L1)*(z - d1 - L1))),
                                z<=0.4,
                                1-(delta2/2)*(1 + cos(2*(Pi/L2)*(z - d2 - L2))),
                                z<=0.6,
                                1+(delta2/2)
                             ):
  K:=((4/h(z)^4)-(sin(alpha)/F)-h(z)^2+Nb*h(z)^4):
  lambda:= Int(K,z=0..0.6):
  plot( [seq(eval(lambda, Nb=j), j in [0.1,0.2,0.3])], delta2=0.02..0.1);

 

The minimum for he function is 20/3, which occurs at x=4/3. There is no (finite) maximum. Theese results can be obtained with

   restart;
#
# Define the expression and the constraint
#
   expr1:=2*x^2+2*x*y+3*y^2;
   cts:=y=2-x;
#
# Substitute the constraint into the
# and plot the result, just for
# illustrative putposes
#
   expr2:=subs(cts, expr1);
   plot(expr2, x=0..4);
#
# Find the maximum and minimum
#
   minimize( expr2, location=true);
   maximize( expr2, location=true);

 

The command

`union`(indets~({eq1, eq2, eq3,eq4}, 'name')[]);

returns

{Omega, a, alpha, beta, k, m, r, V[0], Y[0]}

indicating that you actually have eight unknowns.

solve() may (or may not!) be able to return the unknowns you want, ie [ Omega, alpha, beta, k] in terms of the others - but is this what you want/expect?

that the OP want some combination/variation of the plots p1 and p3 in the following code - but I have no idea exactly what!

   restart;
#
# Define "tunable" parameters for lemniscate and cardioid
#
   a:=1:
   b:=1/2:
#
# generate lemniscate
#
  p1:=plot( sqrt(a*cos(2*theta)),
                  theta=0..2*Pi,
                  color=red,
                  coords=polar,
                  scaling=constrained
               );
#
# Generate cardioid
#
  p2:=plot( 2*b*(1-cos(theta)),
                  theta=0..2*Pi,
                  color=blue,
                  coords=polar,
                 scaling=constrained
               );
#
# Rotate the cardioid
#
   p3:=plottools:-rotate(p2, Pi/2);
#
# Plot cardioid and lemniscate on in same figure
#
   plots:-display([p1,p3]);

but they all fail on one basic problem: I can't make Matlab import anything which contains an undefined variable. So when you say you want to use "symbolic expressions" in your Maple-generated matrix, how do you plan to load these "symbolic expressions" into Matlab?

Let us consider a couple of "toy" examples: firstly with numerics only

   restart;
#
# Generate a 1000x100 matrix filled with zeros
#
    A:=Matrix(1000,100,fill=0);
#
# Insert a couple of numeric values into this matrix
#
   A[100,25]:=2.0:
   A[400,75]:=3.0:
#
# produce expressions suitable for a Matlab m-file
# but only for those matrix entries whihc are non-zero
#
   b:=[seq( seq( `if`(A[i,j]<>0,a[i,j]=A[i,j], NULL), i=1..op([1,1],A)),j=1..op([1,2],A))];
#
# Write all subsequent output to the specified
# file. OP will need to chenge the filename to
# something appropriate for his/her machine
#
   writeto("C:/Users/TomLeslie/Desktop/trymat1.m");
   CodeGeneration:-Matlab(b);

   writeto(terminal)

Now the text in the file trymat1.m will look like

a(100,25) = 0.20e1;
a(400,75) = 0.30e1;

Check this in your favourite text editor. Now this file can be used in Matlab using the (Matlab) code

 %
 % initialise matrix with zeros
 %
     a=zeros(1000,100);
 %
 % load data file
 %
     run C:/Users/TomLeslie/Desktop/trymat1.m
 %
 % Check relevant entries in matrix
 %
     a(100,25)
     a(400,75)

Now you can repeat the whole process, but this time with a couple of "symbolic" entries in the matrix generated by Maple.

   restart;
#
# Generate a 1000x100 matrix filled with zeros
#
   A:=Matrix(1000,100,fill=0);
#
# Insert a couple of numeric values into this matrix
#
   A[100,25]:=x^2:
   A[400,75]:=x*cos(x):
#
# produce expressions suitable for a Matlab m-file
# but only for those matrix entries which are non-zero
#
   b:=[seq( seq( `if`(A[i,j]<>0,a[i,j]=A[i,j], NULL), i=1..op([1,1],A)),j=1..op([1,2],A))];
#
# Write all subsequent output to the specified
# file. OP will need to chenge the filename to
# something appropriate for his/her machine
#
   writeto("C:/Users/TomLeslie/Desktop/trymat2.m");
   CodeGeneration:-Matlab(b);
   writeto(terminal)

If you check the file trymat2.m in your favourite text editor, you will see that it contains

a(100,25) = x ^ 2;
a(400,75) = x * cos(x);

However if one attempt to load this into Matlab with the code

 %
 % initialise matrix with zeros
 %
     a=zeros(1000,100);
 %
 % load data file
 %
     run C:/Users/TomLeslie/Desktop/trymat2.m

you will immediately get

Undefined function or variable 'x'.
Error in trymat2 (line 1)

because Matlab will not load anything with an undefined variable

If you can tell me how to load expressions with undefined variables into Matlab, then I am pretty sure I can persuade Maple to produce a file in the require format

See if you can work out which is which from the following

restart;
eulerphi:=proc( n::integer)
                           local k, count:=0:
                           for k from 1 by 1 to n do
                                 if       gcd(k,n)=1
                                 then count:=count+1;
                                 fi
                           od:
                           return count;
                  end proc:
p:=1267:
eulerphi(p);
NumberTheory:-Totient(p);

 

 

There are definitely 'quirks' when using units. The attached "works around" most(?) of the issues you have expressed.

The one which bothers me is why I can make single plots "work", in a more or less logical(?) manner but I cannot persuade the multiple plot to do so, without using the plots:-display() approach.

unitPlot.mw

An excerpt from the evalb() help page (my emphasis added) - read carefully

Important: The evalb command does not simplify expressions. It may return false for a relation that is true. In such a case, apply a simplification to the relation before using evalb.

You can make the argument that evalb() really *ought* to apply simplification before testing the relation. This is a view with which I have some(?) sympathy.

As a trivial illustrative example, consider a well-known trigonometric identity.

expr:= cos(a+b)=cos(a)*cos(b)-sin(a)*sin(b);

Note the two sides of this expression are "equivalent", but not "identical". Both

evalb(expr)

and

evalb(simplify(expr))

will return false. However, both

evalb(combine(expr, trig));

and

evalb(expand(expr));

will return true

I suppose one has to consider whether evalb() should be checking whether two quanitites are "identical" or merely "equivalent". It seems as if Maplesoft has decided on the former, possibly(?) on the grounds that

  1. there may be occasions when users want to know if two quantities actually are actually "identical" rather than merely "equivalent" - although when would this distinction be important?
  2. if users really do want to test for mere "equivalence", then such expressions can generally be converted to "identical", by appropriate use of simplify(), expand(), combine() commands

As a final comment the counterexample presented by mmcdara 117is not too surprising. The code

 

a := f(n)-g(n);

 

s := simplify(a);
evalb(a=0)

applies the evalb() command to the unsimplified expression 'a' and so returns false. On the other hand, the code

 

f(n)-g(n);

 

simplify(%);
evalb(%=0)

applies the evalb() command to the output of the preceeding simplify() command, which is 0 - so is equivalent to evalb(0=0), and therefore returns true

 

 

 

 

Why don't you just use the ExportMatrix(filename, Matlab) command which will create 'filename' with content being a 'Matlab-format' matrix. This will do nothing for removing the zero elements - but why do you care about a few hundred redundant lines?

As an alternative, you can drive Maple from within Matlab (assuming that you have the link set up), so  it might to be possible to create the default matrix (all zeros) within Matlab; then from Matlab, issue Maple commands to compute non-zero entries.

This *might* be awkward to set up and it only really saves you the intervening step of writing from Maple to a file, and then reading that file from Matlab, so (although possible) I really couldn't recommend it

First 144 145 146 147 148 149 150 Last Page 146 of 207