how to reference previous value of function?

6 views (last 30 days)
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.

Accepted Answer

Star Strider
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
Vert
Vert on 19 Oct 2014
Thank you so much, I really appreciate it. Someday when I am competent in MatLab I will remember to help people learn!
Star Strider
Star Strider on 19 Oct 2014
My pleasure!
If I solved your problem, please Accept my answer.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!