How to loop through a function twice?

조회 수: 6 (최근 30일)
Vert
Vert 2014년 10월 20일
댓글: Star Strider 2014년 10월 22일
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

채택된 답변

Star Strider
Star Strider 2014년 10월 20일
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
Vert
Vert 2014년 10월 22일
No, I know it will have an effect. I'll keep trying.
Star Strider
Star Strider 2014년 10월 22일
See my answer to your subsequent Question to follow up on this.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by