Why am I getting "Requested 4000000000x1 (29.8GB) array exceeds maximum array size preference...." with my code that I am trying to run? I'm using the error function but I don't see why a normal machine can't run this.

2 次查看(过去 30 天)
Here is my current code:
a = 1
b = 3*10^-7
c = 5*10^-8
f = 4*10^9
sigma = 0.2
t0 = 0
tmax = 2*b
F = -2*pi*f*(pi*c^2*f + sqrt(-1)*b)
D = real([(b-sqrt(-1)*2*pi*c^2*f - t0)/(sqrt(2)*c)])
E = real([(b-sqrt(-1)*2*pi*c^2*f - tmax)/(sqrt(2)*c)])
F(f) = sqrt(pi/2)*a*c*exp(F)*[erf(D)-erf(E)]
I don't understand why I can't run this on my machine, or really why it is going to need such a huge chunk of memory to solve this equation. Is there a way that I can make the array that is requested smaller?

采纳的回答

Steven Lord
Steven Lord 2015-8-3
Look at this line of code.
F(f) = sqrt(pi/2)*a*c*exp(F)*[erf(D)-erf(E)]
You're attempting to assign to element f (=4e9) of the matrix F. This means that you're trying to grow your F matrix, which used to be a scalar (1-by-1), into a 4e9-by-1 matrix that will require nearly 30 GB of contiguous memory.
Perhaps if you explain what you're trying to do with that line of code (make it a function of f?) we can offer suggestions on how you can achieve that goal without creating such a huge array.
  5 个评论
imarquez
imarquez 2015-8-3
So essentially I know what I need to execute, but I just don't know how to do it since the way that my code is written I use a huge amount of RAM.
Walter Roberson
Walter Roberson 2015-8-3
Unfortunately there are characters in what you posted that most of us cannot read. U+F06F, U+F070, U+F077 are all in the Private Use section of Unicode.
F06F = some kind of specialized o
F070 = Pi
F077 = omega
I cannot determine the meaning of
Perhaps you could post an image of the formulae?

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2015-8-3
In MATLAB, the statement
F(f) = something
is an assignment to the array F indexed at the locations in f (unless f is a logical array, in which case it is an assignment to the locations in F selected where f is true.)
In order to be able to use such a statement in MATLAB, the index needs to be defined ahead of time.
In your case, your index is defined but it is a scalar that is very large, so you are attempting to assign to a single location that is several gigabytes into the array.
The indices you use in the left side of an array assignment must be non-negative integers, and they define the exact locations to store the results into the array. For example,
f = [3178 51 803432];
F(f) = [1 2 3]
would store 2 at location 51, 1 at location 3178, and 3 at location 803432 without changing any other values in the F array.
The one exception to the above rule is if you are defining a symbolic function which has already been declared using syms, such as if you had defined
syms F(f)
In that case if you had F(f) on the left hand side of the assignment, the right hand side of the assignment would have to be a symbolic expression involving the symbolic variable f (or it could be a symbolic expression independent of f, for the case where the function is to return a constant value.) This particular case where the left hand side has been given in advance in "syms" and the right hand side is symbolic defines a symbolic function rather than a symbolic expression.
If you are defining a formula that is to apply for values of f to be defined later, then you should use a symbolic formula or symbolic function, or you should define an anonymous function parameterized by the name of the variable whose value is to be given later. For example,
F = @(f) -2*pi*f*(pi*c^2*f + sqrt(-1)*b)
D = @(f) real([(b-sqrt(-1)*2*pi*c^2*f - t0)/(sqrt(2)*c)])
E = @(f) real([(b-sqrt(-1)*2*pi*c^2*f - tmax)/(sqrt(2)*c)])
Z = @(f) sqrt(pi/2)*a*c*exp(F(f))*[erf(D(f))-erf(E(f))]
and then Z would be a function that you could pass specific values of f to and get a numeric result.
If you are defining an expression that is to be applied to values that are already stored in f, then leave out the (f) in the assignment: if you aren't creating a formula for later application then you are misleading yourself by writing the result in function form.
Zf = sqrt(pi/2)*a*c*exp(F)*[erf(D)-erf(E)]
This would be a calculation for the particular f that you already have stored.
  4 个评论
imarquez
imarquez 2015-8-4
Can you explain to me a little more how the anonymous function works? Is the @(f) the only way to use the anonymous function or is that just a place holder?
Walter Roberson
Walter Roberson 2015-8-5
Whenever you have a function that could be coded in the form
function result = function_name(arg1, arg2, arg3...)
result = a single expression in the arguments
end
then instead of writing the function out to a file and referencing the function name, you can instead create an anonymous function by using
@(arg1, arg2, arg3) a single expression in the arguments
This becomes a value and you can pass the value between functions and you can assign the value to a variable. Whenever you have a name bound to represent the value, you can invoke the function on its arguments by naming the variable in function syntax:
f = @(arg1, arg2, arg3) a single expression in the arguments
.... f(3,sqrt(17),q) ...
and that will result in the code being executed after temporarily binding those argument values to the dummy variable names listed in the @(...) list (just like on a typical call)
There are some additional things you can do with anonymous functions that are not relevant right at the moment. For the purpose of understanding at the moment, think of it as a short-hand way of coding up a one-line "function" without using a file.

请先登录,再进行评论。

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by