how to reference previous value of function?
6 views (last 30 days)
Show older comments
I am trying to write a function that depends on its own previous value and the previous value of another variable.
Below is the code I am working on:
Storage, where Smax=100; k=0.3
TimeDay = 1:1088;
TimeDay = TimeDay';
K=0.3;
Storage = 0;
for i = 1:1088
Storage(end+1) = (1-K)*((Storage(end)) + (Precipmmday(end)));
end
Storage = Storage';
TimeDay = 1:1089;
plot (TimeDay, Storage)
The output is incorrect. I've tried a lot of ideas for editing it but can't figure it out. Any help appreciated!
I did it correctly in excel (attached). Also attached is the data file.
0 Comments
Accepted Answer
Star Strider
on 19 Oct 2014
This seems to work:
fidi = fopen('A1Rainfalldata.txt');
R = textscan(fidi, '%d %f', 'HeaderLines',1);
Precipmmday = cell2mat(R(:,2));
K = 0.3;
Storage = [Precipmmday(1) 0 0];
for k1 = 2:length(Precipmmday)
Storage(k1,:) = [Precipmmday(k1) (Storage(k1-1,2)+Precipmmday(k1-1))*(1-K) (Storage(k1-1,2)+Precipmmday(k1-1))*K];
end
The ‘Storage’ array matches your Excel output.
2 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets 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!