Carl Love

Carl Love

28155 Reputation

25 Badges

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

MaplePrimes Activity


These are answers submitted by Carl Love

Suppose that your list of equations is copied from Matlab as a single string, which I'll call MS (Matlab String), like this

MS:= "C_p_e = C_state/C_c;
C_p_f = I_state/I_i;
R_p_e = R_r*C_p_f;
I_p_e = (Se_p_e-R_p_e)-C_p_e;
";

The presence of newline characters in the string is insigificant---Maple ignores them. The semicolons, however, are necessary. Then do

eq:= parse~(StringTools:-Split(MS, ";"));

Now eq is a list of all the equations, so eq[1] is C_p_e = C_state/C_c, etc. You just need to use subs once, on all of eq. It's more efficient than calling allsubs repeatedly.

eq_a:= subs(
     C_p_e=fs(t), C_p_f=v(t), R_p_e=fd(t), I_p_e=fm(t), C_c=1/k, I_i=m, 
     R_r=c, Se_p_e=fe(t), I_state=int(fm(t),t), C_state=int(v(t),t),
     eq
);

The solutions are not necessarily real in your example. If the solutions were necessarily real, you could use syntax almost identical to the Mathematica:

a:= INTERVAL(2..4):
b:= INTERVAL(10..11):
c:= INTERVAL(1..3):
evalr~([solve(a*x^2+b*x+c)]);

If you want to do complex range arithmetic with your original example, you could do this:

a:= INTERVAL(2,0,4,0):
b:= INTERVAL(1,0,3,0):
c:= INTERVAL(1,0,3,0):
evalrC~([solve(a*x^2+b*x+c)]);

However, the results are not perfect, because they are in floating point. Note how a complex interval is specified with four numbers. See ?evalrC and ?evalr.

simplify(x1=a-y1-d*y2, {a-y2-d*y1= x2, 1-d^2= b, a-a*d= c});

Note that the three substitution equations must be written with the expressions on the left and the single variables on the right.

Don't use map when applying expand or simplify to a single expression. Use map to distribute these operations over a Matrix, but not on an single entry extracted from a Matrix.

There are also "tutors" for this in the Student package, but I think that it's instructive to see the components of a plot command.

 

restart:

 

Your first question: Plotting a function and one of its tangent lines.

f:= x-> 2*cos(x)-x^2:  a:= 2:  r:= 3:

 

Here I use a columnar layout to emphasize the parallelism between the plot components and their attributes.

plot(
                 [f(x),  D(f)(a)*(x-a)+f(a), [[a,f(a)]]], x= a-r..a+r,
      style=     [line,  line,               point     ],
      color=     [red,   blue,               green     ],
      linestyle= [solid, dot,                solid     ],
      symbolsize= 16, symbol= solidcircle,
      gridlines= false
);

Your second question: Plotting a rational function, its derivative, vertical asymptotes, and critical points.

f:= x-> (x^3-10*x^2-2*x+1)/(4*x^3+5*x+1);

proc (x) options operator, arrow; (x^3-10*x^2-2*x+1)/(4*x^3+5*x+1) end proc

Find the vertical asymptotes as the zeros of the denominator.

VA:= fsolve(denom(f(x))=0);

-.194145720502372

Find the critical points as the zeros of the numerator of the derivative.

CP:= fsolve(numer(D(f)(x))=0);

-1.47480661500595, 1.14215480870403

Get the horizontal domain, including all x-values of interest)...

(a,b):= (min,max)(VA,CP);

-1.47480661500595, 1.14215480870403

...and stretch it a little.

HR:= a-.3*(b-a) .. b+.3*(b-a):

 

And the same for the vertical range.

(a,b):= (min,max)(f~([CP]))[];  YR:= a-.3*(b-a) .. b+.3*(b-a);

-1.01333055349349, 1.09390932026410

-1.64550251562077 .. 1.72608128239138

plots:-display([
     #Use option discont for plots with vertical asymptotes:
     plot([f(x), D(f)(x)], x= HR, discont, legend= ["f", "f '"]),
     #Use parametric plots for vertical lines:
     map(va-> plot([va, t, t= YR], color= red, linestyle= dash), [VA])[],
     map(cp-> plot([[cp,f(cp)]], style= point, color= green), [CP])[]
     ],
     view= [DEFAULT, YR], gridlines= false,
     symbol= solidcircle, symbolsize= 16
);

 

 

Download RatFuncPlot.mw

 

In the future, you should show what you tried. Here's how to plot it.

f:= x-> 2*cos(x)-x^2:  a:= 2:  r:= 3:
plot([f(x), D(f)(a)*(x-a)+f(a)], x= a-r..a+r, color= [red,blue], linestyle= [solid,dot]);

I hope that you can generalize it from there.

Here's a solution for an arbitrary number of dancers, using evenly spaced points on the unit circle as the starting points. The ODEs are essentially the same as in my solution above. I decided to form the dependent variable names with indexing (x[1], x[2], y[1], etc.) rather than with cat and ||. I also changed the indexing from 0..n-1 to 1..n. The brevity of the code shows Maple's great power at forming and working with a repetitive set of ODEs.

restart:
SpiralDance:= proc(n::posint)
local
     k, K:= k= 1..n, x, y, V_k:= [x[k],y[k]], v,
     eqns:= seq(((D+(X->X))(v[k]) = v[1+irem(k,n)]) $ K, v= [x,y]),
     ics:= op~([(V_k =~ [cos,sin](-2*k*Pi/n)) $ K])[],
     t, Sol:= dsolve({eqns(t),ics(0)}, numeric)
;
     plots:-odeplot(Sol, [V_k(t) $ K], thickness= 3, axes= none, gridlines= false)
end proc:

SpiralDance(5);

 

The breakpoint command in Maple is stopat. See ?stopat.

Your transform is (x,y)-> [x-y]. This says "take a point (x,y) on the graph and replace it with the single number x-y." That makes no sense: You need to replace points with points---pairs of numbers---not with single numbers.

I also encourage you to replace PLOT with plots:-display. 

There's a decimal point that you included somewhere in your input. Remove it.

Here's a simple solution, off the top of my head, with the dancers starting at [1,1], [1,-1], [-1,-1], [-1,1]. I leave animating this solution to you. It's really easy. The position of dancer k (0 to 3) at time t is [x||k(t), y||k(t)].

 

restart:

 

eqns:=
     seq(diff(x||k(t), t) = cat(x,irem(k+1,4))(t) - x||k(t), k= 0..3),
     seq(diff(y||k(t), t) = cat(y,irem(k+1,4))(t) - y||k(t), k= 0..3)
;
ics:=
     x0(0) = 1, x1(0) = 1, x2(0) = -1, x3(0) = -1,
     y0(0) = 1, y1(0) = -1, y2(0) = -1, y3(0) = 1
:
Sol:= dsolve({eqns,ics}, numeric):

diff(x0(t), t) = x1(t)-x0(t), diff(x1(t), t) = x2(t)-x1(t), diff(x2(t), t) = x3(t)-x2(t), diff(x3(t), t) = x0(t)-x3(t), diff(y0(t), t) = y1(t)-y0(t), diff(y1(t), t) = y2(t)-y1(t), diff(y2(t), t) = y3(t)-y2(t), diff(y3(t), t) = y0(t)-y3(t)

plots:-odeplot(
     Sol, [seq([x||k(t), y||k(t)], k= 0..3)], t= 0..5,
     thickness= 3, axes= none, gridlines= false
);

dsolve({eqns,ics});

{x0(t) = exp(-t)*sin(t)+exp(-t)*cos(t), x1(t) = exp(-t)*cos(t)-exp(-t)*sin(t), x2(t) = -exp(-t)*sin(t)-exp(-t)*cos(t), x3(t) = -exp(-t)*cos(t)+exp(-t)*sin(t), y0(t) = exp(-t)*cos(t)-exp(-t)*sin(t), y1(t) = -exp(-t)*sin(t)-exp(-t)*cos(t), y2(t) = -exp(-t)*cos(t)+exp(-t)*sin(t), y3(t) = exp(-t)*sin(t)+exp(-t)*cos(t)}

 

 

Download Spriral_Dance.mw

Try the old profiling commands: exprofile, excallgraph, profile. I don't have much experience with profile. I've used exprofile and excallgraph a lot, and I've never come across code that they couldn't handle.

The mathematical constant Pi is spelled with a capital P in Maple. If you spell it pi, it's just another variable, which, unfortunately, prettyprints exactly the same as the mathematical constant. However, the printed forms can be distinguished using the lprint command.

A (nonconstant) function of a random variable is itself a random variable. So your Y is already a random variable and there's no need to apply RandomVariable to it. Skip T1, and do what you want with Y, such as 

simplify(PDF(Y,t));

or, more directly,

simplify(PDF(1/X,t));

 

restart:

 

MaxAbsWithIndex:= proc(M::Matrix(numeric), col::posint)
# Returns the entry with maximal absolute value in column col
# and its row index.
local
     Max:= -infinity, i, MaxI, v, absv, Maxv,
     Rows:= proc(M) option inline; op([1,1],M) end proc
;
     for i to Rows(M) do
          v:= M[i,col];
          absv:= abs(v);
          if absv > Max then
               Max:= absv;
               MaxI:= i;
               Maxv:= v
          end if
     end do;
     (Maxv, MaxI)
end proc:

 

Pivot:= proc(M::Matrix(numeric), row::posint, col::posint)
uses RO= LinearAlgebra:-RowOperation;
local Max,MaxI;
     (Max,MaxI):= MaxAbsWithIndex(M, col);
     RO(RO(M, [row,MaxI], _rest), row, 1/Max, _rest)
end proc:
          

A:= LinearAlgebra:-RandomMatrix(3,4);

A := Matrix(3, 4, {(1, 1) = -32, (1, 2) = 27, (1, 3) = 99, (1, 4) = 92, (2, 1) = -74, (2, 2) = 8, (2, 3) = 29, (2, 4) = -31, (3, 1) = -4, (3, 2) = 69, (3, 3) = 44, (3, 4) = 67})

A1:= Pivot(A, 1, 1);

A1 := Matrix(3, 4, {(1, 1) = 1, (1, 2) = -4/37, (1, 3) = -29/74, (1, 4) = 31/74, (2, 1) = -32, (2, 2) = 27, (2, 3) = 99, (2, 4) = 92, (3, 1) = -4, (3, 2) = 69, (3, 3) = 44, (3, 4) = 67})

A;

Matrix(3, 4, {(1, 1) = -32, (1, 2) = 27, (1, 3) = 99, (1, 4) = 92, (2, 1) = -74, (2, 2) = 8, (2, 3) = 29, (2, 4) = -31, (3, 1) = -4, (3, 2) = 69, (3, 3) = 44, (3, 4) = 67})

Pivot(A, 1, 1, inplace);

Matrix(3, 4, {(1, 1) = 1, (1, 2) = -4/37, (1, 3) = -29/74, (1, 4) = 31/74, (2, 1) = -32, (2, 2) = 27, (2, 3) = 99, (2, 4) = 92, (3, 1) = -4, (3, 2) = 69, (3, 3) = 44, (3, 4) = 67})

A;

Matrix(3, 4, {(1, 1) = 1, (1, 2) = -4/37, (1, 3) = -29/74, (1, 4) = 31/74, (2, 1) = -32, (2, 2) = 27, (2, 3) = 99, (2, 4) = 92, (3, 1) = -4, (3, 2) = 69, (3, 3) = 44, (3, 4) = 67})

 

 

Download Pivot.mw

First 255 256 257 258 259 260 261 Last Page 257 of 396