STACK DocumentationDocumentation home | Category index | Parent | Site map
This page provides examples of how to represent and manipulate ordinary differential equations (ODEs) in Maxima when writing STACK questions.
In a Maxima session (rather than STACK question variables) we can represent an ODE as
ODE: x^2*'diff(y,x) + 3*y*x = sin(x)/x;
Notice the use of the
' character in front of the
diff function to prevent evaluation. Applied to a function call, such as
diff, the single quote prevents evaluation of the function call, although the arguments of the function are still evaluated (if evaluation is not otherwise prevented). The result is the noun form of the function call.
Maxima has two notaions to display ODEs.
'diff(y,x) is displayed in STACK as . Note this differs from Maxima's normal notation of .
'diff(y,x) is displayed in STACK and Maxima as .
- Extra brackets are sometime produces around the differential.
- You must have
simp:trueotherwise the display routines will not work.
Manipulating ODEs in Maxima
This can be solved with Maxima's
ode2 command and initial conditions specified. Below is an example of Maxima's output.
(%i1) ODE: x^2*'diff(y,x) + 3*y*x = sin(x)/x; 2 dy sin(x) (%o1) x -- + 3 x y = ------ dx x (%i2) ode2(ODE,y,x); %c - cos(x) (%o2) y = ----------- 3 x (%i3) ic1(%o2,x=%pi,y=0); cos(x) + 1 (%o3) y = - ---------- 3 x
Further examples and documentation are given in the Maxima manual
Note that by default STACK changes the value of Maxima's
logabs variable. This changes the way is integrated. If you want the default behaviour of Maxima you will need to restore
logabs:false in the question variables.
Randomly generating ODE problems
When randomly generating questions we could easily generate an ODE which cannot be solved in closed form, so that in particular using ode2 may be problematic. It is much better when setting any kind of STACK question to start with the method and work backwards to generate the question. This ensures the question remains valid over a whole range of parameters. It also provides many intermediate steps which are useful for a worked solution.
% characters from solve and ode2
Maxima functions such as solve and ode2 add arbitrary constants, such as constants of integration. In Maxima these are indicated adding constants which begin with percentage characters. For example,
eq1:x^2*'diff(y,x) + 3*y*x = sin(x)/x; sol:ode2(eq1,y,x);
y = (%c-cos(x))/x^3
%c in this example.
STACK forbids the use of the
% character for security reasons: you can refer to previous sessions for example.
Therefore, we need a function to strip out the variables starting with
stack_strip_percent(ex,var) replaces all variable names starting with
% with those in
There are two ways to use this.
varis a list then take the variables in the list in order.
varis a variable name, then Maxima returns unevaluated list entries,
stack_strip_percent(y = (%c-cos(x))/x^3,k)
y = (k-cos(x))/x^3
This is displayed in STACK using subscripts, which is natural.
The unevaluated list method also does not need to know how many % signs appear in the expression.
The other usage is to provide explicit names for each variable, but the list must be longer than the number of constants in
stack_strip_percent(y = (%c-cos(x))/x^3,[c1,c2])
y = (c1-cos(x))/x^3
The following example question variables can be used within STACK.
ode = x^2*'diff(y,x) + 3*y*x = sin(x)/x sol = stack_strip_percent(ode2(ode,y,x),[k]) ta = rhs(ev(sol,nouns))
Note, you may need to use the Option "assume positive" to get ODE to evaluate the integrals formally and hence "solve correctly".
ODEs provide a good example of the principle that we should articulate the properties we are looking for in ordinary differential equations. These properties are
- The answer satisfies the differential equation.
- The answer satisfies any initial/boundary conditions.
- The answer is general.
- The answer is in the required form.
Hence, for ODE questions we need a potential response tree which establishes a number of separate properties. On the basis of the properties satisfied, we then need to generate outcomes.
Satisfying the differential equation
When marking this kind of question, it is probably best to take the student's answer and substitute this into the ODE. The student's answer should satisfy the equation. Just "looking like the model answer" isn't as robust. How else does the teacher avoid the problem of knowing which letter the student used to represent an arbitrary constant?
E.g. in Maxima code
ode:x^2*'diff(y,x) + 3*y*x = sin(x)/x; ans: (c - cos(x))/x^3; /* The student's (correct) answer */ sa1:subst(y=ans,ode); sa2:ev(sa1,nouns); sa3:fullratsimp(expand(sa2));
sa1, sa2 and sa2 can be used as part of the feedback when a student doesn't get the right answer. This is shown below with the values of these three variables displayed in the feedback. ODE feedback example
Satisfying any initial/boundary conditions
If the student's answer is
ans then we can check initial/boundary conditions at a point
x=x0 simply by
Notice in the second example the need to calculate the derivative of the student's answer before it is evaluated at the point
These values can be compare with answer tests in the usual way.
Further tests are needed to ensure the student's solution is non-trivial, satisfies any initial conditions, or is suitably general.
To find which constants are present in an expression use Maxima's
In particular, to find if
c appears in an expression
ans we can use the predicate
However, it is unusual to want to specify the name of a constant. A student may choose another name. The example below may be helpful here.
Second order linear differential equations with constant coefficients
One important class of ODEs are the second order linear differential equations with constant coefficients.
Generating these kinds of problems is relatively simple: we just need to create a quadratic with the correct sort of roots.
Let us assume we have two real roots. We might expect an answer. We might have an unusual, but correct, answer such as . Hence, we can't just "look at the answer".
A sample question of this type is provided by STACK, in which we have the following question variables.
sa1 : subst(y(t)=ans1,ode); sa2 : ev(sa1,nouns); sa3 : fullratsimp(expand(sa2)); l : delete(t,listofvars(ans1)); lv : length(l); b1 : ev(ans1,t=0,fullratsimp); b2 : ev(ans1,t=1,fullratsimp); m : float(if b2#0 then fullratsimp(b1/b2) else 0);
sa3are used to ensure the answer satisfies the ODE and if not to provide feedback.
- To ensure we have two constants we count the number of variables using
listofvars, not including
t. We are looking for two constants.
- To ensure the solution is suitably general, we confirm and calculate . If this simplifies to a number then the constants have canceled out and we don't have a general solution consisting of two linearly independent parts.
These are the properties a correct answer should have. If the teacher has a preference for the form, then a separate test is required to enforce it. For example, you might like the top operation to be a, i.e. sum. This can be confirmed by
aop : is(equal(op(ans1),"+"));
true with another answer test. Note that the arguments to answer tests cannot contain double quotes, so a question variable is needed here.
Next, let us assume we have complex root, e.g. in the equation
We potentially have quite a variety of solutions.
The advantage is that the same code correctly assesses all these forms of the answer.
First order exact differential equations
An important class of differential equations are the so-called first order exact differential equations of the form
Assume thatgives an implicit function, which satisfies this equation. Then
Differentiating once further (and assuming sufficient regularity of) we have
Note that this condition onand is necessary and sufficient for the ODE to be exact. In search of such a function we may define
Notice here thatand are arbitrary functions of integration. To evaluate these we differentiate again, for example taking the first of these we find
where this last equality arises from the differential equation. Rearranging this and solving we have
Similarly we may solve forIf then we have an exact differential equation, and given the integral of our ODE.
As an example consider
In both cases we obtain the same answer for.
The following Maxima code implements this method, and provides further examples of how to manipulate ODEs.
/* Solving exact differential equations in Maxima */ (kill(all),load("format"))$ ODE:x*'diff(y,x)+y+4$ /* Ensure we have an expression, not an equation */ if op(ODE)="=" then ODE:lhs(ODE)-rhs(ODE); /* This should write the ODE in the form p*'diff(y,x)+q which we can then sort out to get the coefficients*/ ODE:format(ODE,%poly('diff(y,x)))$ ODEc:coeffs(ODE,'diff(y,x)); q:ODEc; p:ODEc; /* Check our condition for an exact ODE */ if fullratsimp(diff(p,x)-diff(q,y))=0 then print("EXACT") else print("NOT EXACT")$ /* Next we need to solve [diff(h,x)=q,diff(h,y)=p] to find the integral of our ODE */ h1:integrate(q,x); h2:integrate(p,y); H1:h1+integrate(p-diff(h1,y),y); H2:h2+integrate(q-diff(h2,x),x); /* Note, H1 and H2 should be the same! */ /* Hence the solution is, in terms of y=...+c*/ solve(H1=c,y);
Further examples are /* Non-exact equations / ODE:y=x'diff(y,x);
/* Exact equations */ ODE:2*y*x*'diff(y,x)+y^2-2*x=0$ ODE:sin(x)*cosh(y)-'diff(y,x)*cos(x)*sinh(y)=0$ ODE:(3*x^2*cos(3*y)+2*y)*'diff(y,x)=-2*x*sin(3*y)$ ODE:x*'diff(y,x)+y+4$
Documentation home | Category index | Parent | Site map