Help solving an error

Here is the code:
load PollutionData.txt;
array = PollutionData(:,1); %#ok<NODEF>
timearray = (0:.5:250);
timearray = reshape(timearray,[],1);
constant = reaeration(array);
array(3,:) = array(3,:) .* 0.001076; %conversion
array(6,:) = array(6,:) .* 0.0002778; %conversion
resultarray = deOxygen(array , timearray, constant);
plot(timearray(:,1) , resultarray(:,1))
return
function constant = reaeration(array)
constant = ((array(5,:) * array(3,:)) ^ 0.5) / (array(4,:) .^ 1.5);
return
function resultarray = deOxygen(array , timearray, constant)
resultarray = zeros(1,1000);
for a = (0:.5:250)
resultarray(a) = (((array(6,:) * array(2,:)) / (constant - array(6,:))) * (exp(-array(6,:) * timearray(a) * 3600) - exp(-constant * timearray(a) * 3600))) + (array(1,:) * exp(-constant * timearray(a) * 3600));
end
return
I'm getting in error in the deOxygen function at the "resultarray(a) = (((..." portion. Also, the two following errors also appear:
Attempted to access timearray(0); index must be a positive integer or logical.
Error in myHW7 (line 15) resultarray = deOxygen(array , timearray, constant);
And I have no idea why. Any ideas?
Thanks

 Accepted Answer

In ‘deOxygen’, define the ‘a’ vector first:
a = (0:.5:250);
then set the loop up as:
for k1 = 1:length(a)
and replace the ‘(a)’ subscripts with ‘(k1)’ subscripts (or whatever loop counter variable you choose). MATLAB does not allow subscripts to be negative, zero, or non-integers.
Also, I’m not sure what ‘a’ is doing. You don’t use it anywhere in ‘deOxygen’ that I can find.

6 Comments

Alex
Alex on 8 Dec 2014
its supposed to be the point in the array that is used in the function
I’m still kinda lost.
How does ‘a’ relate to ‘resultarray’? As I read it, ‘timearray’ is a vector.
What do you want to do with ‘a’?
What is the original problem you want to solve? Knowing that may help me suggest solutions.
(Also, replacing (/) by (./) and (*) by (.*) will likely be in your long-term best interests. The dot operator here does element-wise operations rather than the default array operations MATLAB is noted for. That’s certainly not an immediate problem, but worth mentioning.)
Alex
Alex on 8 Dec 2014
sorry. im new to matlab. I usually program in c. in c, i did this same problem, but used a counter to count the number of times in the loop as well as dictate which point in the array would be used at that point in the loop. so what I'm trying to accomplish here is to get a result array of 500 values that can then be plotted vs the timearray
No need to apologise. It’s just that the more I understand, the more I can help.
I don’t know what ‘array’ is, but assuming the column size of ‘array’ is consistent with the size of ‘a’, you should be able to plot ‘resultarray’ as a function of ‘timearray’ as you currently calculate them, using my slightly modified version of your code (to avoid negative, zero, and fractional subscripts).
Also, considering ‘array’, you might have to subscript it as well, for example ‘array(6,k1)’. Otherwise, the way your code is currently written, you’re going to get subscript-array mismatch errors.
Alex
Alex on 9 Dec 2014
array is a 6 row, 1 column matrix of the text file i open at the beginning. just a few values that i was given and need to calculate the result array is all. the basic idea of this code is to take those six given values, along with two given generic equations, and solve. The first thing I find is a constant, which is what the reaeration function does. the next part is to calculate a deficit array using the six values, the constant, and the time array (which is from 0 to 250 hours every half hour) and then plot that resulting array as a function of time
Did you implement the array element subscript idea I suggested in my initial Answer? (You need to change all the ‘(a)’ subscripts to ‘k1’ or some variable of your choice. Keeping them as they are now simply will not work.) If so, did it at least solve part of the problem?
There are other potential problems in the way you are calculating ‘resultarray’ even with the subscripts corrected, but until you fix the subscripts it’s impossible to say what other problems may exist.
You’re currently doing matrix (as distinguished from element-wise) operations to create ‘resultarray’. This may or may not be appropriate, depending on what your ‘array’ variables are and what you want to calculate from them.

Sign in to comment.

More Answers (1)

Because indexes start with one. Why don't you do this:
index = 1;
for a = (0:.5:250)
resultarray(index) =..........
index = index + 1;
% more code......
end
Or else this:
aArray = 0:.5:250;
for index = 1 : length(aArray)
a = aArray(index); % Extract the value of a that we need.
resultarray(index) =..........
% more code......
end

Community Treasure Hunt

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

Start Hunting!