How do get around this limitation on array size?

1 Ansicht (letzte 30 Tage)
Zachary Duff
Zachary Duff am 7 Jun. 2018
Beantwortet: Zachary Duff am 8 Jun. 2018
R2017a, R2014, different computers, same issue.
  1. Why does the computer (I'm pretty sure it's not MATLAB) limit me to arrays of length 32767?
  2. How do I circumvent this feature?
For example, in my command window I can enter
v = rand(1000000,1);
and the variable is generated without a problem. However, when I run a script or function designed to generate variables with approximate length 10^6, I become automatically restricted to
>> size(z)
ans =
32767 1
>> size(Y)
ans =
32767 3
this doesn't make any sense to me, especially considering
>> whos
Name Size Bytes Class Attributes
T 1x1 8 double
Y 32767x3 786408 double
sigma 1x1 8 double
z 32767x1 262136 double
>> memory
Maximum possible array: 5216 MB (5.470e+09 bytes) *
Memory available for all arrays: 5216 MB (5.470e+09 bytes) *
Memory used by MATLAB: 1180 MB (1.237e+09 bytes)
Physical Memory (RAM): 8124 MB (8.518e+09 bytes)
* Limited by System Memory (physical + swap file) available.
Note that 32767 = 2^15-1
Complete Code:
clc
clear
close all
% Solve over time interval [0 T] with initial conditions [1,1,1]
% f is set of differential equations
% Y is array containing x, y, and z variables
% t is time variable
sigma = 10;
beta = 8/3;
rho = 28;
tb = 20;
T = 2000;
% Lorenz system
R = @(Y) ...
[ -sigma*Y(1) + sigma*Y(2); ...
rho*Y(1) - Y(2) - Y(1)*Y(3); ...
-beta*Y(3) + Y(1)*Y(2) ];
[t,Y] = rk4lorenz(R,[0 T],[1 1 1]);
z = Y(:,3);
function [t,Y] = rk4lorenz(f, range, y0)
% RK4 Method for the Lorenz system
% range = time interval
% y0 = initial value vector
% h = time step
a = range(1)
b = range(2)
h = 1/128;
n = int16((b - a)/h)
Y = zeros(n+1,3);
t = zeros(n+1,1);
t(1) = a;
Y(1,:) = y0;
for i = 1:n
t(i+1) = t(i) + h;
Y(i+1,:) = rk4step(Y(i,:),h,f);
end
end
function Y = rk4step(Y,h,f)
Y = Y';
s1 = f(Y);
s2 = f(Y + h * s1/2);
s3 = f(Y + h * s2/2);
s4 = f(Y + h * s3);
Y = Y + h*(f(Y) + 2*s2 + 2*s3 + s4)/6;
end
  5 Kommentare
Zachary Duff
Zachary Duff am 7 Jun. 2018
R2017a. Just posted the script.
James Tursa
James Tursa am 7 Jun. 2018
What variables or lines of code seem to be the problem for you? I.e., what are you getting and what were you expecting?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Zachary Duff
Zachary Duff am 8 Jun. 2018

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by