Formatting Expression to Standard Forms in Maxima - formatting

I'm a newbie to Maxima and I'm finding the tool very useful. One of the more common use-cases for me is the validation of expressions given in academic texts. Take this as an example:
This is a transfer function written in a pretty standard format. Notice how the squared term in the denominator appears by itself. Fair enough.
So, I go about building this expression in pieces and my expectation is to compare results at the end. It helps then if the expressions are displayed using a common format. So, I'm trying to get my result to have the same normalized format as the expression in the book.
In this case, I do the following:
(%i10) F(s):=(1+s*tau_2)/(1+s*(tau_1+tau_2));
(%o10) F(s):=(1+s*tau_2)/(1+s*(tau_1+tau_2))
(%i13) H(s):=(K_0*K_d*F(s))/(s+((K_0*K_d*F(s)/N)));
(%o13) H(s):=(K_0*K_d*F(s))/(s+(K_0*K_d*F(s))/N)
(%i81) H_1(s):=ratsimp(H(s),s);
(%o81) H_1(s):=ratsimp(H(s),s)
(%i82) H_1(s);
(%o82) (K_0*K_d*N*s*tau_2+K_0*K_d*N)/(s^2*(N*tau_2+N*tau_1)+s*(K_0*K_d*tau_2+N)+K_0*K_d)
Which is very close. But, I've tried a few different ways of dividing across the expression by N*(tau_1 + tau_2) to leave s^2 without a coefficient and none worked. Is there an easy way to solve this?
UPDATE:
Some options I've tried:
--> divthru(e,d):=map(lambda([u], multthru(u,d)),e); /* map applies function f() to each subpart of expr*/
(%o164) divthru(e,d):=map(lambda([u],multthru(u,d)),e)
--> divthru(H_1(s), 1/(N*tau_2+N*tau_1));
(%o165) (K_0*K_d*N*s*tau_2+K_0*K_d*N)/(s^2*(N*tau_2+N*tau_1)+s*(K_0*K_d*tau_2+N)+K_0*K_d)
(%i35) matchdeclare([A,B,C,D],all);
(%o35) done
(%i26) defmatch(isDefPoly, A/(s^2*B+s*C+D), x);
(%o26) isDefPoly
(%i27) isDefPoly(H_1(s),s);
(%o27) [A=K_0*K_d*N*s*tau_2+K_0*K_d*N,D=K_0*K_d,B=N*tau_2+N*tau_1,C=K_0*K_d*tau_2+N,x=s]
(%i36) tellsimpafter(A/(s^2*B+s*C+D), (A/B)/(s^2+(s*C)/B+D/B));
(%o36) [\*rule1,simptimes]

ratsimp tries to put expressions in a standardized format which makes further operations more straightforward, and it is difficult to convince it to rearrange terms in some other way. For example, for an expression like (a + b/c)/(d + e/f), ratsimp will try to rearrange it to something which is one sum of terms over another set of terms.
I think there are some add-on functions which rearrange terms in different ways, but I'm not sure there is anything to achieve the form you have shown.
One thing that will help in a small way. My advice is to write subscripted variables as x[k] instead of x_k. That can help the TeX formatting code recognize the variable name, e.g. tau, and format it as a Greek letter. Also, you can enter Unicode characters (from any language, multibyte characters included) as variable names.

I worked on it some more and maybe there are some ideas here which can help. Here is a function to divide above and below. I'll apply it to Hs as shown in the problem statement.
(%i1) divabovebelow (e, r) := (multthru (1/e, num(r)))/(multthru (1/e, denom(r))) $
(%i2) Hs: (K_0*K_d*N*s*tau_2+K_0*K_d*N)/(s^2*(N*tau_2+N*tau_1)+s*(K_0*K_d*tau_2+N)+K_0*K_d);
K_0 K_d N s tau_2 + K_0 K_d N
(%o2) --------------------------------------------------------
2
s (N tau_2 + N tau_1) + s (K_0 K_d tau_2 + N) + K_0 K_d
(%i3) divabovebelow (N*tau_2 + N*tau_1, Hs);
K_0 K_d N s tau_2 K_0 K_d N
----------------- + -----------------
N tau_2 + N tau_1 N tau_2 + N tau_1
(%o3) ----------------------------------------------
s (K_0 K_d tau_2 + N) K_0 K_d 2
--------------------- + ----------------- + s
N tau_2 + N tau_1 N tau_2 + N tau_1
Here are a couple more functions that could be helpful. polynomial_in rearranges terms to kind of isolate one variable from the others. nicer_rational tries to make a rational function look nicer, via polynomial_in.
(%i4) Hs1: ratsimp (Hs);
K_0 K_d N s tau_2 + K_0 K_d N
(%o4) -----------------------------------------------------
2 2
(N s + K_0 K_d s) tau_2 + N s tau_1 + N s + K_0 K_d
(%i5) polynomial_in (var, e) := apply ("+", makelist (var^k * ratcoef (e, var, k), k, 0, hipow (e, var))) $
(%i6) nicer_rational (var, e) := (polynomial_in (var, num(e)))/(polynomial_in (var, denom(e))) $
(%i7) nicer_rational (s, Hs1);
K_0 K_d N s tau_2 + K_0 K_d N
(%o7) --------------------------------------------------------
2
s (N tau_2 + N tau_1) + s (K_0 K_d tau_2 + N) + K_0 K_d
These yield the same eventual result as ratsimp above, but maybe they make more sense since they are devised for the kind of expressions you're working with.
I was thinking about some additional functions to get closer to the textbook form, but it's a little fragile, since Maxima wants to reorganize expressions into its standard form, so for better or worse, one can get either a nice display, or an expression for working with. Hope this helps anyway.

Related

Explicitly write the sum of bilinear or trilinear terms in GAMS?

I am working on writing a constraint in GAMS which involves a polynomial with the sum of 1st, 2nd, and 3rd order terms. Since there are 7 variables (from x('1') to x('7')), there are in total 119 terms (7 first-order terms, 28 second-order terms, and 84 third-order terms).
The first-order terms are easy to write out:
c('1')*x('1') + c('2')*x('2') ...
However, the second-order terms are more difficult to write:
c('8')*x('1')*x('1')+c('9')*x('1')*x('2') ... +c('14')*x('1')*x('7')+c('15')*x('2')*x('2') ... + c('35')*x('7')*x('7')
The thrid order terms are just too many and too long:
c('36')*x('1')*x('1')*x('1') + c('37')*x('1')*x('1')*x('2') + ... + c('119')*x('7')*x('7')*x('7')
It is really challenging to write the equations manually. I was wondering if there is a way to use the 'sum' and conditional ($) operation to write out these 119 terms explicitly.
For instance,
Set
i /1 * 7/
num /1 * 119/
;
Variable
x(i)
func
;
Parameters
c(num)
;
Equation
Eq1;
Eq1.. func =e= c('1')*x('1') + c('2')*x('2') + {many terms in between} + c('8')*x('1')*x('1')+c('9')*x('1')*x('2') + {many terms in between} + c('36')*x('1')*x('1')*x('1') + c('37')*x('1')*x('1')*x('2') + {many terms in between} + c('119')*x('7')*x('7')*x('7')
Thanks!

Simplification of complexity of a function with two arguments in terms of Big O

Let's say we have the following complexity:
T(n, k) = n^2 + n + k^2 + 15*k + 123
Where we do not know anything about relations between n and k.
I could say that in terms of Big O complexity will be the following:
T(n) = O(n^2 + n + k^2 + 15*k)
Can I simplify it further and drop only 15 constant or I can drop n and 15*k?
UPDATE: according to this link Big O is not valid notation for two or more variables
Yes, you can.
O(n^2+n+k^2+15*k)=O(n^2+n)+O(k^2+15*k)=O(n^2)+O(k^2)=O(n^2+k^2)
This does assume that k and n are both positive, looking at the behavior as they get large.

Time complexity of Simpson's rule for simple intergral calculus

I am looking for a reference and a proof for the time complexity of Simpson's rule for integral calculus.
I am not sure if the class complexity of that rule belongs to O(N).
Could you point me out to the right direction ?
Thanks
First of all, the Simpson's Rule requires three inputs:
The function f(x), assume it takes O(1) time.
The bounds of integration (a, b)
The number of subdivisions, n. Then the width of the "bar" d = (b - a) / n Note n must be an even positive integer.
Simpson's Rule states that
∫ab f(x) ≈ (d/3)([f(x0) + f(xn)] + [2f(x1) + 4f(x2)] + [2f(x3) + 4f(x4)] + ... [2f(xn-2) + 4f(xn-1)])
∫ab f(x) ≈ (d/3)([f(x0) + f(xn)] + ∑k=2(n-1)/2 f(xk)
where xk is equal to a + kd. Note x0 = a, xn = a + nd = b.
From the summation term ∑k=2(n-1)/2, we can easily state that there are [(n-1)/2 - 2 + 1] terms, and there are also two more terms for f(x0), f(xn). The number of terms used for the Simpson rule for a given n is linear to n.
Assuming multiplication is constant and the function complexity is constant, we note the summation formula to determine that the time complexity of the Simpson rule is O(n), it runs in linear time.

How do i find the number of times x=x+1 is executed in terms of N?

im also having trouble finding omega(), and theta() as appropriate
x=0;
for k=1 to n
for j=1 to n-k
X=X+1;
The inner loop is n-1 + n-2 + n-3 ... + 1 + 0. Use this tutorial on calculating the sum of an arithmetic series to find the solution. The outer loop is obviously just "n."
This will be the big-theta. The big-oh will be the same as big-theta when you pull off everything but the first term and remove the multiplier, e.g. Theta(2*log(n) + 5) becomes O(log(n)). Omega is the same as big-Oh in this case, because the best case and worst case are identical; or you can cheat and say that big-Omega is constant time, because the big-Omega of EVERY function is constant time.
First, look at your boundaries. k=1 and k=n.
For k=1, the inside loop is executed (n-1) times.
For k=n the inside loop is execured (0) times.
So, 0 + 1 + ... + (n-1) is an arithmetic sum => (n-1)(n)/2 times.
Now, test it on a few small values :)
the answer is like this :
n-1 + n -2 + n -3 + ... = n*n - (1+2+3+ ... + n) = n^2 - n(n-1)/2

Asymptotic analysis question: sum[log(i)*i^3, {i, n}] is big-theta (log(n)*n^4)

I've got a homework question that's been puzzling me. It asks that you prove that the function Sum[log(i)*i^3, {i, n}) (ie. the sum of log(i)*i^3 from i=1 to n) is big-theta (log(n)*n^4).
I know that Sum[i^3, {i, n}] is ( (n(n+1))/2 )^2 and that Sum[log(i), {i, n}) is log(n!), but I'm not sure if 1) I can treat these two separately since they're part of the same product inside the sum, and 2) how to start getting this into a form that will help me with the proof.
Any help would be really appreciated. Thanks!
The series looks like this - log 1 + log 2 * 2^3 + log 3 * 3^3....(upto n terms)
the sum of which does not converge. So if we integrate it
Integral to (1 to infinity) [ logn * n^3] (integration by parts)
you will get 1/4*logn * n^4 - 1/16* (n^4)
It is clear that the dominating term there is logn*n^4, therefore it belongs to Big Theta(log n * n^4)
The other way you could look at it is -
The series looks like log 1 + log2 * 8 + log 3 * 27......+ log n * n^3.
You could think of log n as the term with the highest value, since all logarithmic functions grow at the same rate asymptotically,
You could treat the above series as log n (1 + 2^3 + 3^3...) which is
log n [n^2 ( n + 1)^2]/4
Assuming f(n) = log n * n^4
g(n) = log n [n^2 ( n + 1)^2]/4
You could show that lim (n tends to inf) for f(n)/g(n) will be a constant [applying L'Hopital's rule]
That's another way to prove that the function g(n) belongs to Big Theta (f(n)).
Hope that helps.
Hint for one part of your solution: how large is the sum of the last two summands of your left sum?
Hint for the second part: If you divide your left side (the sum) by the right side, how many summands to you get? How large is the largest one?
Hint for the first part again: Find a simple lower estimate for the sum from n/2 to n in your first expression.
Try BigO limit definition and use calculus.
For calculus you might like to use some Computer Algebra System.
In following answer, I've shown, how to do this with Maxima Opensource CAS :
Asymptotic Complexity of Logarithms and Powers