How to evaluate logarithms in Maxima? - logarithm

Following examples from here, and here I tried this:
log2(x) := log(x) / log(2);
log2(8), float;
But this doesn't give 3, instead I get log(8)/log(2).

You have to simplify radicals:
(%i1) log2(x) := log(x) / log(2);
log(x)
(%o1) log2(x) := ------
log(2)
(%i2) radcan(log2(8));
(%o2)
3

or use float :
float(log(8)/log(2));
gives:
2.999999999999999

Another way to calculate logarithms in base 10:
You can also write in the command line: load(log10);(enter)
Then for the calculation of the decimal logarithm: log10(100);(Enter) the result is 2, and so on. To exemplify I did some calculations with log10. These results are shown in the picture below

Related

hyperpolic function integration in maple

Im trying to evaluate this function in maple
but I keep getting this answer, why isn't maple integrating properly. I tried numerically integrating it and it works but I need the analytical solution too.
restart;
sig := x->(exp((x-t)/a)-exp((-x-t)/a))
/(exp((x-t)/a)+exp((-x-t)/a)):
new := convert(simplify(convert(expand(sig(x)),trigh)),tanh);
new := tanh(x/a)
simplify(expand(convert(sig(x) - new, exp)));
0
Now, you originally wrote int(f*sig(x)/x,x).
You didn't indicate that f was a function of x, and as a mere constant it's not really important and could simply be pulled out in front of the integral as a constant factor. If f is some function of x then you really need to state what it is!
Let's consider int(sig(x)/x,x=c..d). Using the simplification new, that is just,
Q := Int( new/x, x=c..d );
Q := Int(tanh(x/a)/x, x = c .. d)
QQ := IntegrationTools:-Change(Q, y=x/a, y);
QQ := Int(tanh(y)/y, y = c/a .. d/a)
You said that you wanted an "analytical solution" by which I take it you mean an explicit formula for the symbolic integration result. But what do you want if the integral does not (mathematically) have a closed form exact, symbolic result?
Would you be content with an (exact, symbolic) series approximation?
H := (a,ord,c,d)
-> int(convert(series(eval(new/x,:-a=a),x,ord),
polynom),x=c..d):
# order 5
H(a, 5, c, d);
3 3 / 5 5\
d - c -c + d 2 \-c + d /
----- - -------- + ------------
a 3 5
9 a 75 a
For a specific example, taking a=2 and an (exact) series approximation of order 25, then the integral from x=0 to x=1 gets evaluated as an exact rational.
evalf(H(2, 25, 0, 1));
0.4868885956
Here's the numeric integration for those same values,
evalf(Int( eval(new/x,a=2), x=0..1 ));
0.4868885956
Specialized numeric quadrature could be as good as a series approximation for a variety of applications, but of course that would depend on what you intend on doing with the result.
This raises the question: what do you hope to do with some supposed "analytical result" that you cannot do with a black-box function that generates the floating-point numeric approximation? Why do you "need" an "analytic result"?
BTW, another way to simplify it (in case the construction above of new does not succeed in your Maple version):
new := convert(simplify(expand( numer(sig(x))/exp(-t/a) ))
/simplify(expand( denom(sig(x))/exp(-t/a) )),
compose,trigh,tanh);
/x\
new := tanh|-|
\a/

How to obtain and work with steady-state part of DE solution?

I'm trying to obtain resonance curves of the system. System can be described as
F,m,k:=2,1,4:
lambda:= beta/(2*m):
omega:=sqrt(k/m):
de:=diff(x(t),t$2)+2*lambda*diff(x(t),t)+omega^2*x(t)=F*cos(gamma1*t):
cond:=x(0)=0, D(x)(0)=0:
sol := dsolve({cond, de});
Solving gives sum of terms, some of which "die out" with time (since these terms have exp(-...*t)) and some of which form steady-state solution (solution for t -> ∞). This solution will be in form xstst=f(gamma1)*sin(...). In order to obtain resonance curves, I need to plot f(gamma1) (for chosen constant betas, say, 2,1,0.5,0.25,etc.).
I've solved this "by hand" and found f := F/(sqrt((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)). Plotting this for any chosen beta gives the result needed, for example, for beta:=0.5 the plot is
I wonder if I can obtain these curves using maple functions only (without solving anything "by hand" at all).
[edited]
It does not make sense to expect Maple to represent the result in terms of either sin(theta) or cos(theta), using some formulas for those terms that appear nowhere in the problem specification and are entirely introduced by you.
The following is obtained using a radical (square root) in each of cond1 and cond2.
restart;
de := diff(x(t),t,t)+2*lambda*diff(x(t),t)+omega^2*x(t)=F*cos(gamma1*t):
cond := x(0)=0, D(x)(0)=0:
sol := dsolve({cond, de}):
E,T := selectremove(hastype,rhs(sol),specfunc(anything,exp)):
lprint(T);
F*(2*sin(gamma1*t)*gamma1*lambda-cos(gamma1*t)*gamma1^2
+cos(gamma1*t)*omega^2)/(gamma1^4+4*gamma1^2*lambda^2-
2*gamma1^2*omega^2+omega^4)
cond1 := cos(theta) = (-gamma1^2+omega^2)
/((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)^(1/2):
cond2 := sin(theta) = (-2*lambda*gamma1)
/((-gamma1^2+omega^2)^2+4*lambda^2*gamma1^2)^(1/2):
frontend(algsubs, [numer(rhs(cond1))=lhs(cond1)*denom(rhs(cond1)),
numer(T)],
[{`+`,`*`,`=`},{}]):
frontend(algsubs, [numer(rhs(cond2))=lhs(cond2)*denom(rhs(cond2)),
%],
[{`+`,`*`,`=`},{}]):
ans := collect(combine(%, trig),cos)/denom(T):
lprint(ans);
F*cos(gamma1*t+theta)/(gamma1^4+4*gamma1^2*lambda^2-
2*gamma1^2*omega^2+omega^4)^(1/2)
subsans := eval(eval(ans,[lambda=beta/(2*m),omega=sqrt(k/m)]),
[F=2,m=1,k=4]):
lprint(subsans);
2*cos(gamma1*t+theta)
/(beta^2*gamma1^2+gamma1^4-8*gamma1^2+16)^(1/2)
I'm not sure I fully understand your question, but when I run your code I get some terms with exponentials, some with sines and some with cosines. You can grab the coefficient of the sine terms with
coeff( collect( rhs( sol ) , sin( gamma1 * t ) ) , sin( gamma1 * t ) , 1 )

Solve equation with exponential term

I have the equation 1 = ((π r2)n) / n! ∙ e(-π r2)
I want to solve it using MATLAB. Is the following the correct code for doing this? The answer isn't clear to me.
n= 500;
A= 1000000;
d= n / A;
f= factorial( n );
solve (' 1 = ( d * pi * r^2 )^n / f . exp(- d * pi * r^2) ' , 'r')
The answer I get is:
Warning: The solutions are parametrized by the symbols:
k = Z_ intersect Dom::Interval([-(PI/2 -
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n))], (PI/2 +
Im(log(`fexp(-PI*d*r^2)`)/n)/2)/(PI*Re(1/n)))
> In solve at 190
ans =
(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
-(fexp(-PI*d*r^2)^(1/n))^(1/2)/(pi^(1/2)*d^(1/2)*exp((pi*k*(2*i))/n)^(1/2))
You have several issues with your code.
1. First, you're evaluating some parts in floating-point. This isn't always bad as long as you know the solution will be exact. However, factorial(500) overflows to Inf. In fact, for factorial, anything bigger than 170 will overflow and any input bigger than 21 is potentially inexact because the result will be larger than flintmax. This calculation should be preformed symbolically via sym/factorial:
n = sym(500);
f = factorial(n);
which returns an integer approximately equal to 1.22e1134 for f.
2. You're using a period ('.') to specify multiplication. In MuPAD, upon which most of the symbolic math functions are based, a period is shorthand for concatenation.
Additionally, as is stated in the R2015a documentation (and possibly earlier):
String inputs will be removed in a future release. Use syms to declare the variables instead, and pass them as a comma-separated list or vector.
If you had not used a string, I don't think that it would have been possible for your command to get misinterpreted and return such a confusing result. Here is how you could use solve with symbolic variables:
syms r;
n = sym(500);
A = sym(1000000);
d = n/A;
s = solve(1==(d*sym(pi)*r^2)^n/factorial(n)*exp(-d*sym(pi)*r^2),r)
which, after several minutes, returns a 1,000-by-1 vector of solutions, all of which are complex. As #BenVoigt suggests, you can try the 'Real' option for solve. However, in R2015a at least, the four solutions returned in terms of lambertw don't appear to actually be real.
A couple things to note:
MATLAB is not using the values of A, d, and f from your workspace.
f . exp is not doing at all what you wanted, which was multiplication. It's instead becoming an unknown function fexp
Passing additional options of 'Real', true to solve gets rid of most of these extraneous conditions.
You probably should avoid calling the version of solve which accepts a string, and use the Symbolic Toolbox instead (syms 'r')

Matlab is rounding my sigmoid function

I have implemented a sigmoid function as follows in Matlab.
function [y] = sig(x)
y = 1.0 / (1.0 + exp(-x));
end
When I give it a large input such as 100, the function rounds off my result and gives me a 1.
How can I get its accurate value? Is it possible or am I limited to a low range for the value of x.
If you want the difference between 1 and your sigmoid function, you could define a function with the simplified mathematical expression:
1 - 1/(1+exp(-x)) = (1+exp(-x))/(1+exp(-x)) - 1/(1+exp(-x)) = exp(-x) / (1+exp(-x))
function [y] = one_minus_sig(x)
y = exp(-x) / (1+exp(-x));
end
And then:
one_minus_sig(100) = 3.7200759760208356e-44
1.0000000000000... is accurate to about 44 digits so I'm not sure what the problem would be?
Edit: In an earlier version, I said 300 digits - for some reason I had used x=900 in that computation. Here are a few digits:
0.99999999999999999999999999999999999999999996279924023979164037040304196136881662641107846012870706553139188204855222012652822147634307
Computed using Maple.

Matlab gamma function: I get Inf for large values

I am writing my own code for the pdf of the multivariate t-distribution in Matlab.
There is a piece of code that includes the gamma function.
gamma((nu+D)/2) / gamma(nu/2)
The problem is that nu=1000, and so I get Inf from the gamma function.
It seems I will have to use some mathematical property of the gamma
function to rewrite it in a different way.
Thanks for any suggestions
You can use the function gammaln(x), which is the equivalent of log(gamma(x)) but avoids the overflow issue. The function you wrote is equivalent to:
exp(gammaln((nu+D)/2) - gammaln(nu/2))
The number gamma(1000/2) is larger than the maximum number MATLAB support. Thus it shows 'inf'. To see the maximum number in MATLAB, check realmax. For your case, if D is not very large, you will have to rewrite your formula. Let us assume that in your case 'D' is an even number. Then the formula you have will be: nu/2 * (nu/2 -1) * ....* (nu/2 - D/2 + 1).
sum1 = 1
for i = 1:D/2
sum1 = sum1*(nu/2 - i+1);
end
Then sum1 will be the result you want.