フィルターのクリア

how to reference previous value of function?

3 ビュー (過去 30 日間)
Vert
Vert 2014 年 10 月 19 日
コメント済み: Star Strider 2014 年 10 月 19 日
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.

採用された回答

Star Strider
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 件のコメント
Vert
Vert 2014 年 10 月 19 日
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 2014 年 10 月 19 日
My pleasure!
If I solved your problem, please Accept my answer.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by