Take the difference of 2 vectors and sum and square them - matrix

I have 2 vectors of size 1x90.
I have to do the operator
diff=sum((V_new-V).^2);
But every time i do it i get the error:
Subscript indices must either be real positive integers or logical.
How can i fix this problem and prevent it from occurring again?

make sure you did not step over the sum function:
type
>> dbstop if error
run the code, it should stop in debugger when error occurred.
check what sum is:
>> which sum
should return that sum is a build-in function, but if you accidentally created a variable with that name, it will tell you that sum is a variable.
DO NOT USE BUILT-IN FUNCTIONS' NAMES AS VARIABLES

Related

Julia BoundsError Means

ERROR: BoundsError: attempt to access Tuple{Int64} at index [2]
Stacktrace:
[1] indexed_iterate
# .\tuple.jl:89 [inlined]
When I run my Julia code, it shows this BoundsError. What does this mean and how can I solve it?
My code is to find the size of a Vector{Float64}.
(m,n)=size(c)
where c is a Vector{Float64}.
A Tuple{Int64} is a tuple containing a single element. So the error message is saying that you tried to access element 2 of a one-element tuple.
In Julia, vectors are truly one-dimensional, so size returns only one value (for eg. size(1:10) is just (10,)). When you try to assign that to (m, n), you're trying to extract two values from it, hence the error.
Since you know it's a Vector already, you know that its number of columns is going to be just 1. So you can instead just do n = length(c) to obtain the number of elements c contains.

Maple: How to define new elements of a matrix?

In Maple, I have a matrix N and its elements N[i,j], If I modify the elements of this matrix as follows for example
>for j from 1 to 4 do
>print(F[i,j]=(diff(N[i,j],x)));
>od;od;
where the matrix elements are functions of x.
I've wanted to define new matrix elements
>BA[i,j]:=(diff(N[i,j],x)));
but I can't do this with Maple, through the above command. Can someone help me ?
Better than using a loop is simply BA:= diff~(N,x). The ~ can be appended to any operator to mean "apply the operator to each member of the container and return a new container containing the modified members."
Also, be careful about using print. Its only purpose is to print stuff on the screen from the middle (not the end) of a computation. It can't be used to change any stored values. Good programs use print very sparingly, if at all. The end result of a computation is displayed automatically, without needing a print command.

Random number generation requires too many iterations

I am running simulations in Anylogic and I'm trying to calibrate the following distribution:
Jump = normal(coef1, coef2, -1, 1);
However, I keep getting the following message as soon as I start the calibration (experimentation):
Random number generation requires too many iterations (> 10000)
I tried to replace -1 and 1 by other values and keep getting the same thing.
I also tried to change the bounds of coef1 and coef2 and put things like [0,1], but I still get the same error.
I don't get it.
Any ideas?
The four parameter normal method is not deprecated and is not a "calibration where coef1 and coef2 are the coefficicents to be solved for". Where did you get that understanding from? Or are you saying that you're using your AnyLogic Experiment (possibly a multi-run or optimisation experiment) to 'calibrate' that distribution, in which case you need to explain what you mean by 'calibrate' here---what is your desired outcome?
If you look in the API reference (AnyLogic classes and functions --> API Reference --> com.xj.anylogic.engine --> Utilities), you'll see that it's a method to use a truncated normal distribution.
public double normal(double min,
double max,
double shift,
double stretch)
The first 2 parameters are the min and max (where it will sample repeatedly and ignore values outside the [min,max] range); the second two are effectively the mean and standard deviation. So you will get the error you mentioned if min or max means it will sample too many times to get a value in range.
API reference details below:
Generates a sample of truncated Normal distribution. Distribution
normal(1, 0) is stretched by stretch coefficient, then shifted to the
right by shift, after that it is truncated to fit in [min, max]
interval. Truncation is performed by discarding every sample outside
this interval and taking subsequent try. For more details see
normal(double, double)
Parameters:
min - the minimum value that this function will return. The distribution is truncated to return values above this. If the sample
(stretched and shifted) is below this value it will be discarded and
another sample will be drawn. Use -infinity for "No limit".
max - the maximum value that this function will return. The distribution is truncated to return values below this. If the sample
(stretched and shifted) is bigger than this value it will be discarded
and another sample will be drawn. Use +infinity for "No limit".
shift - the shift parameter that indicates how much the (stretched) distribution will shifted to the right = mean value
stretch - the stretch parameter that indicates how much the distribution will be stretched = standard deviation Returns:
the generated sample
According to AnyLogic's documentation, there is no version of normal which takes 4 arguments. Also note that if you specify a mean and standard deviation, the order is unusual (to probabilists/statisticians) by putting the standard deviation before the mean.

How to form a for loop which performs descending order in Scilab

I was trying to perform a descend loop in Scilab
like:
for i=7: 1
disp(i);
end
But apparently it didn't work and there are no errors too.
Is this possible? And if yes, how to do it?
Syntax
As the for-loop documentation states, the for loop's syntax is:
for variable=expression do instruction, ,instruction,end
If expression is a matrix or a row vector, variable takes as values the values of each column of the matrix.
Your example
Your for-loop uses 7:1 as expression, what will result in a valid but empty vector:
-->7:1
ans =
[]
The for-loop then takes each value in the vector and then finishes, but since your vector is empty it finishes directly. So it is valid syntax.
Working example
The documentation gives the following example of a working decreasing loop:
for j=4:-1:1
disp(j);
end // decreasing loop

VBScript logical operator range

I'm using VBScript in ASP on IIS and I cannot seem to get the annoying error to go anyway so this makes debugging so much harder not to mention it does not tell me the exact error except the same error message so I can only assume what's wrong in my code.
My question is: Using the logical operators AND,XOR,NOT,OR in VBScript , is there a limit on the range to what the operands can be? I have implemented a bit shift right function and used the mod operator and I didn't notice until now that my function was causing the error.
My right shift function
function rshift(number,n)
'Shifts a number's bits n bits to the right
for i=1 to n
if number mod 2 = 0 then
number = number / 2
else
number = (number - 1) / 2
end if
next
rshift = number
end function
'Fails
rshift(1125899906842624,2)
I think for values larger than 2^32 ( or 31) - 1 that the operators do not work. Tried googling for the range of the operands but couldn't find anything helpful. I saw someone posted a topic about logical operators not working on large values but I can't seem to find that anymore.
Can someone verify this ?
Edit: Found a topic which gives more information on using the mod operator on signed 32 bit integers http://blogs.msdn.com/b/ericlippert/archive/2004/12/01/integer-arithmetic-in-vbscript-part-one.aspx
In VBScript there are several subtypes for integers including integer and long. VBScript will attempt to determine what type of value you are using and use the appropriate subtype.
Integer can store a value between -32,768 to 32,767 and long can store a value between -2,147,483,648 to 2,147,483,647. That value that you are using is greater than this and will result in an overflow.
You can use the VarType function to see what type your number is interpreted as. You may use Double to represent larger values.
This answer looks interesting. Maybe you can use it as part of your function.

Resources