My array returns all zeros. Help
4 次查看(过去 30 天)
显示 更早的评论
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
回答(1 个)
Aditya Nair
2021-1-2
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!