Integrate & simplify in Maple - maple

I'm new to Maple.
I want to integrate and simplify the following expression:
int(((1 - r*cos(x-t)))/(1+r^(2)-2* r*cos(x-t)), x=0..2*pi,AllSolutions=true)
assuming t>=0, assuming r>=0, additionally t <=2*pi, additionally r<=1;
If I just run the int line itself, I'm getting very confusing expression.
Int and assume lines are giving me error,
if smb could help me out with that,
thanks!

Do you know that lowercase pi means nothing special to Maple?
If you simply meant to use the well-known constant Pi instead, then your problem is just with the syntax of assuming.
I split the lines below, just to format them more nicely here. You could enter each a a single longer line.
restart:
int( ((1 - r*cos(x-t)))/(1+r^(2)-2*r*cos(x-t)),
x=0..2*Pi ) assuming t>=0, r>=0, t <=2*Pi, r<=1;
2 Pi
It turns out that the assumptions on t are not necessary for that result to obtain.
restart:
int( ((1 - r*cos(x-t)))/(1+r^(2)-2* r*cos(x-t)),
x=0..2*Pi ) assuming r>=0, r<=1;
2 Pi
It's understandable that you got the assuming syntax not right. You mistakenly cobbled something together that looks more like syntax for the assume command. Their purposes are related, but the syntax and usage differ.
The AllSolutions option allows Maple to compute piecewise results based upon conditions of unknowns in the range of integration (but not just in the integrand). So, it's conceivable that someone might want to do,
restart:
int( ((1 - r*cos(x-t)))/(1+r^(2)-2* r*cos(x-t)),
x=0..2*pi, AllSolutions=true ) assuming t>=0, r>=0, t <=2*pi, r<=1;
which will produces a piecewise result with a condition on the unknown parameter pi. But that will print confusingly since both Pi and pi prettyprint the same. Better to use some other unassigned name than pi. As I said at the top, you probably meant to use the special constant Pi instead.

Related

define a 4th rank tensor in Maple

I am a newbie in Maple. Could you please help me to convert the following short code from Matlab to Maple:
I=0.0;
for i1=1:3
I(i1,i1,i1,i1)=1.0;
end
I've tried to write it like:
unprotect(I);
I:=0.0;
for i1 from 1 to 3 do
for i2 from 1 to 3 do
for i3 from 1 to 3 do
for i4 from 1 to 3 do
if i1=i2 and i2=i3 and i3=i4 then I[i1,i2,i3,i4]:=1.0;
else I[i1,i2,i3,i4]:=0.0;
end if;
od;
od;
od;
od;
But it gives the following error:
Error, illegal use of an object as a name
Error, illegal use of an object as a name
Can anybody tell me what's wrong?
Thank you,
It'd be easier if you didn't insist on using the name I, which in Maple has the special meaning of the sqrt of -1.
restart;
interface(imaginaryunit=j):
local I:=Array((1..3)$4,datatype=float[8]);
for i1 from 1 to 3 do
I[i1,i1,i1,i1]:=1.0;
end do:
The above produces I as a 4-dimensional Array, where each dimension has a width of three elements. And the three "long diagonal" elements are all initialized to 1.0. And the Array can contain hardware double precision floats. And all the other elements are 0.0 by default.
Is that what you were trying to do?
If you don't insist on calling assigning it to the special name I then things are easier. Eg,
restart;
II:=Array((1..3)$4,datatype=float[8]):
for i1 from 1 to 3 do
II[i1,i1,i1,i1]:=1.0;
end do:
You cannot properly override/disable the special meaning of I merely by unprotecting it. (And even if you could, unprotecting and redefining I is an unworkable idea since a significant portion of Maple commands would then no longer compute properly in the given session.)
Recent versions of Maple allows you to create a so-called top-level "local" instance of that name, which can be used separately from the usual global name I. If you insist on that route, and if your Maple version is recent enough to support that, then you'd probably also want to change the
interface setting for the imaginary unit (sqrt of -1) so that things don't get too confusing. That's why I showed it in the first example above.
But I really think that you'd find things easiest if you just used another name, like II or what have you.
You don't need to put the datatype=float[8] restriction on the Array. But if your subsequent code mimics some (originally) Matlab code then maybe floats are all that will be assigned into the Array. And some operations on Arrays can be much faster, with it. See how it goes.

subscript indices must be either positive integers less than 2^31 or logicals

I'm quite new to Matlab/Octave programming, but have this one issue that I can't seem to solve.
I wrote the following which is actually a quite straight forward calculation on an option price using the Black Scholes Formula (just to give you some background). However, I do constantly get the following error msg:
"subscript indices must be either positive integers less than 2^31 or logicals"
One would think that this explaines it quite nicely and I know there've been questions on it before. The thing that causes troubles, however, is that I am not using any kind of subscript index in my code at all.
Here is my code:
function v=BS_LBO_strike_call(s,T,sigma,r,q,l,alpha)
d1=(log(alpha*l./s) + (r-q-0.5*sigma^2)*T)/(sigma*sqrt(T));
d2=(log(alpha*l./s) - (r-q+0.5*sigma^2)*T)/(sigma*sqrt(T));
d3=(log(alpha*l./s) + (r-q+0.5*sigma^2)*T)/(sigma*sqrt(T));
d4=(log(alpha*l./s) + (r-q-0.5*sigma^2)*T)/(sigma*sqrt(T));
v = exp(-r*T)*s(0.5*sigma^2./(r-q)*(l./s).^(2*(r-q)./sigma^2).*normcdf(d1) - 0.5*sigma^2./(r-q)*alpha.^(-2*(r-q)./sigma^2).*exp((r-q).*T).*normcdf(d2) + alpha.exp*((r-q).*T).*normcdf(d3) - (l./s).*normcdf(d4));
So, I can't seem to figure out what doesn't work out for Octave.
I would highly appreciate if you could maybe shed some light on this. I'm convinced there must be something minor that I overlook
The source of your issues lies in your last line. You have the following:
v = exp(-r * T) * s(0.5 * sigma^2 ....
I think that you have omitted an * between the s and the opening parenthesis because as it is now, everything after that parenthesis is being treated as a subscript into s. This is the root cause of the error you are getting because what follows is likely not an integer or logical.
There is one other point in that line that is likely going to lead to some errors as well. You have the following as part of that statement.
alpha.exp*((r-q).*T) ...
Unless alpha is a struct (I'm sure it's not because you haven't used it that way previously), you will likely want something else besides the . between alpha and exp. Maybe another *?

Unbounded (infinite) repetitions in transitions for covergroup bins

How can I define coverage bin for transition that might have many repetitions in it? What I'm trying to do is something like this:
bins st = (2=> 3[* 1:$] => 2);
Of course, this doesn't work. Simulators give error messages when compiling this line.
Edit: Simulators complain about $ symbol, they don't recognize it as "unbounded maximum". However when writing sequences, it is legal to use consecutive repetition operator as [* 1:$]. I hope next version of SystemVerilog makes it legal for covergroups too.
As a crude workaround, I substituted $ with a large number so it works fine for my case.
bins st = (2=> 3[* 1:1000] => 2);
SystemVerilog transition bins were not designed to handle anything but simple transitions. Anything more complex should be modeled using a cover directive, or some combination of sequence.triggered() method and covergroup.

Logical indexing and double precision numbers

I am trying to solve a non-linear system of equations using the Newton-Raphson iterative method, and in order to explore the parameter space of my variables, it is useful to store the previous solutions and use them as my first initial guess so that I stay in the basin of attraction.
I currently save my solutions in a structure array that I store in a .mat file, in about this way:
load('solutions.mat','sol');
str = struct('a',Param1,'b',Param2,'solution',SolutionVector);
sol=[sol;str];
save('solutions.mat','sol');
Now, I do another run, in which I need the above solution for different parameters NewParam1 and NewParam2. If Param1 = NewParam1-deltaParam1, and Param2 = NewParam2 - deltaParam2, then
load('solutions.mat','sol');
index = [sol.a]== NewParam1 - deltaParam1 & [sol.b]== NewParam2 - deltaParam2;
% logical index to find solution from first block
SolutionVector = sol(index).solution;
I sometimes get an error message saying that no such solution exists. The problem lies in the double precisions of my parameters, since 2-1 ~= 1 can happen in Matlab, but I can't seem to find an alternative way to achieve the same result. I have tried changing the numerical parameters to strings in the saving process, but then I ran into problems with logical indexing with strings.
Ideally, I would like to avoid multiplying my parameters by a power of 10 to make them integers as this will make the code quite messy to understand due to the number of parameters. Other than that, any help will be greatly appreciated. Thanks!
You should never use == when comparing double precision numbers in MATLAB. The reason is, as you state in the the question, that some numbers can't be represented precisely using binary numbers the same way 1/3 can't be written precisely using decimal numbers.
What you should do is something like this:
index = abs([sol.a] - (NewParam1 - deltaParam1)) < 1e-10 & ...
abs([sol.b] - (NewParam2 - deltaParam2)) < 1e-10;
I actually recommend not using eps, as it's so small that it might actually fail in some situations. You can however use a smaller number than 1e-10 if you need a very high level of accuracy (but how often do we work with numbers less than 1e-10)?

Turn off "smart behavior" in Matlab

There is one thing I do not like on Matlab: It tries sometimes to be too smart. For instance, if I have a negative square root like
a = -1; sqrt(a)
Matlab does not throw an error but switches silently to complex numbers. The same happens for negative logarithms. This can lead to hard to find errors in a more complicated algorithm.
A similar problem is that Matlab "solves" silently non quadratic linear systems like in the following example:
A=eye(3,2); b=ones(3,1); x = A \ b
Obviously x does not satisfy A*x==b (It solves a least square problem instead).
Is there any possibility to turn that "features" off, or at least let Matlab print a warning message in this cases? That would really helps a lot in many situations.
I don't think there is anything like "being smart" in your examples. The square root of a negative number is complex. Similarly, the left-division operator is defined in Matlab as calculating the pseudoinverse for non-square inputs.
If you have an application that should not return complex numbers (beware of floating point errors!), then you can use isreal to test for that. If you do not want the left division operator to calculate the pseudoinverse, test for whether A is square.
Alternatively, if for some reason you are really unable to do input validation, you can overload both sqrt and \ to only work on positive numbers, and to not calculate the pseudoinverse.
You need to understand all of the implications of what you're writing and make sure that you use the right functions if you're going to guarantee good code. For example:
For the first case, use realsqrt instead
For the second case, use inv(A) * b instead
Or alternatively, include the appropriate checks before/after you call the built-in functions. If you need to do this every time, then you can always write your own functions.