How to loop through a function twice?
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Vert
am 20 Okt. 2014
Kommentiert: Star Strider
am 22 Okt. 2014
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
0 Kommentare
Akzeptierte Antwort
Star Strider
am 20 Okt. 2014
You needed to create separate loops for ‘Smax’ and ‘K’. This runs. I will let you decide if it gives the results you want:
Storage = [Precipmmday(1) 0];
Baseflow = [Precipmmday(1) 0];
Smax = linspace(10,500,10);
K = linspace(0.01,1,10);
for k3 = 1:length(Smax)
for k2 = 1:length(K)
for k1 = 2:length(Precipmmday)
Baseflow(k1,k2,k3) = [(Storage(k1-1,2)+Precipmmday(k1-1))*K(k2)];
Storage(k1,k2,k3) = [(Storage(k1-1,2)+Precipmmday(k1-1))*(1-K(k2))];
end
ModelStorage = Storage;
ModelSpill=ModelStorage - Smax(k3);
ModelSpill(ModelSpill<0)=0;
ModelStorage(ModelStorage>Smax(k3))=Smax(k3);
ModelBaseflow = Baseflow;
ModelOutflow = ModelBaseflow + ModelSpill;
end
end
I still had the data file and code from yesterday, so I used it to compute ‘Storage’. I tested these revisions on your data.
8 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Axis Labels finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!