Effacer les filtres
Effacer les filtres

how to reference previous value of function?

2 vues (au cours des 30 derniers jours)
Vert
Vert le 19 Oct 2014
Commenté : Star Strider le 19 Oct 2014
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.

Réponse acceptée

Star Strider
Star Strider le 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 commentaires
Vert
Vert le 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 le 19 Oct 2014
My pleasure!
If I solved your problem, please Accept my answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by