How to loop through a function twice?
Show older comments
I have a function that now works for individual values. I am trying (and so far, failing) to run the function for several values for each of two parameters: K and Smax.
Any help would be very appreciated. This is my first time using MatLab.
Here is the functional, version I'm using for individual trials:
No Spill Scenario Storage where k=0.3; Smax=100
SynthK=0.3;
SynthSmax=100;
Storage = [Precipmmday(1) 0];
Baseflow = [Precipmmday(1) 0];
for k1 = 2:length(Precipmmday)
Baseflow (k1,:) = [(Storage(k1-1,2)+Precipmmday(k1-1))*SynthK];
Storage(k1,:) = [(Storage(k1-1,2)+Precipmmday(k1-1))*(1-SynthK)];
end
SynthStorage = Storage;
SynthStorage (:,1) =[];
SynthSpill=SynthStorage - SynthSmax;
SynthSpill(SynthSpill<0)=0;
SynthStorage(SynthStorage>SynthSmax)=SynthSmax;
SynthBaseflow = Baseflow;
SynthBaseflow (:,1) = [];
SynthOutflow = SynthBaseflow + SynthSpill;
And here is what I'm working on to run through many values for the two parameters:
Run Model
Storage = [Precipmmday(1) 0];
Baseflow = [Precipmmday(1) 0];
for Smax = linspace(10,500,10);
Smax = repmat(Smax,10,1);
for K = linspace(0.01,1,10);
K = repmat(K,10,1);
for k1 = 2:length(Precipmmday)
Baseflow(k1,:) = [(Storage(k1-1,2)+Precipmmday(k1-1))*K];
Storage(k1,:) = [(Storage(k1-1,2)+Precipmmday(k1-1))*(1-K)];
end
ModelStorage = Storage;
ModelSpill=ModelStorage - Smax;
ModelSpill(ModelSpill<0)=0;
ModelStorage(ModelStorage>Smax)=Smax;
ModelBaseflow = Baseflow;
ModelOutflow = ModelBaseflow + ModelSpill;
end
end
I can see where some of the errors are but don't know how to fix them. I think the way I'm defining Storage and Baseflow is an issue. Also, I keep getting the "Subscripted assignment dimension mismatch." or "Attempted to access Storage(2,2); index out of bounds becausesize(Storage)=[10,1]." error messages.
Thank you for any suggestions you have.
ETA: Data file is attached
Accepted Answer
More Answers (0)
Categories
Find more on Climate Science and Analysis 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!