I'm struggling to find a way to switch from a symbolic declaration of the differential operator to its implementation
I give you an example.
F: (10-'diff(x(t),t)^2 -2*x(t)*'diff(x(t),t) -5*x(t)^2)*%e^(-t);
E: ratsimp(diff(F, x(t)) - diff(diff(F, 'diff(x(t),t)), t));
sol: ode2(E, x(t), t);
sol: ev(sol, [%k1 = C1, %k2=C2]);
trans_cond: diff(F, 'diff(x(t), t));
trans_cond: ev(trans_cond, sol);
trans_cond: at(trans_cond, [t=1]);
The corresponding output maintains the symbolic notation whereas I would like to evaluate the diff() obtained after the last substitution.
Giving the result:
% 4*C1-C2^(-2)
Another solution. The nouns option for ev causes the evaluation of symbolic derivatives, and also any other noun expressions such as symbolic integrals, symbolic summations, etc.
(%i2) 'diff(4*x^2, x);
d 2
(%o2) -- (4 x )
dx
(%i3) ev (%o2, nouns);
(%o3) 8 x
A shorter form of ev(..., nouns) is recognized by the interactive console. You can input ..., nouns instead.
(%i5) %o2, nouns;
(%o5) 8 x
Here is ev(..., nouns) applied to symbolic integral:
(%i6) 'integrate (x^2, x);
/
[ 2
(%o6) I x dx
]
/
(%i7) %, nouns;
3
x
(%o7) --
3
and here, to a symbolic summation:
(%i8) 'sum (f(k), k, 1, 3);
3
====
\
(%o8) > f(k)
/
====
k = 1
(%i9) %, nouns;
(%o9) f(3) + f(2) + f(1)
Found the answer, ev() carries the option diff which solves all the symbolic differentiation in the expression.
Related
I cannot find anything about this, sorry.
If I have expressions with the symbolic function f(x) and now I want to replace in these expression f(x) by its explicit form how to do it?
For example:
I have
f(x):= x^2+sin(x)
and in the differentiation
diff (%e**sqrt(f(x)*a), x,2);
I want to replace now f(x) by the expression above?
Thanks
Karl
(%i1) i: integrate(f(x)*f(4*x), x, 0, 1) $
(%i2) f(x):= x^2+sin(x) $
(%i3) ev(i, f);
1
/
[ 2 2
(%o3) I (sin(x) + x ) (sin(4 x) + 16 x ) dx
]
/
0
-- Function: ev (<expr>, <arg_1>, ..., <arg_n>)
Evaluates the expression <expr> in the environment specified by the
arguments <arg_1>, ..., <arg_n>. The arguments are switches
(Boolean flags), assignments, equations, and functions. 'ev'
returns the result (another expression) of the evaluation.
I'm struggling with getting maxima to simplify expressions in the way that I want them.
rhs(solve(a*x-3=b*y,x)[1]);
returns
(b*y+3)/3
However, I'm wanting to display the result to students and so I want the output to be
(b*y/3) + 1
Is there some simplification that I can do to a rational expression in this form to get the output I want?
You can undo the effect of ratsimp in this case by dividing each term in the numerator by the denominator. Here is a simple-minded implementation:
(%i1) unratsimp (e) :=
block ([foo, bar],
[foo, bar]: [num(e), denom(e)],
map (lambda ([foo1], foo1/bar), expand (foo))) $
(%i2) 1 + a/b + c*d/3 - %pi*x/y;
%pi x c d a
(%o2) (- -----) + --- + - + 1
y 3 b
(%i3) ratsimp(%);
(b c d + 3 b + 3 a) y - 3 %pi b x
(%o3) ---------------------------------
3 b y
(%i4) unratsimp(%);
%pi x c d a
(%o4) (- -----) + --- + - + 1
y 3 b
(%i5) unratsimp((b*y + 3)/3);
b y
(%o5) --- + 1
3
I don't know how general that is; it's just the first thing I tried, but maybe it's enough for your purpose. I don't know a built-in function for this.
The results %o4 and %o5 are maybe not exactly as a human would write them. Convincing Maxima to display the terms in + and * expressions in a different way is not straightforward -- Maxima has strong ideas about how to order terms in an expression which are assumed throughout the code. But I think that others have asked questions, which might have answers, about displaying expressions -- you might search Stackoverflow if you are are interested.
The command you can use here is multthru:
(%i1) e1:(b*y+3)/3;
(%o1) (b*y+3)/3
(%i2) multthru(e1);
(%o2) (b*y)/3+1
In this case also the following will work
(%i3) distrib(e1);
(%o3) (b*y)/3+1
(%i4) expand(e1);
(%o4) (b*y)/3+1
I need to make tons of simple computations and present each step in my report with predefined manner:
(for ex i got B=2, C=3):
A=B+12-6/C^2; A=2+12-6/3^2=13.333;
I can get 1st block and answer like this:
B:2$ C:3$
A:'(B+12-6/C^2)$
print("A=",A,"; ","A= ??? =",ev(A, numer) );
and get:
6
A= (- --) + B + 12 ; A= ??? = 13.33333333333333
2
C
What i need instead of '???' to get desired output?
Maxima distinguishes two parts of figuring out a result: evaluation and simplification. Evaluation = substituting one thing (the value) for another thing (a variable or a function). Simplification = applying mathematical identities to get a "simpler", equivalent result.
In your problem, it appears you want to postpone simplification. You can say simp: false to do that. Here's one possible approach. I'll disable simplification, substitute values into the expression, print the substituted expression, and then re-enable simplification to get the final result.
(%i2) expr: A=B+12-6/C^2;
6
(%o2) A = (- --) + B + 12
2
C
(%i3) simp: false $
(%i4) subst ([B = 2, C = 3], expr);
- 2
(%o4) A = 12 + 2 + (- 6) 3
(%i5) simp: true $
(%i6) %o4;
40
(%o6) A = --
3
Note that many operations in Maxima happen by simplification (e.g. adding numbers together), so in general, Maxima will act noticeably different when simp is false. But in this case that's what you want.
EDIT: OP points out that the result after substitution is displayed in a somewhat different from. The reason for this has to do with some obscure implementation details of Maxima. Be that as it may, it's possible to work around that behavior by using the Lisp substitution function SUBST (referenced in Maxima as ?subst) instead of Maxima subst. SUBST is a little different than Maxima subst; the syntax is ?subst(new_thing, old_thing, some_expression). After substituting via SUBST, it's necessary to resimplify explicitly; one way to do that is to say expand(..., 0, 0) (which doesn't expand anything, the only effect is to resimplify).
(%i2) expr: A=B+12-6/C^2;
6
(%o2) A = (- --) + B + 12
2
C
(%i3) simp: false $
(%i4) ?subst (3, C, ?subst (2, B, expr));
6
(%o4) A = (- --) + 2 + 12
2
3
(%i5) simp: true $
(%i6) expand (%o4, 0, 0);
40
(%o6) A = --
3
Since SUBST is has a different effect on the internal representation, it is possible you could create an invalid expression, for some choices of new_thing, old_thing, and some_expression. I won't try to sort that out here.
I want to understand how dependencies in Maxima for differentiation work
for iterated cases.
I tried here:
(%i1) depends([f],[x,y]);
(%o1) [f(x,y)]
(%i2) depends([g],[x,y]);
(%o2) [g(x,y)]
(%i3) depends([x,y],[ε]);
(%o3) [x(ε),y(ε)]
(%i4) diff(g,ε);
(%o4) (g[y])*(y[ε])+(g[x])*(x[ε])
(%i5) h(x,y):=f(x,y)+g(x,y);
(%o5) h(x,y):=f(x,y)+g(x,y)
(%i6) diff(h(x,y),ε);
(%o6) g(x,y)[ε]+f(x,y)[ε]
(%i7) diff(h,ε);
(%o7) 0
In (%o4) I get the total derivative with respect to \epsilon. Whereas in (%o6) the derivatives of x and y with respect to \epsilon are not shown. Why?
And can I make Maxima to show these derivatives in the result?
Dependencies declared by depends are only recognized for symbolic, undefined functions. The dependency is associated with the function name (a symbol).
A function with an actual definition, as defined by := or define, is not recognized. The body of the function could contain any combination of other functions, so the only way to know on which other functions the function depends is to evaluate the function body. That is what is happening when you write diff(h(x, y), ε).
In mathematics you can write f(f(f(x))) as f^3(x). I've trawled through the Maxima manuals but can't find a way to express this for an arbitrary power.
Maxima doesn't have a built-in operator for that. But I think you can define a workable solution yourself. Maybe this works for you:
(%i1) infix ("o^") $
(%i2) matchdeclare (ff, symbolp, xx, all) $
(%i3) tellsimp ((ff o^ 0)(xx), xx) $
(%i4) tellsimp ((ff o^ 1)(xx), ff(xx)) $
(%i5) matchdeclare (nn, lambda ([e], integerp(e) and e > 1)) $
(%i6) tellsimp ((ff o^ nn)(xx), (ff o^ (nn - 1))(ff (xx))) $
I've defined simplification rules for "o^" since I want it to be applied only if it appears in a function call expression (i.e., something like (f o^ 3)(x), not just f o^ 3 by itself), and the second argument is a literal, nonnegative integer.
You have to write a space between "o^" and the following token, since otherwise the parser gets confused.
Here are some examples:
(%i7) (h o^ 0)(u);
(%o7) u
(%i8) (h o^ 1)(u);
(%o8) h(u)
(%i9) (h o^ 2)(u);
(%o9) h(h(u))
(%i10) (h o^ 3)(u);
(%o10) h(h(h(u)))
(%i11) (h o^ n)(u);
(%o11) h o^ n(u)
(%i12) %, n=2;
(%o12) h(h(u))
(%i13) h o^ 3;
(%o13) h o^ 3
(%i14) %(z);
(%o14) h(h(h(z)))
(%i15) (h o^ -1)(u);
(%o15) h o^ (- 1)(u)
Note that in cases which don't match the rules, the expression is not simplified. This leaves the door open to defining additional rules to handle those cases, without changing the stuff that already works.
The display of unsimplified expressions (e.g. %o11) is ambiguous; this is a bug.