Clear Filters
Clear Filters

Issue with making an array

1 view (last 30 days)
Tony
Tony on 23 Apr 2016
Commented: Tony on 24 Apr 2016
Here is my code:
function Tfunc(a,b)
a = (-(a-1)/a);
b = (-(b-1)/b);
for i = 1:30000
Approximation_b(i) = ((-1)^(1+i)) * ((b^i)/i);
Approximation_a(i) = ((-1)^(1+i)) * ((a^i)/i);
end
sum(Approximation_b)/sum(Approximation_a)
end
I tried making the following change (2 lines under function):
function Tfunc(a,b)
Approximation_b = zeros(30000);
Approximation_a = zeros(30000);
a = (-(a-1)/a);
b = (-(b-1)/b);
for i = 1:30000
Approximation_b(i) = ((-1)^(1+i)) * ((b^i)/i);
Approximation_a(i) = ((-1)^(1+i)) * ((a^i)/i);
end
sum(Approximation_b)/sum(Approximation_a)
end
I thought adding this would help the code run faster since I am creating the array ahead of time, but all I get is a slower code that 1) doesn't run, 2) memory problem.
Why am I getting a memory problem if the first code works just fine?

Accepted Answer

Walter Roberson
Walter Roberson on 23 Apr 2016
Approximation_b = zeros(1, 30000);
You were creating a 30000 by 30000 array.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!