Carl Love

Carl Love

28070 Reputation

25 Badges

13 years, 32 days
Himself
Wayland, Massachusetts, United States
My name was formerly Carl Devore.

MaplePrimes Activity


These are answers submitted by Carl Love

From your title, I assume that you mean that you want to plot a line with the geom3d package. But you also say "Line has coordinates x, y, and z." The geom3d:-line command has no way to specify the names of the coordinates, so I don't know what you mean.

Here's an example of how to plot in 3D a line from [1,1,1] to [2,2,2] using geom3d.

geom3d:-line(L1, [geom3d:-point(A,[1,1,1]), geom3d:-point(B,[2,2,2])]):
geom3d:-draw(L1, axes= boxed, orientation= [70,70], color= red);

 

What's your integration variable: t or x? The only way that you're going to get a numeric answer from this (which I assume is what you want---why else would you use evalf?) is if the integration variable is the same as the "free" variable in curva. If you want to maintain the generality of being able to specify the free variable at the time that the procedure is called, then you'll have to pass the free variable to both procedures. In other words, you passed t to curva, so you'll need to pass it to fun also. One way to do this that maintains most of the structure that you already have is to include t as the final return value of the sequence returned by curva and to make t the final parameter of fun. This will let you maintain the call structure fun(curva(m,t)). The code is

curva:= (m,t)-> (a-> (2*(cos(a), sin(a)*cos(a*m), sin(a)*sin(a*m)), t))(2*Pi*t);
fun:= (a,b,c,t)-> evalf(Int(add(_k*Pi*args[_k]*cos(_k*Pi*t), _k= 1..3)^2, t= 0..1));

And your example usage is

fun(curva(2,t));

     75.02998738

Update: The idea of passing around the free variable doesn't sit well with me. It seems artificial. Here's a formulation that doesn't need it.

curva:= proc(m)
local t, a:= 2*Pi*t;
     unapply~(2*[cos(a), sin(a)*~[cos,sin](a*m)[]], t)[]
end proc;

fun:= proc()
local k, t, a:= k*Pi;
     evalf(Int(sum(a*args[k](t)*cos(a*t), k= 1..nargs)^2, t= 0..1))
end proc;

fun(curva(2));

     75.02998738

It'd be better if you didn't include the output in your Questions until you learn how to properly format it. Just the input and error message are usually enough to get an Answer here on MaplePrimes.

Here's the relevant input and output upto and including the solve command.

restart:
v1 := int(cos(tau)*g(tau), tau = t0 .. t):
v2 := int(-sin(tau)*g(tau), tau = t0 .. t):
soln := C1*y1+C2*y2+v1*y1+v2*y2:
soln := combine(soln):
Eqs:= eval(soln, t = t0) = 0, eval(diff(soln, t), t = t0);
solve({Eqs}, {C1, C2});

So, those are your two equations (the second being considered to be equated to 0). The first has the variables C1 and C2. The second has neither. That pair of equations can't be solved for both C1 and C2. Notice that the solve command has given no output. That's because the equations can't be solved. So in your subsequent eval command, the % actually refers to the output of the last command which had non-NULL output, which is Eqs.

 

ex:= D[2,2](G)(x,y):
[op([0,0,..], ex)];

You can get the plot in your code if you reduce the precision of the numeric integration. This is done by changing int to Int and adding an epsilon option to the Int. Using epsilon= 1e-5 will make no visible difference in the plot. After you create the expression U, add the line

U1:= subsindets(U, specfunc(anything, int), J-> Int(op(J), epsilon= 1e-5));

Then simply use U1 instead of U in the plot command. You'll get this plot:

which shows that your iterative technique is far from the known exact solution.

 

In your second solve, one of the equations is redundant. With exact solutions, this wouldn't matter---solve would figure it out. But with floats, the redundant equation deviates enough from its exact value (for m > 3) that the system becomes inconsistent . I don't think that it's possible to solve this system exactly in any reasonable time (not sure about that). The solution is to seq the equations and variables for n= 3..m rather than n= 2..m.

There's another issue: Your second solve returns multiple solutions, even for m = 3. Your current code is ignoring all but the last of these solutions. In the code below, I return all solutions. Here's my version of your code with the two corrections. It works for m = 3, 4, 5. The m = 5 took about 20 minutes.

restart:
Digits:= 30:
m:= 3:  g:= 0.3:  nu:= 0.2:  a:= 1:
w:= unapply(add(b[n]*cos(n*r), n= 1..m), r):
W:= simplify(eval(w(r), solve((D@@2)(w)(1) + nu*D(w)(1), {b[1]}))):
d1:= diff(W,r):  d2:= diff(d1,r):
F:= int(((d2+d1/r)^2-2*(1-nu)*d2*d1/r)*r*(1+g*r/a)^3, r= 0..a)/int(d1^2*r, r =0..a):
Sol:= solve({seq(eval(diff(F,b[n]), r= n/m), n= 3..m)}, {b[n] $ n= 3..m});
FW:= map2(eval, F, [Sol]);

n:= 3:  N:= 5:
A:= Matrix((n,n), symbol= a):
B:= Matrix((n,n), symbol= b):
z:= Matrix(N-n,n):  Z:= Matrix(N,N-n):
<<A,z>|Z> + <Z|<z,B>>;

From your responses to my earlier questions, I inferred that you don't need much precision in your timer: If your program ran approximately once per minute that'd be fine. In that case, use the simple command Threads:-Sleep(60). That simply pauses the program for approximately 60 seconds. Just put that in a loop with a call to your main program.

@mapleq2013 

To understand the answers to all your questions above, you must first understand some fundamental principles of procedures. The most important is

The Non-Evaluation at Definition (NED) principle: No part of a procedure's definition is evaluated at the time that the procedure is defined (except for basic numeric arithmetic and some very trivial algebra). Evaluation only happens when the procedure is called (i.e., when it's passed arguments). In particular, at the time of definition, no non-local variable's value is determined or substituted, and no procedure invocation inside the procedure definition is executed.

An example of NED:

a:= 1:
f:= x-> x+a;

Note that the response isn't f:= x-> x+1, because the a remains unevaluated.

a:= 2:
f(1);

     3

The response is 3, not 2, because a is evaluated at the time the procedure is called. Further changes to a will continue to change how f behaves.

The second fundamental principle is

The Procedures are First-Class Objects (PFCO) principle: In Maple, procedures are first-class objects (see these Wikipedia articles: First-class function, Non-local variable, Closure, and Partial application).

In my opinion, any language that doesn't implement NED can hardly be called a language. (It'd be like a human language that didn't allow the description of hypothetical situations.) Perhaps the qualified name "macro language" could be applied to it. PFCO, however, is a very sophisticated feature (which allows for meta-programming), and there are many quite powerful languages (like C++) that don't fully implement it. A full implementation requires a sophisticated and resource-consuming runtime infrastructure that includes garbage collection.

Here's an example of both NED and PFCO.

f:= x-> 1: #Constant function.
g:= y-> y+f(0);

Because of NED, the f(0) remains unevaluated, even though it'd be trivial for the parser to replace it with 1.

g(2);

     3

(Calling g forces the call to f, obviously.)

Here's some meta-programming (albeit trivial for the sake of example): We'll change f's definition by substituting 2 for 1 inside the definition.

f:= subs(1= 2, eval(f));

That a procedure can be both the argument and the return value of subs is an example of PFCO, and a quite profound one at that. (The need for the eval in the statement above is due to an issue called last name evaluation, which is Maple specific and is completely unrelated to the rest of this post.)

g(2);

     4

Now I'll directly address your questions. You wrote:

I don't quite understand what is "to compute the determinant outside the call to fsolve".

I could've made a procedure like this---computing the determinant inside the call to fsolve:

Sol:= (Y,X)-> fsolve(LinearAlgebra:-Determinant(eval(C, [x= X, y= Y])), w, complex);

That would work, but it'd be inefficient because due to NED, the determinant would be recomputed for each numeric pair (Y,X). To avoid that, the call to Determinant must be outside the definition of Sol, and hence outside the call to fsolve.

My understanding is that, solve(Dt(y,x), w) would create a symbolic solution first and then substitute y and x later during the plotting process.

Correct. And a symbolic solution is usually a good thing----if you can get it.

That didn't work reliably since Maple wasn't able to find a symbolic solution everytime.

Correct. And I wouldn't fault Maple for that, since there's no general solution to a degree-six polynomial.

I tried to use fsolve(Dt(y,x), w) myself earlier but it tries to create numerical solutions and returned me an error.

Yes. Being a strictly numerical solver, fsolve needs to have the same number of equations as unknowns. With symbolic x and y, your Dt(y,x) is one equation with three unknowns.

I also tried to loop through all x and y first and then solve(Dt(y,x), w) ..., but it's very slow because Maple doesn't seem to handle massive loops as well as Matlab.

It has nothing to do with the loop size, and Maple handles massive loops just fine. The issue is that for specific numeric values of x and y, the solve is much slower than fsolve.

So you somehow overcame all of those problems by (y,x)-> Re(fsolve(Dt(y,x), w, complex) in my understanding by letting Maple temporarily hold x and y as variables while calling fsolve and then supply x and y values later.

Yes, that's NED.

Also wouldn't unapply be unnecessary because (y,x)-> already defines a function?

I used unapply to define the procedure (or "function") Dt, not solD, which is a separate procedure. (But you're correct that using (y,x)-> does define a procedure ("function").)

(By the way, your usage of "function" is totally correct from a computer-science viewpoint. Those Wikipedia articles will quickly confirm that. It's just slightly imprecise (although not incorrect) from a Maple viewpoint. So don't be offended that I put quotes on "function". I'll just continue using "procedure" when I want to be precise.)

Now, like I said in an earlier Answer, I could've avoided the use of unapply and still maintained efficiency. It'd be done like this:

Dt:= LinearAlgebra:-Determinant(C);
Sol:= (Y,X)-> fsolve(eval(Dt, [x= X, y= Y]), w, complex);

In the above, Dt is merely an expression, not a procedure. I find that a bit awkward because of the need to use symbols X and Y (as procedure parameters) distinct from x and (which, in this case, are global unevaluated symbols). By using unapply, I convert x and y themselves into the parameters. (They still also exist as globals; they're only parameters inside Dt.)

For the subs being necessary in solD||n:= (y,x)-> Re(fsolve(Dt(y,x), w, complex)[n]): This is my long standing confusion with using Maple to do loops.

I don't see what it has to do with loops. Your previous confusion was due a lack of understanding NED and PFCO.

Because I have some experience progamming with C++ and in there you don't need to do subs each time

I wasn't aware that a compiled language like C++ even had or could have anything like subs. Even if it does, I'd be quite surprised if it could be applied to a function.

I understand Maple is not a progamming language.

Au contraire. Maple is a package that includes a programming language, which is also called Maple---and it's an extremely powerful and rich language. Even if you were to throw away all of Maple's higher mathematical functionality, the plotting, and the GUIs (with all their bugs), you'd still be left with one of the most powerful computer languages ever developed---which nonetheless has an intuitive, simple, and highly consistent syntax. The Maple language is implemented in the kernel, which has much fewer bugs, and the bugs are addressed much more quickly.

what puzzles me is that how Maple doesn't substitute the n directly?

That it doesn't substitute it directly is NED. That one is able to substitute it later is PFCO.

and I think in other languages you don't need to do that either.

Like I said before, any "language" that doesn't have NED hardly qualifies to be called a language. However, there are many ways to implement the substitution (the PFCO aspect) without having a direct subs command that works on procedures. These alternatives can be used in Maple also. One (crude) way is shown in my first example of NED above: You change the value of a global variable, that variable being n for the case at hand. So it'd work like this:

solD:= (y,x)-> fsolve(Dt(y,x), w, complex)[n];
for n to 6 do
     plot3d(solD, -Pi..Pi, -Pi..Pi)
end do;

I say that this method is crude because it relies on a global variable whose usage in the body of the loop is not explicitly apparent. Any code reviewer should flag this and tell the programmer "Your index variable n is not used in the loop."

A more sophisticated approach would be to make both Sol and n exports of a module. This is akin to a class in C++.

Wait, sometimes it does substitute directly because I was looping through x and y and the numbers did go in solve(Dt(y,x), w).  So yea I'm confused here ...

That's because solve(Dt(y,x), w) isn't a procedure definition; thus NED doesn't apply. These issues have nothing to do with loops.

There are a great many ways to do that.

1. My preference:

V:= [2,8,14]:
T:= tau-> 440*(1 - exp(-1/tau)):
t:= evalf(T~(V));

2. A way that uses your original T (without turning it into a procedure):

T:= 440*(1 - exp(-1/tau)):
t:= evalf([seq(eval(T, tau= x), x= V)]);

3. My guess as to what most Maplers would do:

t:= map(x-> evalf(eval(T, tau= x)), V);

Note that eval(T, tau= a) is preferable to subs(tau= a, T).

 

 

You simply need more digits of precision to fsolve this. After restart, set Digits:= 15. Then the fsolve returns

There are no differences.

As implied by Rouben, Maple doesn't handle symbolic matrices. However, if you specify just the size of the Matrix, then the Matrix's contents can be completely symbolic. Then your product rule for derivatives can be verified for that size Matrix like this:

A:= Matrix((3,3), (i,j)-> a[i,j](t)):
B:= Matrix((3,3), (i,j)-> b[i,j](t)):
C:= A^+.B:
diff~(C, t) - (diff~(A^+, t).B + A^+.diff~(B, t));

Update: I apologize that my code is functionally equivalent to Tom Leslie's. A vote up for him. The codes were written independently and simultaneously.

Your error is in statements such as

a[0] := mu*lambda*sqrt(-6*a);

The a and a[0] are not independent variables. The second a needs to be some other variable.

You may have the highest number of errors that I've ever seen in a single line of code that didn't yield an error message.

This is your line, copied as 1D input:

A[k]:= ((d^k/`d&lambda;`^k)[F(sum(lambda^i*e^y[i], i= 0..k))], lambda= 0)/k!

And this is my corrected version:

A[k]:= eval(diff(F(add(lambda^i*exp(y[i]), i= 0..k)), [lambda$k]), lambda= 0)/k!

The errors:

  1. You need to use diff or D for derivatives; d won't do it,
  2. Your dlambda is a single variable.
  3. The arguments to the diff need to be in parentheses.
  4. Square brackets can't be used for algebraic grouping.
  5. The exponential function is denoted exp(y[i]), not e^y[i].
  6. You need wrap it all in the eval command to apply the condition lambda= 0.

Here are the results of my version:

n:= 3:
for k from 0 to n do
     A[k]:= eval(diff(F(add(lambda^i*exp(y[i]), i= 0..k)), [lambda$k]), lambda= 0)/k!
end do;

                        

First 250 251 252 253 254 255 256 Last Page 252 of 395