[英]Parameter Optimization in MATLAB
我试图得出Scf
, TT
, C
, B
的最佳参数集。 我已经阅读了一些有关在MATLAB中在何处以及如何执行此操作的文章,但它们看起来是如此令人困惑,并且这些示例并未转移到我的代码中……或似乎没有。 我非常希望能对我的代码的外观提供一些指导。 我如何要求MATLAB最小化观测到的雪水当量值与估计值之间的目标函数? 这些变量显然必须保持在一定范围内,那么如何在代码中显示这些变量?
%Degree-day method for snow melting.
%Data from the ... Switzerland
x=load('C:\n.dat'); %Load the file with data
td=x(:,4); %hour of the day
GlobalRad=x(:,5); %Global Radiation
T=x(:,6); %Air Temperature
P=x(:,7); %Precipitation
Snow_obs=x(:,8); %SWE Observed
SnowDepth=x(:,9); %Snow Depth Observed
ts=x(:,10); %start time of daylight on day d (t0)
te=x(:,11); %end time of daylight on day d (t1)
dTd=x(:,12); %difference between the max and min daily temperatures on day d
snow_sim(1)=0;
runoff=zeros(length(P));
%Parameters (TO BE CALIBRATED)
Scf=1.3; %Snowfall correction factor
TT=1; %Threshold temperature
C=3.5; %Degree-day factor (mm day-1 C-1)(Ac)
B=0.05; % Factor to convert the temp amplitude into a degree day factor
%Parameters (USE DEFAULT)
Cfr=0.05; %Refreezing coefficient (use default value of 0.05)
Cwh=0.1; %Water holding capacity (use default value of 0.1)
snow_sim(1)=0; %Simulated snowpack is 0 mm for day 1
snow_sim_water(1)=0; %Liquid water in snowpack for day 1 the water is 0 mm
for t=1 : length(td); %time series loop
ln(t)=24-te(t)+ts(t); %length of the night
Z(t)=2*((te(t)-ts(t))/(3.14*ln(t))); %factor ensuring that daily mean vaules of As equals Ac
if ts(t)<=td(t)<te(t);
As(t)=C+(B*dTd(t)*(sin(3.14*((td(t)-ts(t))/(te(t)-ts(t)))))); %equation for time variant degree day factor scenario 1
else As(t)=C-(B*dTd(t)*Z(t)); %equation for time variant degree day factor scenario 2
end
end
as=As'; % transpose As from a row to a column vector
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for t=2 : length(P); %time series loop
if T(t)< TT; %If the temperature for day t is lower than the threshold value for melting (=below TT degrees) then refreezing of melt water will occur
refreez(t)=Cfr*as(t)*(TT-T(t)); %Equation for refreezing of meltwater
if refreez(t) > snow_sim_water(t-1); %If the refreezing for day t is larger than the water in the snowpack the day before, then the refreezing is limited
refreez(t)= snow_sim_water(t-1); %Can't freez more water than is accumulated in the snowpack
end
snow_sim(t)=P(t)*Scf+snow_sim(t-1)+refreez(t); %The total simulated snowpack for any given day is the precipitation that day together with snow pack from day before and refreeze of that day.
snow_sim_water(t)=snow_sim_water(t-1)-refreez(t); %The total simulated amount of water in the snowpack is the water in the snowpack the day before minus the water refrozen the same day
else %T(t) > TT %temperature above threshold temperature, snowmelt will occur
Melt(t)=as(t)*(T(t)-TT); %Equations for melting rate of existing snowpack
if Melt(t) > snow_sim(t-1); %If the melting rate for day t is larger than the snowpack the day before, then the melting is limited
Melt(t)= snow_sim(t-1); %Because it can't melt more snow than is available
end
snow_sim(t)=snow_sim(t-1)-Melt(t); %Total simulated snow is the simulated snowpack for the day before minus the melted snow
snow_sim_water(t)=snow_sim_water(t-1)+P(t)+Melt(t); %Total water amount in snow is the water amount in snow for the day before plus the precipitation and the melted snow
if Cwh*snow_sim(t) < snow_sim_water(t); %The snowpack can retain as much as 10% of its water equivalent, but not more
runoff(t)=snow_sim_water(t)-0.1*snow_sim(t); %if there is more liquid water, this goes to runoff (note:if there is no snowpack all water will go to runoff
snow_sim_water(t)=0.1*snow_sim(t);
end
end
end
snow_sim_total=snow_sim+snow_sim_water; %The total simulated snowpack is the water in snow and the simulated snowpack
daynr=1:length(P);
正如上面其他人提到的,您的代码在这里很长。 但是,我为您提供一些建议。 通常,当您要优化几个参数(超级参数)时,需要更改它们的值,评估输出(即计算输出中的误差)。 您需要记住,每次运行更改一个值,并保持其他参数不变(恒定)。 就像您在代码中一样,此过程必须循环执行(for循环)。 最后,您将创建一些图形/表格以查看在何处实现了优化的参数。 如果使用任何错误,则优化的参数将使错误最小化。 假设对于特定的C和B ...误差最小。 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.