Creating a plot with linspace function

5 views (last 30 days)
Alex
Alex on 10 Dec 2014
Commented: dpb on 10 Dec 2014
Here is the 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 , resultarray)
xlabel('Time after pollutant is added(hours)');
ylabel('Dissolved oxygen deficit (mg/l)');
for k1= 2:501
if resultarray(k1) <= array(1,1)
disp(['The deficit will fall below the initial deficit at time ' num2str(timearray(k1))])
break;
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)
for k1 = 1:length(timearray)
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)); %#ok<AGROW>
end
return
So my next step is to create a plot of varying flow velocities using the 'linspace(100,250,6)' function and plot those as timearray vs resultarray, but I have no clue what that means. The flow velocity is defined as array(5,1) from the text file.
How would I incorporate this new linear vector into what I have to get a plot containing six different lines?
  1 Comment
dpb
dpb on 10 Dec 2014
Couple of comments on code...
timearray = (0:.5:250);
reshape(timearray, [501,1]);
The reshape above does nothing as you didn't assign the result to anything. Matlab does NOT ever modify arguments. If you want a column vector for timearray write
timearray = (0:.5:250).';
% NB the dot-transpose operator (not complex transpose ')
function resultarray = deOxygen(array , timearray, constant)
resultarray = (((array(6,1).*array(2,1))/(constant-array(6,1))) ...
.* (exp(-array(6,1)*timearray*3600)- exp(-constant*timearray*3600))) ...
.+ (array(1,1)*exp(-constant*timearray*3600));
Don't need a loop; use the vectorized operations in Matlab. The "dot-operators" are used for element-by-element operations on arrays and vectors; aren't need to apply a constant to such (altho doesn't hurt).
As to the question posed, I'm not certain what you're after -- the output of
linspace(100,250,6)
will be a series of length 6 from 100 to 250 similarly to your above timearray variable you created with the colon operator. But what, precisely you really want to plat versus that at such a disparate set of values isn't clear to me but whatever they are, you'll need a set of six values to match.
Oh, I suppose it's possible you're looking to find the places within the computed array that matchup -- that would be
>> [~,~,ib]=intersect(linspace(100,250,6),timearray)
ib =
201 261 321 381 441 501
>>
so you could get resultarray(ib) for the computed values at those times. That doesn't seem to have anything to do with any flow velocity, however, so I'm still uncertain as to the actual question.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!