Carl Love

Carl Love

28035 Reputation

25 Badges

12 years, 317 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

Try command eliminate instead of solve. In cases where solve returns nothing, eliminate returns partial solutions and residual equations. Pay particular attention to the number of these residual equations.

You can get the matrix from Excel to Maple by

M:= ExcelTools:-Import(...);

You can plot that matrix directly (converting to ordered triples not necessary) by

Statistics:-ScatterPlot3D(M, lowess, ...);

See help pages ?ExcelTools,Import and ?Statistics,ScatterPlot3D. In particular, ScatterPlot3D has many options for refining the plot.

The reason why it doesn't work is explained in detail in the 5th paragraph of Description at ?patmatch. In particular, it says at the very end of that paragraph that the condition can't use <>. So, if I wanted to use a conditional clause to do what you want, I'd do

MyPat1:= (e, la::name)->
   patmatch(
      e, 
      conditional(
         a::anything*x^b::anything,
         _type(a, Not(identical(y))) and _type(b, Not(identical(y)))
      ),
      `if`(nargs > 1, la, NULL)
   )
:

However, it's just easier to avoid the conditional altogether, this way:

MyPat2:= (e, la::name)-> 
   patmatch(
      e, 
      a::Not(identical(y))*x^b::Not(identical(y)), 
      `if`(nargs > 1, la, NULL)
   )
:

In addition to the knowledge about conditional, you should learn from this Answer that there is a type identical and that the negation of any type T is also a type, Not(T).

Your interpretation of the problem is correct. However, I object to the wording of the problem. A sphere is a surface, not a volume. The correct word for the 3-D region enveloped by a sphere is ball.

To avoid repetition in your calculation, which increases the risk of mistyping something, I'd arrange it like this:

f:= (x, y, z)-> (x^2 + y^2 + z^2)^2:
Spherical:= [x,y,z]=~ rho*~[sin(phi)*cos(theta), sin(phi)*sin(theta), cos(phi)];
V:= [rho= 0..r, theta= 0..2*Pi, phi= 0..Pi]:
(M, d):= VectorCalculus:-Jacobian(rhs~(Spherical), lhs~(V), 'determinant'):
d:= simplify(abs(d));
J:= Int(eval(f(x,y,z), Spherical)*d, V);
soln:= value(J);

 

You can call solve using keyword explicit as the third argument. Or you can use command allvalues to convert RootOfs after they are generated.

Your definition for G needs multiplication signs, just like you have in your definition for M.

Parameters can have default values set in the procedure header. I'd change the third parameter, d, to

{D::truefalse:= false}  #must be in curly braces

and change if d <> D to if not D

That's all that you need to do.

Now you can include or omit your third arguments. So, it'll work unchanged for all existing code, and for new code, you only need to put a third argument if it's a draw.

 

 

It's great that you found applyrule and made it work.

Since I already wrote the subsindets command for this situation, I'll post it anyway. It is much more complicated than your applyrule, but it'll find any integral whose integrand contains x^n::posint.

MyIntSubs:= Expr-> 
   subsindets(
      Expr, 
      And(
         specfunc(anything, {Int,int}), 
         patfunc(satisfies(J-> hastype(J, identical(x)^integer)), anything)
      ),
      J-> m[op([1,2], indets(J, identical(x)^integer))](t)
   )
:

And use it as MyIntSubs(expr) where expr is any expression, possibly containing the relevant integrals or multiple instances of them.

That's an interesting example, but it's easily explainable. Eigenvalues are unique, but eigenvectors are not: Any nonzero scalar multiple of an eigenvector is also an eigenvector. Different algorithms may present the eigenvectors in different forms. Maple chooses an algorithm based on the type of data in the Matrix. By including terms of the form 0*0.1, you make it choose a floating-point algorithm. Without including those, a purely algebraic algorithm is used: finding the roots of a cubic polynomial. If you normalize the two sets of eigenvectors, they'll be the same (upto a bit of round-off error).

Since your integral is with respect to eta, I don't think that anything can be said in general without knowing u. If the integral was with respect to u (as a name, rather than as a function of eta), then it's trivial as far as Maple is concerned: The antiderivative is expressed in terms of the roots of the denominator, regardless of the discriminant and regardless of whether the roots can computed exactly. If p(x) is any polynomial, then

int(1/p(x), x) = add(ln(x-r)/D(p)(r), r= [the roots of p(x)]);

If a first-degree linear model z = b0 + b1*x + b2*y doesn't fit well, the usual next thing to try is including first-degree products of the independent variables. In this case, that makes the model z = b0 + b1*x + b2*y + b3*x*y. Using this for the data that you present, all of the coefficients are highly significant (max p-value = 0.0024):

Statistics:-LinearFit([1, x, y, x*y], <X | Y | Z>, [x,y], summarize);
Summary:
----------------
Model: -2057.7133+988.24655*x+6195.9003*y-2973.2493*x*y
----------------
Coefficients:
              Estimate   Std. Error   t-value  P(>|t|)
Parameter 1   -2057.7133    495.8423      -4.1499   0.0011
Parameter 2    988.2466     203.4795       4.8567   0.0003
Parameter 3    6195.9003    1649.5165      3.7562   0.0024
Parameter 4   -2973.2493    673.0242      -4.4177   0.0007
----------------
R-squared: 0.7911, Adjusted R-squared: 0.7428

With (1-R^2) = 21%, there may still be, as you say, "great amount of standard errors". That may or may not be relevant. The residuals should be analyzed: Are they random and normally distributed? If yes, then it may not be possible to get a better model.

Also consider:

Statistics:-CorrelationMatrix(<X | Y | X*~Y | Z>);

These are only my newbie suggestions from an undergraduate-level knowledge of statistics. Mmcdara/Sand has deeper ideas.

 

Maple offers many subtle ways to delay evaluation so that the evaluation of specific subexpressions can be triggered at precise future times. Here are some examples:

restart:
a:= 3:
Template:= x-> a*e:
f:= subs(['a'= a, e= x^2], eval(Template));
                                            2
                               f := x -> 3 x 
restart:
Instantiate:= (Template::procedure, N::list(name))->
   subs(N=~ eval(N), eval(Template))
:
a:= 3: e:= x^2:
meta_f:= x-> a*e:
V:= '['a', 'e']': #2 layers of unevaluation quotes needed  
f1:= Instantiate(meta_f, V);
                                             2
                               f1 := x -> 3 x 
a:= 4: e:= x^3:
f2:= Instantiate(meta_f, V);
                                             3
                               f2 := x -> 4 x 

So, note that the exact same line of code, Instantiate(meta_f, V), has been invoked twice, with no direct change having been made to any of its names (Instantiate, meta_f, and V) in the meantime, and the result is two independent procedures, f1 and f2.

Like the other two respondents, I recommend that you use unapply for the particular situation that you present. However, there is a way to do it that is close to what you were trying, that is as efficient as possible, and that can be applied in some situations where unapply won't work:

e:= x^2: #Any expression that depends on x
f:= subs('F'= e, x-> F);

This must be done with subs rather than eval. The quotes around the F are optional. They'll make it work even if F is already defined. The e in the second line could be your %, but I think that it's best to avoid using % in finished code (i.e., something that's not scratch work).

 

In those cases where it's possible to get rid of the integral by taking a derivative, as VV has done, I recommend that you do that. However, those cases seem relatively uncommon (at least among the Questions that we get here). Usually the integrand also contains z, and then taking the derivative just makes it a more-complicated expression that still contains an integral. For those cases, do this:

INT:= proc(z)
local t;
   if not [args]::list(numeric) then return 'procname'(args) fi;
   evalf(Int(t^t, t= 0.1..z))
end proc:
   
Sys:= {diff(z(x),x) = x  + INT(z(x)), z(0.1)=0.1}:
Sol:= dsolve(Sys, numeric, known= [INT], range= 0.1..1);

The procedure must return unevaluated (rather than give an error) when passed nonnumeric arguments, so you need the line

if not [args]::list(numeric) then return 'procname'(args) fi;

(Those quotation marks are mandatory!)

If you want to add method selection and error control to the integral, that's fine.

I'm not sure why your solve didn't find all the solutions, but I suspect that it only looks for real solutions in equations that contain abs. What I usually do in cases like this is

  • set z= x+I*y
  • use evalc to extract the real and imaginary parts (Re and Im)
  • solve these as a pair of simultaneous equations
  • recompose z = x+I*y

Doing that in this case, I do get three solutions: -1, and two very long algebraic numbers of degree 3. I've verified that these give 0 in the original equation. I consider it necessary to verify the solutions in cases like these. I don't think that it's possible to make solve respect assuming real.

Here are my commands:

f:= z-> abs(z)*(z-4-I)+2*I - (5-I)*z:
Z:= x+I*y:
simplify~(eval~(Z, {solve(evalc~({Re,Im}(f(Z))), {x,y}, explicit)}));

That is a hard test problem! What is the level of the course?

First 170 171 172 173 174 175 176 Last Page 172 of 395