My array returns all zeros. Help
Show older comments
So my 'resultarray' in the 'deOxygen' function gives me all zeros and I'm not sure why. The values of the pollutiondata.txt file are as follows: array =
5.0000
1.0000
0.0000
20.0000
0.0550
0.2000
These are rounded and I don't want them to be.
Here is my entire code:
function myHW7()
% Alex
% December 12, 2014
% myHW7.m
load PollutionData.txt;
array = PollutionData(:,1); %#ok<NODEF>
timearray = (0:.5:250);
reshape(timearray, [501,1]);
array(3,:) = array(3,:) .* 0.001076; %conversion
array(6,:) = array(6,:) .* 0.0002778; %conversion
constant = reaeration(array);
resultarray = deOxygen(array , timearray, constant);
plot(timearray(:,1) , resultarray(:,1))
xlabel('Time after pollutant is added(hours)');
ylabel('Dissolved oxygen deficit (mg/l)');
for k1= 1:501
if resultarray(k1) == array(1,:)
disp(['The deficit will equal the initial deficit at time ' num2str(timearray(k1))])
end
end
return
function constant = reaeration(array)
constant = ((array(5,1) * array(3,1)) ^ 0.5) / (array(4,1) .^ 1.5);
return
function resultarray = deOxygen(array , timearray, constant)
resultarray = zeros(1,1000);
a = (0:0.5:250);
for k1 = 1:length(a)
resultarray(k1) = (((array(6,1) .* array(2,1)) ./ (constant - array(6,1))) .* (exp(-array(6,1) .* timearray(k1) .* 3600) - exp(-constant .* timearray(k1) .* 3600))) + (array(1,1) .* exp(-constant .* timearray(k1) .* 3600));
end
reshape(resultarray, [1000,1]);
return
Any ideas?
Thanks
3 Comments
Adam
on 10 Dec 2014
Have you tried splitting up the code in deOxygen onto multiple lines and putting a breakpoint in to see what is going on in the function?
Also please use the '{} Code' button for formatting code to make it readable (select all your text that is code and just click it)
Alex
on 10 Dec 2014
Alex
on 10 Dec 2014
Answers (1)
Aditya Nair
on 2 Jan 2021
0 votes
Hey! I recently encountered a similar problem. After racking my brains I realised that referencing an array incorrectly could actually make a new array whose values are set to zero. I was referencing the 1-D arrays in my 2-D array x(91,1500) as x(i) as opposed to x( i, : ) which is actually the proper way. Hope this 6 year late comment helps!
Categories
Find more on Logical 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!