MapleMathMatt

277 Reputation

6 Badges

2 years, 60 days

MaplePrimes Activity


These are answers submitted by MapleMathMatt

Hi @wagaren ,

Another way would be to set the original expression to the desired form with undetermined coefficients, and then solve the identity for the constants:

f := cos(t)^6;
g := add( a[k] * cos(2*k*t), k = 0 .. 3 );
S := solve( identity( f = g, t ) );
h := eval( g, S );

Hi @meatboi ,

If you define a procedure for a numeric integral, you can get the plot:

G := proc( epsilon )
  int( exp( -epsilon * ( x^4 + x^2 ) ), x = -10 .. 10, numeric );
end proc:
plot( G, 0 .. infinity );

 

Assuming you're using Maple 2018, you could try the SMTLIB:-Satisfy() command. By converting the equations to a boolean expression, the retriction that a certain number of terms be 1 can be written as a condition on the sum of all the terms. For instance:

# equations/inequalities
E := [
    abs( x[1] * x[2] * x[3] ) <= 1,
    x[4] * x[5] * x[6] = 0,
    x[1] * x[4] + x[2] * x[5] + x[3] * x[6] = 1
];

# variables
V := indets( E, name );

# convert to boolean expression
f := &and( op(E), seq( &or( v=0, v=1 ), v=V ) );

# solution with two non-zero terms
SMTLIB:-Satisfy( f );

# solution with three non-zero terms
g := &and( f, add(V) > 2 );
SMTLIB:-Satisfy( g );

Hi @asa12,

This isn't quite what you're after, but it is close (I used regular indexed variables like x[1], not suffixed like x1, and Maple auto-sorted the terms, with your x1 and x2 becoming x[10] and x[11]):

restart;

phi := proc( p :: polynom(integer,x) )

    local c, f, i, q, u:

    c := 1:

    f := proc( y :: symbol, n :: posint )
        local j, Y:
        Y := [ seq( y[j], j=c..c+n-1 ) ]:
        c := c + n:
        return Y:
    end proc:

    q := collect( p, x ):
    q := evalindets( q, `&*`(posint,`^`(identical(x),posint)), u -> add( mul( f(x,op([2,2],u)) ), i=1..op(1,u) ) ):    
    q := evalindets( q, `&*`(posint,identical(x)), u -> add( f(x,op(1,u)) ) ):

    return q:

end proc:

p := 2 * x + 3 * x^3;
q := phi( f );

Hi @zjervoj97,

You can use the element-wise operator ~, or the map() function:

L1 := [-3,-2,-1,0,1,2,3];
L2 := L1^~2;
L3 := map( u -> u^2, L1 );

Hi @maple2015,

A drawback of using op(2,M) is that it won't include elements that are 0. I suggest using the indices() command instead:

restart;

M := < 1, 2; 3, 0; 0, 2 >;

# Doesn't include (2,2)=0 and (3,1)=0.
op( 2, M );

SearchMatrix := proc( A :: Matrix, x :: anything, $ )
    return select( u -> A[op(u)] = x, { indices( A ) } ):
end proc:

# { [2,2], [3,1] }
SearchMatrix( M, 0 );

 

 

 

Hi @Adam Ledger ,

Maybe it is Maple's engine security.

I received the same error when I tried the code with an existing file, but after going to "Tools" > "Options" > "Security", checking "Enable engine security", adding the file under "Readable files", and choosing to "Apply to session", Maple read the file with no error.

Hi @kumar29 ,

When you make an implicit plot in Maple, the plot structure contains an array of the points, which can be extracted and exported. For example:

p := plots:-implicitplot( x^2 + y^2 - 1, x=-1..1, y=-1..1 );
A := op( [1,1], p );
ExcelTools:-Export( A, "data.xlsx" );

Hi @Annonymouse ,

I suggest the indets() command, which returns a set of indeterminates. You can then divide these into those that are variables and those that are parameters. Please try the following:

A := [ k[a1]*C[T]*(R-x[1]-x[2])-k[d1]*x[1], k[a2]*C[T]*(R-x[1]-x[2])-k[d2]*x[2] ];

# All indeterminates.
U := indets( A, 'name' );

# Variables.
V := select( u -> type( u, 'indexed' ) and member( op( 0, u ), ['x','y'] ), U );
v := numelems( V );

# Parameters.
P := U minus V;
p := numelems( P );

Hi @student_md

If you specify the 'insequence'=true option for display(), you can create the animation. Here's my variation:

restart;

# endpoints for extension
a, b := -Pi, Pi;

# original function
f := t -> t / Pi;

# periodic extension
g := proc(t)
  option cache:
  piecewise( t > b, g(t+a-b), t < a, g(t+b-a), f(t) ):
end proc:

# constant term of Fourier series
alpha := 1/(b-a) * int( f(t), t=a..b );

# Fourier cosine coefficients
A := unapply( simplify( 2/(b-a) * int( f(t) * cos(2*Pi/(b-a)*n*t), t=a..b ) ), n ) assuming n :: posint;

# Fourier sine coefficients
B := unapply( simplify( 2/(b-a) * int( f(t) * sin(2*Pi/(b-a)*n*t), t=a..b ) ), n ) assuming n :: posint;

# Inert Fourier series
F := unapply( alpha/2 + Sum( A(n) * cos(2*Pi/(b-a)*n*t) + B(n) * sin(2*Pi/(b-a)*n*t), n=1..infinity ), t );

# Truncated series
G := proc( t, m )
  alpha/2 + add( A(n) * cos(2*Pi/(b-a)*n*t) + B(n) * sin(2*Pi/(b-a)*n*t), n=1..m ):
end proc:

# animation
M := 20:
plots:-display( seq( plot( [ 'g'(t), 'G'(t,m) ], 'tickmarks'=[piticks,[-1,1]], 'color'=[blue,red], 'thickness'=3, 'discont'=true ), m=1..M ), 'insequence'=true );

 

Hi @Dim

 

If you are using the default grading code

evalb(($ANSWER)-($RESPONSE)=0);

for a Maple-graded question, it won't mark equations as being equal, since it is comparing the difference with zero. However, this should work:

evalb(($ANSWER)=($RESPONSE));

To make the grading code more robust, you can also allow for equivalent equations:

EquivEQ := proc( A :: 'Or'(equation,algebraic), B :: 'Or'(equation,algebraic), { params :: set := {} } )
    local phi, X, Y, k, sol:
    phi := u -> `if`( type( u, 'equation' ), lhs(u) - rhs(u), u ):
    X := simplify( phi(A) ):
    Y := simplify( phi(B) ):
    sol := solve( { X = k * Y, k <> 0 }, k ):
    return is( numelems( sol ) = 1 and indets( sol ) = { k } union params ):
end proc:

EquivEQ( $ANSWER, $RESPONSE );

For an example in Maple:

f := s*Y(s)-2-3*Y(s) = 2/(s-1);
g := simplify( f );
evalb( f = g ); # false
is( f = g ); # false
EquivEQ( f, g ); # true

 

 

Hi @josephrajck

This works for me for the parameter value I chose, but there is probably a more robust way:

vanderpol.mw

restart;

UseHardwareFloats := false:
Digits := 35:

# Van der Pol oscillator.
mu := 4.0;
de := diff( x(t), t ) - y(t), diff( y(t), t ) - mu * ( 1 - x(t)^2 ) * y(t) + x(t);

# Parametric initial conditions.
ic := x(0) = a, y(0) = b;

# Parametric solution of IVP.
sol1 := dsolve( { de, ic }, { x(t), y(t) }, 'numeric', 'abserr'=1e-25, 'parameters'=[a,b] ):

# Objective function, requiring periodicity.
phi := proc( a__0, b__0, tau__0 )
    sol1( 'parameters' = [ 'a'=a__0, 'b'=b__0 ] ):    
    eval( ( x(t) - a__0 )^2 + ( y(t) - b__0 )^2, sol1( tau__0 ) ):
end proc:

# Bounds on parameters.
a__r, b__r, tau__r := -3..3, -7..7, 1..25:

# Plot to estimate initial values for parameters.
DEtools:-DEplot( { de }, { x(t), y(t) }, t=tau__r, x=a__r, y=b__r, 'dirfield'=500 );

# Estimate for parameters.
P := [ a=1.5, b=-0.25, tau=10.0 ];

# Determine parameters [a,b,tau] based on constraint phi(a,b,tau)=0.
sol2 := Optimization:-NLPSolve( 'phi(a,b,tau)', 'initialpoint'=P ):

# Assign parameter values.
assign( sol2[2] ):

# Fix solution for the parameter values.
sol3 := dsolve( { de, ic }, { x(t), y(t) }, 'numeric', 'abserr'=1e-25 ):

# Choose smallest tau (period).
while true do
    d := fnormal( eval( ( x(t) - a )^2 + ( y(t) - b )^2, sol3( tau/2 ) ), 1e-10 ):
    if d = 0 then
        tau := tau/2:
    else
        break:
    end if:
end do:

# Print values.
'a' = a, 'b' = b, 'tau' = tau;

# Plots.
p := DEtools:-DEplot( { de }, { x(t), y(t) }, t=0..5*tau, x=a__r, y=b__r, 'dirfield'=5000 ):
q := plots:-odeplot( sol3, [ x(t), y(t) ], t=0..tau, 'numpoints'=1000, 'color'=blue, 'thickness'=3 ):
plots:-display( [ p, q ] );

Hi @sideshow

Does this work for you?

randomize():
f := rand( 1..2 ):
RM := Matrix( 10, 10, (i,j) -> (-1)^f() );

Hi @rahinui ,

The :- oparator will work. It will tell Maple to use the global, unassigned version:

 

foo1 := proc( {param:={}}, $)
  foo2(':-param'=param):
end proc:

 

Alternatively:

foo1 := proc( {param:={}}, $)
  foo2(_options['param']):
end proc:

Hi @MALU ,

 

I didn't have any luck with applyrule(), but I did with evalindets():


r := u -> op(0,u)[1](op(1..2,u)) * op(0,u)[2](op(3..4,u));
evalindets( f(t,x,y,z), function, r );
evalindets( g(a,b,c,d), function, r );

 

1 2 Page 1 of 2