wxMaxima: Finding the closest larger/smaller number - maxima

How can one find the closest larger and closest smaller number from a maxima list? Which maxima functions should I explore?

Here's a solution based on finding the sublist of elements which are less than or greater than x, and returning the greatest or least such element. If there is no such element, false is returned.
(%i4) next_smaller (L, x) :=
(sort(L),
sublist (%%, lambda ([y], y < x)),
if %% # [] then last(%%)) $
(%i5) next_larger (L, x) :=
(sort(L),
sublist (%%, lambda ([y], y > x)),
if %% # [] then first(%%)) $
(%i6) list: [0, 0.014, 0.021, 0.028, 0.042, 0.056, 0.07, 0.084, 0.11, 0.17, 0.28, 0.42, 0.56] $
(%i7) next_smaller (list, 0.04);
(%o7) 0.028
(%i8) next_larger (list, 0.04);
(%o8) 0.042
(%i9) next_larger (list, 0.56);
(%o9) false
(%i10) next_smaller (list, 0.56);
(%o10) 0.42
(%i11) next_smaller (list, 0);
(%o11) false
(%i12) next_larger (list, 0);
(%o12) 0.014
(%i13) next_larger (list, -1);
(%o13) 0
(%i14) next_smaller (list, -1);
(%o14) false
(%i15) next_smaller (list, 1);
(%o15) 0.56
(%i16) next_larger (list, 1);
(%o16) false
These functions probably could be made more efficient, but you might notice the difference only if you're working with long lists. Maybe see if this works before trying to optimize.

Related

Solving Equations with (wx)Maxima: Control stack exhausted

Solving Equations with (wx)Maxima: Control stack exhausted
I'm trying to solve equations with (wx)Maxima: formulate the equation, then let it insert the variables and solve the equation for the missing variable. But I'm having a hard time. Somehow it's having problems in the last line:
Control stack exhausted (no more space for function call frames).
This is probably due to heavily nested or infinitely recursive function
calls, or a tail call that SBCL cannot or has not optimized away.
That's my code:
kill(all);
load(physical_constants);
load(unit);
setunits([kg,m,s,N]);
showtime: false;
α: 30*%pi/180;
/*α: 30*°;*/
masse: 1000*kg;
g: 9.80665*m/(s*s);
b: 0.3*m;
B: 0.5*m;
L: 0.1*m;
F_g: masse*g;
F_H: masse * g;
kill(S, x);
S: solve(0=F_H-2*x*sin(α), x);
S: assoc(x, S);
kill(H, x);
H: solve(0=-F_g+2*x, x);
H: assoc(x, H);
kill(Ly, x);
Ly: solve(tan(α)=x/(B/2), x);
Ly: assoc(x, Ly);
kill(FN, x);
FN: solve(0=H*B/2-x*(L+Ly)+S*sin(α)*B/2+S*cos(α)*Ly, x);
FN: assoc(x, FN);
If I calculate it "directly", it works though:
kill(all);
load(physical_constants);
load(unit);
setunits([kg,m,s,N]);
showtime: false;
kill(FN, x);
FN: solve([α=30*%pi/180, H=196133/40*N,
B=0.5*m, L=0.1*m,
Ly=sqrt(3)/12*m, S=196133/20*N,
0=H*B/2-x*(L+Ly)+S*sin(α)*B/2+S*cos(α)*Ly],
[x, α, H, B, L, Ly, S]);
FN: assoc(x, FN[1]);
FN: float(FN);
(FN) 1934473685/128529*N
Unfortunately the unit package has not been updated in some time. I'll suggest to use instead the package ezunits, in which dimensional quantities are represented with a back quote. To solve equations, try dimensionally which goes through some gyrations to help other functions with dimensional quantities, e.g. dimensionally (solve (...)). (Note that dimensionally isn't documented, I'm sorry for the shortcoming.)
I've modified your program a little to remove some unneeded stuff and also to use rational numbers instead of floats; Maxima is generally more comfortable with rationals and integers than with floats. Here is the program:
linel: 65 $
load(ezunits) $
α: 30*%pi/180;
masse: 1000`kg;
g: rationalize(9.80665)`m/(s*s);
b: 3/10`m;
B: 5/10`m;
L: 1/10`m;
F_g: masse*g;
F_H: masse * g;
S: dimensionally (solve(0=F_H-2*x*sin(α), x));
S: assoc(x, S);
Ly: dimensionally (solve(tan(α)=x/(B/2), x));
Ly: assoc(x, Ly);
FN: dimensionally (solve(0=H*B/2-x*(L+Ly)+S*sin(α)*B/2+S*cos(α)*Ly, x));
FN: assoc(x, FN);
subst (x = S, F_H-2*x*sin(α));
subst (x = Ly, tan(α)=x/(B/2));
subst (x = FN, H*B/2-x*(L+Ly)+S*sin(α)*B/2+S*cos(α)*Ly);
ratsimp (expand (%));
and here is the output I get. Note that I substituted the solutions back into the equations to verify them. It looks like it worked as expected.
(%i2) linel:65
(%i3) load(ezunits)
(%i4) α:(30*%pi)/180
%pi
(%o4) ---
6
(%i5) masse:1000 ` kg
(%o5) 1000 ` kg
(%i6) g:rationalize(9.80665) ` m/(s*s)
5520653160719109 m
(%o6) ---------------- ` --
562949953421312 2
s
(%i7) b:3/10 ` m
3
(%o7) -- ` m
10
(%i8) B:5/10 ` m
1
(%o8) - ` m
2
(%i9) L:1/10 ` m
1
(%o9) -- ` m
10
(%i10) F_g:masse*g
690081645089888625 kg m
(%o10) ------------------ ` ----
70368744177664 2
s
(%i11) F_H:masse*g
690081645089888625 kg m
(%o11) ------------------ ` ----
70368744177664 2
s
(%i12) S:dimensionally(solve(0 = F_H-2*x*sin(α),x))
690081645089888625 kg m
(%o12) [x = ------------------ ` ----]
70368744177664 2
s
(%i13) S:assoc(x,S)
690081645089888625 kg m
(%o13) ------------------ ` ----
70368744177664 2
s
(%i14) Ly:dimensionally(solve(tan(α) = x/(B/2),x))
1
(%o14) [x = --------- ` m]
4 sqrt(3)
(%i15) Ly:assoc(x,Ly)
1
(%o15) --------- ` m
4 sqrt(3)
(%i16) FN:dimensionally(solve(0 = (H*B)/2-x*(L+Ly)
+(S*sin(α)*B)/2
+S*cos(α)*Ly,x))
1 1
(%o16) [x = (----------------------------------------- ` --)
140737488355328 sqrt(3) + 351843720888320 2
s
2
(351843720888320 sqrt(3) H ` s
3/2
+ 1150136075149814375 3 ` kg m)]
(%i17) FN:assoc(x,FN)
1 1
(%o17) (----------------------------------------- ` --)
140737488355328 sqrt(3) + 351843720888320 2
s
2
(351843720888320 sqrt(3) H ` s
3/2
+ 1150136075149814375 3 ` kg m)
(%i18) subst(x = S,F_H-2*x*sin(α))
kg m
(%o18) 0 ` ----
2
s
(%i19) subst(x = Ly,tan(α) = x/(B/2))
1 1
(%o19) ------- = -------
sqrt(3) sqrt(3)
(%i20) subst(x = FN,(H*B)/2-x*(L+Ly)+(S*sin(α)*B)/2+S*cos(α)*Ly)
1 1
(- ---------) - --
4 sqrt(3) 10 1
(%o20) ((----------------------------------------- ` --)
140737488355328 sqrt(3) + 351843720888320 2
s
2
(351843720888320 sqrt(3) H ` s
3/2 H
+ 1150136075149814375 3 ` kg m) + -) ` m
4
2
690081645089888625 kg m
+ ------------------ ` -----
281474976710656 2
s
(%i21) ratsimp(expand(%))
2
kg m
(%o21) 0 ` -----
2
s
EDIT. About converting kg*m/s^2 to N, you can apply the double back quote operator. For example:
(%i25) F_g `` N
690081645089888625
(%o25) ------------------ ` N
70368744177664
By the way, to convert back to floats, you can apply float:
(%i26) float(%)
(%o26) 9806.649999999998 ` N
Converting FN to N is a little more involved, since it's a more complex expression, especially because of H which doesn't have units attached to it yet. Some inspection seems to show the units of H must be kg*m/s^2. I'll apply declare_units to say that's what are the units of H. Then I'll convert FN to N.
(%i27) declare_units(H,(kg*m)/s^2)
kg m
(%o27) ----
2
s
(%i28) FN `` N
351843720888320 sqrt(3) qty(H)
(%o28) (-----------------------------------------
140737488355328 sqrt(3) + 351843720888320
3/2
1150136075149814375 3
+ -----------------------------------------) ` N
140737488355328 sqrt(3) + 351843720888320
(%i29) float(%)
(%o29) (1.023174629940149 qty(H) + 10033.91548470256) ` N
The notation qty(H) represents the unspecified quantity of H. One could also just subst(H = 100 ` kg*m/s^2, FN) (or any quantity, not just 100) and go from there.

Taylor series expansion in maxima

How to expand taylor series/polynomials about Q=0 , and then extract coefficients as a list
example :
taylor ( (sin(q)), q, 0, 9); //taylor expansion for first 9 terms gives the next line
(%o1)/T/ q\-q^3/6+q^5/120\-q^7/5040+q^9/362880+...
then using coeff ((%o1), q ^n); gives me the coefficient at n only, what i want is a list for all the coefficients of that expression
Try coeff plus makelist, e.g. something like: makelist(coeff(%o1, q, n), n, 0, 9);
Edit:
I see now that I misread your question and there is already an answer. Nevertheless I will keep it because it is related to your question.
Use powerseries instead of taylor:
(%i1) expr:powerseries(sin(x),x,0);
inf
==== i2 2 i2 + 1
\ (- 1) x
(%o1) > -----------------
/ (2 i2 + 1)!
====
i2 = 0
You can access the coefficient by the args or part function
(%i2) op(expr);
(%o2) sum
(%i3) args(expr);
i2 2 i2 + 1
(- 1) x
(%o3) [-----------------, i2, 0, inf]
(2 i2 + 1)!
(%i4) part(expr,1);
i2 2 i2 + 1
(- 1) x
(%o4) -----------------
(2 i2 + 1)!
(%i5) args(expr)[1];
i2 2 i2 + 1
(- 1) x
(%o5) -----------------
(2 i2 + 1)!
If you want to change the index variable:
(%i6) niceindices(expr),niceindicespref=[n];
inf
==== n 2 n + 1
\ (- 1) x
(%o6) > ---------------
/ (2 n + 1)!
====
n = 0

Performing Laplace transform on non-continuous function in Maxima

I am calculating the following function's Laplace transform, but I get some strange output with limit and realpart etc on (%t5) and (%t6). What I expect is something like a_n = 0 and b_n = -1/(n*%pi).
Why is this happening? Am I defining the function at (%i1) incorrectly? Or is this a limitation of Maxima?
(%i1) f(t) := mod(t, 1);
(%o1) f(t) := mod(t, 1)
(%i2) plot2d(f(t), [t, -2, 2]);
(%o2) [/tmp/maxout1266174.gnuplot_pipes]
(%i3) load(fourie);
(%o3) /usr/share/maxima/5.43.0/share/calculus/fourie.mac
(%i4) fourier(f(t), t, 1);
1
(%t4) a = -
0 2
/
[
(%t5) a = limit I cos(%pi n t) realpart(floor(t)) dt
n t -> - 1 ]
/
/
[
- limit I cos(%pi n t) realpart(floor(t)) dt
t -> 1 ]
/
/
[
(%t6) b = (- limit I sin(%pi n t) realpart(floor(t)) dt)
n t -> 1 ]
/
/
[ 2 sin(%pi n) 2 cos(%pi n)
+ limit I sin(%pi n t) realpart(floor(t)) dt + ------------ - ------------
t -> - 1 ] 2 2 %pi n
/ %pi n
(%o6) [%t4, %t5, %t6]
(%i7)

Symbolic evaluation of expressions containing relational operators, possibly using Maxima

I'm looking for either another tool that will do symbolic evaluation of relational expressions, or perhaps a Maxima package that extends Maxima to enable such a feature.
As is, Maxima does not do this.
As far as I know maxima can work with relational operators in the manual:
http://maxima.sourceforge.net/docs/manual/maxima_7.html#SEC39
(%i1) [x, y, z] : [123, 456, 789];
(%o1) [123, 456, 789]
(%i2) is (x < y);
(%o2) true
(%i3) maybe (y > z);
(%o3) false
(%i4) if x >= z then 1 else 0;
(%o4) 0
(%i5) block ([S], S : 0, for i:1 while i <= 100 do S : S + i,
return (S));
(%o5) 5050
or even solve inequalities:
(%i4) load(fourier_elim)$
(%i5) fourier_elim([(x+1)*(x+2)>0],[x]);
(%o5) [- 1 < x] or [x < - 2]

Maxima - differentiating a piecewise function

Suppose you have a function defined by intervals, such as
f(x):=block(if x<0 then x^2 else x^3);
When we differentiate it with
diff(f(x),x);
we get
d/dx (if x<0 then x^2 else x^3)
whereas I'd like to get
(if x<0 then 2*x else 3*x^2)
Is there a way to obtain such result?
This may help in a simple case:
(%i1) f(x):= charfun(x<0)*x^2 + charfun(x>=0)*x^3$
(%i2) gradef(charfun(y), 0)$
(%i3) diff(f(x),x);
2
(%o3) 2 x charfun(x < 0) + 3 x charfun(x >= 0)
charfun, gradef
You can try also Pw.mac package from Richard Hennessy.
Here's a different approach using a simplification rule for "if" expressions. The unsolved part here is to detect discontinuities and generate delta functions for those locations. If you want to ignore those, you can define FOO to return 0. Note that I didn't attempt to implement the function discontinuities; that part is unsolved here. I can give it a try if there is interest.
(%i1) display2d : false $
(%i2) matchdeclare ([aa, bb, cc], all, xx, symbolp) $
(%i3) 'diff (if aa then bb else cc, xx) $
(%i4) tellsimpafter (''%, apply ("if", [aa, diff (bb, xx), true, diff (cc, xx)]) + FOO (aa, bb, cc, xx)) $
(%i5) FOO (a, b, c, x) := 'lsum ((ev (c, x = d) - ev (b, x = d)) * delta (d, x), d, discontinuities (a, x)) $
(%i6) diff (if x > 0 then x^2 else x^3, x);
(%o6) (if x > 0 then 2*x else 3*x^2)+'lsum((d^3-d^2)*delta(d,x),d,
discontinuities(x > 0,x))
Building on slitinov's answer I wrote this quite naive implementation for functions with more than two "pieces":
gradef(charfun(dummy),0)$
/* piecewise function definition */
itv: [[x<0],[x>=0,x<1], [x>=1]]; /* intervals */
fi: [ 1, x^2+1, 2*x ]; /* local functions */
/* creation of global function f and its derivative df */
f:0;
for i: 1 thru 3 do f:f+charfun(apply("and",itv[i]))*fi[i];
df:diff(f,x);
/* display of local functions and derivatives */
for i: 1 thru 3 do (
apply(assume,itv[i]),
newline(),
print(itv[i]),
print("f = ",ev(f)),
print("df = ",ev(df)),
apply(forget,itv[i])
);
plot2d([f,df],[x,-2,3],[y,-1,5],[style,[lines,4,3],[lines,2,2]]);

Resources