how to reference previous value of function?
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Star Strider
2014 年 10 月 19 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!