Maple Questions and Posts

These are Posts and Questions associated with the product, Maple

Maple allows to extract, manipulate, and optimize equations from a MapleSim model. Code can be generated from the equations in various programming languages. To verify the code, C code can be imported back into the original MapleSim model and compared to the model.

This verification step is not an everyday task, but it is advisable before the code is used elsewhere (e.g., in a controller). This post summarizes helpfull links and provides an additional example with equations that are too large to be efficiently verified by code review.

Comparison to a physical model is demonstrated here on an older version of MapleSim (~2015). In newer versions the import has changed (basics are described in Tutorial 6.6: Using the External C Code/DLL Custom Component App). An external C compiler must be set-up to make the import work.

The attached MapleSim model verifies against an optimized custom component. Instead of manually entering and modifying the code as described in the Tutorial 6.6, the model uses a Maple worksheet that programmatically generates C code from Maple equations and modifies the C code (sets C definitions and parameters) to be usable for MapleSim’s External C/Library Block App.

The Maple worksheet to generate and modify C code has been improved in many details with support from MaplePrime users for which I would like to express my thanks.

C_code_generation_of_optimised_code_for_MapleSim.mw

C_code_generation_of_optimised_code.msim

 

 

Why doesn't 

plot((-2)^x, x = -2 .. 2)

produce anything?

I guess this is some kind of tricky function since for non-integer x it seems not to correspond to a real number.

This came up for me when thinking about a sequence n(c^n), where |c|<1.

Is there some way to plot the values of a sequence? 

Suppose H is a 2*2 matrix and (x0,y0) is the center of an ellipse. We want to draw the ellipse in form of (x-x0,y-y0)H(x-x0,y-y0)^T. 

Equivalently, suppose we use variable P to show both x and y in the vector form. How is it possible to draw P^T H P? I would be thankful if you could give me and example.


 

``

eq1 := -6*sin(theta)*(cos(theta)^2*(diff(n(r, theta), r))*a^3+cos(theta)^2*a^4+3*cos(theta)^2*a^2*r^2+(diff(n(r, theta), r))*a*r^2+2*r^4-n(r, theta)*a*r)/(r^2+cos(theta)^2*a^2)^(3/2) = 0

-6*sin(theta)*(cos(theta)^2*(diff(n(r, theta), r))*a^3+cos(theta)^2*a^4+3*cos(theta)^2*a^2*r^2+(diff(n(r, theta), r))*a*r^2+2*r^4-n(r, theta)*a*r)/(r^2+cos(theta)^2*a^2)^(3/2) = 0

(1)

eq2 := -(6*(cos(theta)^2*sin(theta)*(diff(n(r, theta), theta))*a^3+a^4*r*cos(theta)+2*cos(theta)*a^2*r^3+cos(theta)*r^5+n(r, theta)*a^3*cos(theta)+cos(theta)*n(r, theta)*a*r^2+sin(theta)*(diff(n(r, theta), theta))*a*r^2))/(r^2+cos(theta)^2*a^2)^(3/2) = 0

-6*(cos(theta)^2*sin(theta)*(diff(n(r, theta), theta))*a^3+a^4*r*cos(theta)+2*cos(theta)*a^2*r^3+cos(theta)*r^5+n(r, theta)*a^3*cos(theta)+cos(theta)*n(r, theta)*a*r^2+sin(theta)*(diff(n(r, theta), theta))*a*r^2)/(r^2+cos(theta)^2*a^2)^(3/2) = 0

(2)

pdsolve([eq1, eq2])

{n(r, theta) = (1/2)*(4*r^2+2*a^2*cos(2*theta)+2*a^2)^(1/2)*((Int(-2*((a^4+3*a^2*r^2)*cos(2*theta)+a^4+3*r^2*a^2+4*r^4)/((4*r^2+2*a^2*cos(2*theta)+2*a^2)^(1/2)*(a^2*cos(2*theta)+a^2+2*r^2)*a), r))*sin(theta)+Int(5*(a^2*(-(1/5)*(a^2+4*r^2)^2*cos(3*theta)+(-(3/5)*a^4-(8/5)*r^2*a^2)*cos(5*theta)-(1/5)*a^4*cos(7*theta)+cos(theta)*(a^4+(16/5)*r^2*a^2+(16/5)*r^4))*(4*r^2+2*a^2*cos(2*theta)+2*a^2)^(1/2)*(Int(16*((a^2+3*r^2)*cos(2*theta)+a^2-r^2)/(4*r^2+2*a^2*cos(2*theta)+2*a^2)^(5/2), r))-(16/5)*(a^2+r^2)*((-(1/2)*a^2-2*r^2)*cos(3*theta)-(1/2)*a^2*cos(5*theta)+cos(theta)*(a^2+2*r^2))*r)*a/((4*r^2+2*a^2*cos(2*theta)+2*a^2)^(1/2)*((32*a^4+64*a^2*r^2)*cos(2*theta)+8*a^4*cos(4*theta)+24*a^4+64*r^2*a^2+64*r^4)), theta)+_C1)/sin(theta)}, {n(r, theta) = -(a^2*r+r^3)/a}

(3)

``


 

Download PDE_integral.mw

Hey guys im using maple 2022 with last version of Physics package (version 2022) , I know my question might be stupid but in the following expression 

restart;
with(Physics[Vectors]);
Setup(mathematicalnotation = true);
with(Physics);
Setup(op = {I__b, I__s, Omega, `&omega;_`});
eval(I__b . (diff(`&omega;_`(t), t)), {I__b = <<I__1 | 0 | 0>, <0 | I__2 | 0>, <0 | 0 | I__3>>, `&omega;_`(t) = <omega[1](t), omega[2](t), omega[3](t)>})

I want to get the following as result

but I keep getting the unevaluated one as follows

What am I doing wrong here, 

thanks in advance

 

 

 

i have a problem in an optimzation problem. in the problem using NLPSolve to find the minimum, i have an integration which i use the Int command to be solved in the optimization process, but this error occures: Error, (in Optimization:-NLPSolve) could not store Int(..) in a floating-point rtable 
please help to solve the problem, tnx in advance

restart:with(LinearAlgebra):

N:=3:

m:=Vector([ 1 , log(x+b3) , b2/(x+b3) ]):

A:=m.m^+:

for i to N do
m||i:=eval(A,[x=x||i]);
od:

M:=add(w||i*m||i,i=1..N-1)+(1-add(w||i,i=1..N-1))*m||N:

MM:=( LinearAlgebra:-Trace(MatrixInverse(M)) ):

IF1:=evalf(Int(MM,[b2=1..2,b3=1..2],method = _d01ajc,epsilon=0.001)):

s:= Optimization:-NLPSolve(IF1,w1=0..1,w2=0..1,x1=1..10,x2=1..10,x3=1..10,variables=[w1,w2,x1,x2,x3],initialpoint={w1=0.6,w2=.1,x1=8,x2=7,x3=5},maximize=false,method=modifiednewton)

Error, (in Optimization:-NLPSolve) could not store Int(Int(16.6666666666666679*(-448.000000000000057*ln(7.+b3)*ln(5.+b3)+76.1999999999999886*ln(8.+b3)^2*b3^2+.199999999999999956*ln(8.+b3)^2*b3^4+.399999999999999911*ln(5.+b3)^2*b3^4+527.500000000000000*ln(5.+b3)^2+780.799999999999727*ln(8.+b3)^2+191.100000000000023*ln(7.+b3)^2+89.0999999999999943*ln(5.+b3)^2*b3^2+9.79999999999999893*ln(5.+b3)^2*b3^3+6.39999999999999858*ln(8.+b3)^2*b3^3+400.*ln(8.+b3)^2*b3+12.3000000000000025*ln(7.+b3)^2*b3^2+84.0000000000000142*ln(7.+b3)^2*b3+356.*ln(5.+b3)^2*b3+.600000000000000089*ln(7.+b3)^2*b3^3-1176.*ln(8.+b3)*ln(5.+b3)+280.*ln(8.+b ... 99999999999716*ln(7.+b3)*ln(5.+b3))^2), b2 = 1. .. 2.), b3 = 1. .. 2.) in a floating-point rtable

 

 

Download LinearLog-A-Bayesian_1.mw

"Warning, unable to evaluate 2 of the 6 functions to numeric values in the region; see the plotting command's help page to ensure the calling sequence is correct" How to fix it?

mww.mw

Hi, 

everyone! I want to define a function with a constant (i. e., 1/3 in the following figure). Actually, I tried it by "proc" or "piecewise", but it does not work.  So could you give me some suggestions? Thanks a lot!

g:=proc(n+1/4)
  if (n=0) then 1
    else 0
  end;
end proc;

How do i solve nonlinear coupled orinary differential equation with boundary conditions?

restart;
alias(u = u(x, y, z, t), f = f(x, y, z, t));
                              u, f
u := (diff(f, y, x, x, x, x))*f^4-(diff(f, x, x, x, x))*(diff(f, y))*f^3-4*(diff(f, y, x, x, x))*(diff(f, x))*f^3+8*(diff(f, x, x, x))*(diff(f, x))*(diff(f, y))*f^2-4*(diff(f, x, x, x))*(diff(f, y, x))*f^3+12*(diff(f, y, x, x))*(diff(f, x))^2*f^2-36*(diff(f, x, x))*(diff(f, x))^2*(diff(f, y))*f+24*(diff(f, x, x))*(diff(f, x))*(diff(f, y, x))*f^2-6*(diff(f, x, x))*(diff(f, y, x, x))*f^3+6*(diff(f, x, x))^2*(diff(f, y))*f^2-24*(diff(f, x))^3*(diff(f, y, x))*f+24*(diff(f, x))^4*(diff(f, y))+2*(diff(f, y, x, t))*f^4-2*(diff(f, y, x))*(diff(f, t))*f^3-2*(diff(f, x, t))*(diff(f, y))*f^3+4*(diff(f, x))*(diff(f, y))*(diff(f, t))*f^2-2*(diff(f, x))*(diff(f, y, t))*f^3-3*(diff(f, z, x, x))*f^4+3*(diff(f, x, x))*(diff(f, z))*f^3+6*(diff(f, x))*(diff(f, z, x))*f^3-6*(diff(f, x))^2*(diff(f, z))*f^2;

Given a module A, it has a proc which is called to set some internal variable to some value. This proc is meant to be used as initialization of the module, and not meant to return anything (i.e. procedure vs. a function in other languages, where a procedure does not return anything, but a function does).

When this proc is called from outside, the result of the last assignment in the proc is returned back to the user since that is the default behavior.

Is there a way to prevent this, other than adding an explicit NULL at the end of this proc?  Adding : at the end the last statement or at the end of the proc did not prevent this.

Here is an example

restart;
A:=module()
  local r::integer;
  export set_r:=proc(r::integer)   
    A:-r:= r:   
  end proc:
end module:

Now when calling  A:-set_r(10), Maple will return back/echo back 10. But this is not something I want.

A:-set_r(10);

    10

To prevent this, currently I add NULL; at end of the proc, like this

A:=module()
  local r::integer;
  export set_r:=proc(r::integer)   
    A:-r:= r:   
    NULL;
  end proc:
end module:

and now

A:-set_r(10);

returns nothing. Well, it returns NULL but that is like nothing.

My question is, is there a better way to do this? Can one define a proc in Maple that returns nothing?

Maple 2022.1

Hi everyone

I am trying to get the maximum value (angle) for a function, which is a solution from a ODE. I tried evalf(max.. which I already thaught wouldnt work. 

After that I installed the package "DirectSearch", again with no success. 

Does anybody know what I am doing wrong or how I am going to get the maximum. I added the maple file with the direct seach attempt. 

Thank you in advance!

Please how  can I use getdata to extract only 200x2 of 208x2 matrix from maple to excel.

For instance I have:

Q:= ( seq( seq( plottools:-getdata(ans1[s1,3])[j,3],j=1..3), s1=1));

It returns  208x2,  200x2 200x2 matrices and I can't extract it using 

`<|>`( seq( seq( plottools:-getdata(ans1[s1,3])[j,3],j=1..3), s1=1));

because the matrices have different dimension. Please, how can make 208x2 matrix to 200x2?

I am trying to figure out the width-at-half-maximum for a speical case of the difference of Guaussians.  In this scenario, I start with a standard formula for a Gaussian:

f(x, x0, S, Gmax) = -Gmax*(exp(S*(x - x0)^2*(-1/2)) - 1)*heaviside(x - x0)

 

where x0, is the location of the peak, S is the spread, and Gmax is the amplitude.  However, then I take a difference of two, assuming that x0 is 0 for both, that Gmax is unity for both, and the only thing free to vary between them is the spread.

fDOG(x, S_a, S_d) = f(x, 0, S_a, 1) - f(x, 0, S_d, 1)

 

Here I also assume that S_a > S_d, and that all values (including x) are real positive numbers.  In this case, I (believe) I always get a peak function that rises from y = 0 at x = 0 to some peak, and then falls back to y = 0 at infinity.

Differentiating fDOG and solving for y = 0, I can find the time of the peak of this function:

tpeak(S_a, S_d) = (sqrt(2)*sqrt(ln(S_a/S_d))/sqrt(S_a - S_d))

 

I can then find the amplitude at the peak by substituting tpeak for x in fDOG. However, what I would like to do now is is find the (two) points on x where y = fDOG(tpeak, S_a, S_d)/2.  Is this possible?

The goal here is to verify if the solution found by Maple is valid for the submitted ODE or not. Following the usual, 'odetest' and simply plugging the solution provided by Maple into the submitted ODE did not inspire confidence, so I am writing to see where I went wrong. The steps which were followed...

restart;
Equation := 2/(3*int(diff(y(x), x)*x^2/(x^2 - 1), x)^(3/2))
            + int(sqrt(2)*sqrt(y(x)), x) = 0:

ImpliciteSol := simplify(expand(dsolve(Equation)[1]));

     1/16*(-9*y(x)*(-y(x)*x^2)^(1/3)*6^(2/3)*x^2+16*_C1*x^(8/3)-24*x^2+6)/x^(8/3) = 0

yofx := simplify(expand(isolate(ImpliciteSol[1], y(x)))); # does not succeed

    (1/16*(-9*y(x)*(-y(x)*x^2)^(1/3)*6^(2/3)*x^2+16*_C1*x^(8/3)-24*x^2+6)/x^(8/3) = 0)[1] = 0

The goal here is to verify if the solution found by Maple is valid for the submitted ODE or not. Following the usual, 'odetest' and simply plugging the solution provided by Maple into the submitted ODE did not inspire confidence, so I am writing to see where I went wrong.

First 271 272 273 274 275 276 277 Last Page 273 of 2219