## 7776 Reputation

17 years, 361 days

## Functions rather than indexed names...

Finite differences work more naturally in Maple if we use function form rather than indexed name for the variables. Here is an illustration.

Finite differences in Maple

Finite difference calculations work more naturally in Maple

if  is expressed as a function .

 > restart;

and

 > dx := (i,j) -> (u(i+1,j) - u(i,j))/h; dy := (i,j) -> (u(i,j+1) - u(i,j))/h;

and

 > (dx(i,j) - dx(i-1,j))/h: simplify(%): d2x := unapply(%, i, j); (dy(i,j) - dy(i,j-1))/h: simplify(%): d2y := unapply(%, i, j);

 > (d2x(i+1,j) - d2x(i,j))/h: simplify(%): d3x := unapply(%, i, j);

The Laplacian

 > d2x(i,j) + d2y(i,j): simplify(%): Lap := unapply(%, i, j);

The x derivative of the Laplacian

 > (Lap(i+1,j) - Lap(i,j))/h: simplify(%);

## Making a phase portrait...

I can't tell whether you have picked the coefficients of the DEs based on some specific need or just randomly.  The initial conditions and the plotting range are certainly picked randomly.  A phase portrait is interesting near the equilibria.  You should indentify the locations of the equilibria and then set up the plotting scene near that location.

Have a look at this modified version of your worksheet.

 > restart;
 > with(plots):
 > with(DEtools):

The vector field:

 > rhs1 := (A,l,S) -> `αAP`*A - `μA`*A; rhs2 := (A,l,S) -> `αlS`*l*S - `μl`*l - `βlA`*l*A; rhs3 := (A,l,S) -> `αS`*S - `βSl`*l*S - 0.25*S*A;

Equilibria:

 > equilibria := solve([rhs1(A,l,S), rhs2(A,l,S), rhs3(A,l,S)], [A,l,S]);

The differential equations:

 > de1 := diff(A(t), t) = rhs1(A(t),l(t),S(t)); de2 := diff(l(t), t) = rhs2(A(t),l(t),S(t)); de3 := diff(S(t), t) = rhs3(A(t),l(t),S(t));

Parameters:

 > `αAP` := 2; `μA` := 1; `αlS` := 2; `μl` := 0.2; `βlA` := 0.5; `αS` := 100; `βSl` := 1; `βSA` = 0.25;

Where are the equilibria?

 > equilibria;

Pick initial conditions near the second equilibrium:

 > ICs := seq([A(0)=0,l(0)=z1,S(0)=0.3], z1=90..110, 10),                 seq([A(0)=0.1,l(0)=z1,S(0)=0.15], z1=90..110, 10);

How many initial conditions?

 > nops([ICs]);

Specify colors for plotting orbits, one color per initial condition:

 > DEplot3d([de1, de2, de3], [A,l,S], t=0..3, [ICs],         linecolor=["Gold", "Cyan", "Magenta", "Red", "Green", "Blue"],         thickness=5, stepsize=0.01, arrows=hybrid, dirgrid=[5,5,5],         animatecurves=true);

Warning, numpoints adjusted from 301 to 313 for animation

 >

## This ought to do it...

 > restart;

Rounds the entries of the matrix A to n decimal places.

Removes trailing zeros.

 > doit := proc(A::Matrix, n::posint)         map[3](sprintf, "%.*g", n, A);         parse~(%); end proc:

Give it a try:

 > A := Matrix(3,3, (i,j) -> 1.0/(i+j));

 > doit(A, 4);

## Solution...

 > restart;
 > de := (diff(H(K),K))^3+4*K^4*H(K)^4*diff(H(K),K)+8*K^4*H(K)^5=0;

The differential equation above is not well-defined.  A properly defined first order

differential equation should be of the form dH/dK = something.  Therefore we

begin with solving your equation for the derivative:

 > convert(de, D); sol := [ solve(%, D(H)(K)) ];

 > nops(sol);

We see that there are three possible solutions.  Two of them are complex-valued.

We discard those and keep what is left:

 > DE := diff(H(K),K) = simplify(remove(has, sol, I))[] assuming K > 0;

This is the differential equation that you should be working with.
Let's solve it:

 > dsol := dsolve({DE, H(0)=1/2}, numeric);

 > plots:-odeplot(dsol, K=0..7);

## Here it is...

 > restart;
 > with(plots):

We wish to determine the equation of the surface obtained by rotating
the line :  about the line , and plot that surface.

We observe that the rotation axis,  may be parameterized as
or equivalently
, where :

 > u := < 1, 0, 1 >; L := t -> <0,0,-1> + u*t;

Introduce the mutually orthogonal unit vectors  and , both of which
are also orthogonal to
:

 > v := <0,1,0>;

 > LinearAlgebra:-CrossProduct(u,v): w := %/sqrt(% . %);

Also observe that the line  may be parametrized as

 > C := s -> <0,s,s>;

For any point  on the line  there is a point  on the line  so that the line segment

is orthogonal to ,  that is,  is the nearest point on the line  to the point .

Let ,  and We determine  by asserting that

is perpendicular to :

 > P := <0, s, s>; Q := ; (Q - P)^+ . u = 0; isolate(%, t); Q := eval(Q, %);

As the line  rotates about , the point  traces a circle about the point .

The circle's radius is the length of :

 > r := sqrt(simplify((Q - P)^+ . (Q-P)));

The entire surface of revolution consists of a family of such circles.  We conclude that

the parametric equation of the surface of revolution is

 > S := Q + r*(v*cos(q) + w*sin(q));

Plotting

 > display(         tubeplot([seq](L(t)), t=-1..2, radius=0.06, color="Red"),   # the line L         tubeplot([seq](C(s)), s=-2..2, radius=0.06, color="Green"), # the line C         plot3d(S, s=-2..2, q=-Pi..Pi, color="Gold"),         scaling=constrained, style=surface, lightmodel=light2);

## Spurious solutions...

You are right, the equation has no (real) solutions.

If you were solving that equation by hand, you would square both sides and get

`x^2 - 10*x + 1 = -8*x^2 + 9*x - 1`

and then solve the quadratic and obtain x = 1/9, 2.  Then you would substitute these into the original equation to check whether they solve the original equation.  If it happens that they don't, they are referred to as spurious solutions and should be thrown out.

In the present case, x = 1/9 and 2 are both spurious.  Ideally Maple should check for that but it doesn't.  Just be aware of the issue and check it yourself.

## Linear or nonlinear?...

Z1T = ZL*(I1T + I2)/I1T

which is equivalent to

Z1T*I1T = ZL*(I1T + I2)

The left-hand side of that equation is the product of two of your unknowns:

vars_unknown := [V1, I1T, V1T, Z1, Z1T]

and therefore your system of equations is nonlinear.  It's no wonder that
LinearAlgebra:-GenerateMatrix produces nonsense.

It seems that you believe that the system of equations should be linear.  If so, then you must have entered some of the equations incorrectly.  Check!

## Use the "latex" command...

After you calculate your w, do:

`latex(w);`

That produces a latex code for your expression.  Copy and paste that code into your latex document.

## Here is a sample solution...

I will show you how to solve the problem if we are looking for a quadratic (2nd degree) polynomial that goes through three prescribed points.  I leave it to you to adapt it to the case of a cubic (3rd degree) polynomial that goes through four prescribed points.

 > restart;

Want to fund a quadratic function  as in:

 > f := x -> a[2]*x^2 + a[1]*x + a[0];

so that it fits the following data:

 > eqns := { f(-2) = -5, f(1) = 1, f(4) = 61 };

Solve that system of equations to get the quadratic's coefficients:

 > the_coeffs := solve(eqns);

Substitute the calculated coefficients into the quadratic's formula

to see what it looks like:

 > y = eval(f(x), the_coeffs);

## Palette...

You will find that in the Common Symbols palette, fourth row, fourth column.

## Three equations...

In your failed version you attempt to solve three equations for two unknowns.  That's not good.  What you want is to solve your three equations for three unknowns, as in:

`solve({I__cm = m*r^2, r*T = I__cm*a/r, g*m - T = m*a}, {T, a, I__cm});`

## This ought to do it:p := v -> R*T/(v-...

This ought to do it:

```p := v -> R*T/(v-b) - a/v^2;
numer(p(v)) = 0;```

The result is -R*T*v^2 - a*b + a*v = 0, which quadratic in v.  There is no v^3 term.

## A couple of errors...

A couple of errors:

1. In your equations you have variables named mu and mu[1].  That's bad.  One equation declares mu as a scalar, the other makes it an array.  Hence the confusion.  If you want a subscripted mu1, write it as mu__1 (note the two underscore characters!) not mu[1].  That will not conflict with the variable named mu.
2. Your equations involve a variable T.  It needs to be assigned a numerical value.

`dsn := dsolve(eval({eqn1, eqn2, eqn3, eqn4, i(0) = 11437, r(0) = 1077, s(0) = 1770000, t(0) = 1087}), {i(t), r(t), s(t), t(t)}, numeric)`

Aren't you interested in seeing the system of equations that you are passing to dsolve() to solve?  In general, you should be.  So define:

`sys := eval({eqn1, eqn2, eqn3, eqn4, i(0) = 11437, r(0) = 1077, s(0) = 1770000, t(0) = 1087}), {i(t), r(t), s(t), t(t)};`

and then examine sys to make sure that what you are passing to dsolve() is what you really mean, and only then do:

`dsolve(sys, numeric);`

## Will this do?...

Let's say you have y = f(x) for some function f.  Then dy / dx = f '(x) is a (trivial) differential equation which has y = f(x) as a solution.  You don't need Maple to tell you that.

For instance, if y = cos (x),  then dy/dx = − sin (x) meets your requirements.

## Solution through the method of lines...

I had noted earlier that it would be possible to solve your system of equations through the method of lines.  Now I have worked out the details.  See if this worksheet makes sense.

 > restart;
 > with(plots):

The PDEs

I have changed the notation somewhat in order to get consistency

across the board.  Here are the changes:

I solve the system in the spatial range .  Furthermore, I have changed
the forcing term  to  so that at  it

matches the initial condition .

The PDEs are given as equ1, equ2, equ3:

 > equ1 := diff(v(x,t),t) = phi__7*diff(v(x,t),x\$2) + phi__7*diff(v(x,t),t,x\$2) - phi__8*M*v(x,t) + phi__9*Gr*theta(x,t) + phi__10*Gm*phi(x,t); equ2 := diff(theta(x,t),t) = lambda__f*diff(v(x,t),x\$2)/(Pe*phi__11); equ3 := diff(phi(x,t),t) = (1-eta)*diff(phi(x,t),x\$2)/Sc;

We are going to solve this system on the interval  for a prescribed

subject to prescribed initial and boundary conditions

 > ic_v := v(x,0) = v__0(x); ic_theta := theta(x,0) = theta__0(x); ic_phi := phi(x,0) = phi__0(x); bc_v := v(0,t) = v__left(t), v(L,t) = v__right(t); bc_theta := theta(0,t) = theta__left(t), theta(L,t) = theta__right(t); bc_phi := phi(0,t) = phi__left(t), phi(L,t) = phi__right(t);

The system is not a "standard" PDE and it requires a bit of manipulation

to put it in a manageable form.  Toward that end,

we introduce a new variable u through

 > pde0 := u(x,t) = v(x,t) - phi__7*diff(v(x,t),x\$2);

or equivalently:

 > eq := isolate(pde0, diff(v(x,t),x\$2));

Using the expression above to eliminate the second derivative of v from equ1.  Call the result pde1:

 > diff(eq, t): subs(%, equ1): subs(eq, %): pde1 := isolate(%, diff(u(x,t),t));

Similarly, eliminate the second derivative of v from equ2.  Call the result pde2:

 > pde2 := subs(eq, equ2);

Keep equ3 as is.  Call it pde3:

 > pde3 := equ3;

Equations pde0, pde1, pde2, pde3 form a system of four PDEs in the
four unknowns .  It's not a standard PDE because pde0 has
no time derivative.

Let's observe that the initial condition
on  may be determined from its definition:

 > ic_u := subs(t=0, diff(ic_v, x\$2), ic_v, pde0);

 >

Discretization through the method of lines

We apply the method of lines to solve the PDEs.  Toward that end, we partition the

interval  into  subintervals of equal lengths  through the
equally-spaced points .
We discretize the system's unknowns in space, through

Furthermore, we discretize the 2nd order derivatives through centered differences,

as in
,
and similarly for .
Here we fix the value of  and express the PDEs and the initial
and boundary conditions in terms of the discretized variables.
We choose a small  for this demonstration but  will be larger

for practical applications.

 > n := 8;   # change as needed

The initial condition on :

 > convert(ic_u, D): subs(x=h*j, %): IC_U := seq(U[j](0) = rhs(%), j=1..n);

These equations involve  and  which are known from the boundary conditions,

 > V[0](t) = subs(bc_v, v__left(t)): V[n+1](t) = subs(bc_v, v__right(t)): BC_V := %%, %;

Shortly we will need the boundary conditions on  so let's calculate them now:

 > Phi[0](t) = subs(bc_phi, phi__left(t)): Phi[n+1](t) = subs(bc_phi, phi__right(t)): BC_Phi := %%, %;

Now we turn to pde0, pde1, pde2, pde3, and express them in discretized forms:

 > DE0 := U[j](t) = V[j](t) - phi__7*(V[j-1](t) - 2*V[j](t) + V[j+1](t))/h^2;

 > DE1 := diff(U[j](t),t) = -U[j](t) + V[j](t) - phi__8*M*V[j](t) + phi__9*Gr*Theta[j](t) + phi__10*Gm*Phi[j](t);

 > DE2 := diff(Theta[j](t),t) = lambda__f/(phi__7*Pe*phi__11)*(-U[j](t) + V[j](t));

 > DE3 := diff(Phi[j](t),t) = (1 - sigma)/Sc*(Phi[j-1](t) - 2*Phi[j](t) + Phi[j+1](t))/h^2;

We apply each of DE0 through DE3 at the  interior nodes  and thus obtain a system of

equations in the  unknowns.   The system involves the values

of  and  at the domain's left and right boundaries but we eliminate them through

substituting their values from the boundary conditions BC_V and BC_Phi.

 > seq(DE0, j=1..n), seq(DE1, j=1..n), seq(DE2, j=1..n), seq(DE3, j=1..n): SYS := subs(BC_V, BC_Phi, {%});

The SYS computed above is a system of linear first order DAEs (Differential-Algebraic Equations)
in the  unknowns   These are DAEs because the

unknowns   appear algebraically only; there are no derivatives of .

 > indets(SYS, function);

The system of DAEs may be solved by applying the initial conditions IC_U
calculated earlier, and the initial conditions IC_Theta and IC_Phi which we calculate now:

 > subs(x=h*j, ic_theta): IC_Theta := seq(Theta[j](0) = rhs(%), j=1..n);

 > subs(x=h*j, ic_phi): IC_Phi := seq(Phi[j](0) = rhs(%), j=1..n);

The overall initial conditions are:

 > IC := { IC_U, IC_Theta, IC_Phi };

 >

Numerics

Here are the parameters/coefficients/functions that define the problem:

 > params := h = L/(n+1), L = 10, v__0 = (x -> 0), theta__0 = (x -> 0), phi__0 = (x -> 0), v__left = (t -> 0), theta__left = (t -> 0), phi__left = (t -> 0), v__right = (t -> sin(omega*t)), theta__right = (t -> 1), phi__right = (t -> 1), phi__11 = 1 - eta + eta*rho__s*c__ps/(rho__f*c__pf), phi__10 = phi__4/phi__1, phi__9 = phi__3/phi__1, phi__8 = phi__2/phi__1, phi__7 = phi__6/(1 + lambda__1), phi__6 = phi__5/(phi__1*Ra), phi__5 = 1/(1 - eta)^2.5, phi__4 = 1 - eta + eta*rho__s*beta__Cs/(rho__f*beta__Cf), phi__3 = 1 - eta + eta*rho__s*beta__Ts/(rho__f*beta__Tf), phi__2 = 1 + 3*(sigma - 1)*eta/(sigma + 2 - (sigma - 1)*eta), phi__1 = 1 - eta + eta*rho__s/rho__f, lambda__f = k__nf/k__f, k__nf = (k__s + 2*k__f + 2*eta*(k__s - k__f))/(k__s + 2*k__f - eta*(k__s - k__f)), lambda__f = k__nf/k__f, a__0 = Pe*phi__11/lambda__f, a__1 = Sc/(1 - eta), M = 0.3, Gr = 5, Gm = 0.3, Pe = 0.2, Sc = 0.2, Ra = 1, lambda__1 = 0.5, omega = 1, rho__f = 5610, rho__s = 2300, c__pf = 0.880, c__ps = 41.086, beta__Tf = 0.0000125, beta__Ts = 0.0000157, beta__Cf = 0.0000125, beta__Cs = 0.0000157, eta = 0.02, k__f = 1.046, k__s = 1.160, sigma = 0.2, NULL:

Maple's dsolve() function is capable of solving DAEs.  Here is our solution:

 > eval(subs(params, SYS union IC)): dsol := dsolve(%, numeric, output=operator, maxfun=0):

Let's plot a few samples:

 > odeplot(dsol, [t,U[3](t)], t=0..2*Pi);

 > odeplot(dsol, [t,V[3](t)], t=0..2*Pi);

 > odeplot(dsol, [t,Theta[3](t)], t=0..2*Pi);

 > odeplot(dsol, [t,Phi[3](t)], t=0..2*Pi);

 >
 >

Plotting in 3D

We wish to plot the solution  over the domain

.  For the plotting grid, we take

the ,   as we have done already,

and , where  is specified as desired,

and .  Thus, the solution is represented as a grid
of points  that is, in 3D.
The solutions   and  are plotted in the same way.

A detail that needs to be taken care of is that the solution

of the DAEs accounts for the interior nodes

only.  The values of the solution corresponding to

the boundary nodes, that is, , etc., are not

included in the solution of the DAE and need to be evaluated

separately through the prescribed boundary conditions.  These are:

 > V[0] := subs(params, v__left); V[n+1] := subs(params, v__right); Theta[0] := subs(params, theta__left); Theta[n+1] := subs(params, theta__right); Phi[0] := subs(params, phi__left); Phi[n+1] := subs(params, phi__right);

Here is a proc for plotting the solution  of the DAEs where  is one of .
over the domain , and where  is the number of

time steps.

 > my_plot3d := proc(Q, T, m)   local i, j, A, k:= T/m, L := subs(params, :-L);   A := Array(0..n+1, 0..m, (i,j)->eval(Q[i](j*k), dsol), datatype=float[8]):   plots:-display(GRID(0..L, 0..T, A), labels=[x,t,Q(x,t)], _rest); end proc:

The solution

 > my_plot3d(V, 2*Pi, 20);

The solution :

 > my_plot3d(Theta, 2*Pi, 20);

The solution :

 > my_plot3d(Phi, 2*Pi, 20);

 >