2017-01-12 10 views
0

私は次の例のようなものに取り組んでいます:<x(t)>は、シミュレーションの数を超えた関数x(t)の平均です。これを行うために、私は次のコードを生成します。異なるパラメータ値のシミュレーション上の平均を計算する

sim=50;% number of simulations 
t=linspace(0,1);% time interval 

a_range=[1,2,3];% different values for the parameter a 
b_range=[0,0.5,1];% different values for the parameter b 
z=zeros(1,sim); 
theta=zeros(1,sim); 

for nplot=1:3 
    a=a_range(nplot); 
    b=b_range(nplot); 
    average_x=zeros(nplot,sim); 
    for i=1:sim 
     z(i)=rand(1);% random number for every simulation 
     theta(i)=pi*rand(1);% random number for every simulation  
    x=z(i)*t.^2+a*sin(theta(i))+b.*tan(theta(i));% the function 

    end 
    average_x(nplot,sim)=mean(x);% average over the number of simulations 
end 

fname=['xsin.mat']; 
save(fname) 

時間は100で、ベクトル1であり、xは100で、ベクトル1である、とaverage_xは私が探しています何50で1で書くことですスクリプトを使用してファイルをロードし、異なるパラメータaとbの平均時間をプロットします。だから私は図1で平均をプロットするように3つの数字を生成するコードを書いて、a = 1とb = 0に対して平均をプロットしたいと思います。

次に、図2では、平均を再びプロットしますが、a = 2およびb = 0.5などとします。問題は時間tの次元で平均は同じではありません。どのようにしてこの問題を解決し、3つの異なる数字を生成できますか?あなたがしたい場合

load('results.mat') 
for k = 1:size(average_x,1) 
    figure(k) 
    plot(t,average_x(k,:)) 
    title(['Parameter set ' num2str(k)]) 
    xlabel('Time') 
    ylabel('mean x') 
end 

これは1つの図のプロットである(:

+0

100値に対して50の値をプロットするとします。これは、100 **または**の代わりに50の値を選択せず​​に、50の値を100に補間することは不可能です。 – EBH

答えて

2

私が正しくあなたの意図を持っている場合、これはあなたが探して何をされて:あなたが書くことができ、別のファイルでは

sim = 50;% number of simulations 
t = linspace(0,1);% time interval 

a_range = [1,2,3];% different values for the parameter a 
b_range = [0,0.5,1];% different values for the parameter b 

% NO NEED TO GENERATE THE RANDOM NUMBERS ONE BY ONE: 
theta = pi*rand(sim,1);% random number for every simulation 
z = rand(sim,1); % random number for every simulation 

% YOU SOULD INITIALIZE ALL YOUR VARIABLES OUTSIDE THE LOOPS: 
x = zeros(sim,numel(t)); 
average_x = zeros(3,numel(t));% the mean accross simulations 
% for average accros time use: 
% average_x = zeros(3,sim); 

for nplot=1:3 
    a = a_range(nplot); 
    b = b_range(nplot); 
    for i=1:sim 
     x(i,:) = z(i)*t.^2+a*sin(theta(i))+b.*tan(theta(i));% the function 
    end 
    average_x(nplot,:) = mean(x); % average over the number of simulations 
    % average_x(nplot,:) = mean(x,2); % average accross time 
end 
% save the relevant variables: 
save('results.mat','average_x','t') 

その後、シミュレーションの平均):

mean sim


ところで、コードをもっとコンパクトにするには、bsxfunを中心にベクトル化することができます。

% assuming all parameters are defined as above: 
zt = bsxfun(@times,z,t.^2); % first part of the function 'z(i)*t.^2' 
% second part of the function 'a*sin(theta(i)) + b.*tan(theta(i))': 
ab = bsxfun(@times,a_range,sin(theta)) + bsxfun(@times,b_range,tan(theta)); 
% convert the second part to the right dimensions and size: 
ab = repmat(reshape(ab,[],1,3),1,numel(t),1); 
x = bsxfun(@plus,zt,ab); % the function 
average_x = squeeze(mean(x)); % take the mean by simulation 
plot(t,average_x) % plot it all at once, as in the figure above 
xlabel('Time') 
ylabel('mean x') 
+0

あなたの投稿をありがとうございます。私が正しいとすれば、図1は時間に対するシミュレーションの平均のプロットです。つまり、x軸は時間です。だからそれはplot(t、average_x)でなければなりません。 forループに来ると混乱します。もし私が間違っていれば私を訂正してもかまいません:x(i、:)はすべてのシミュレーションが関数を計算し、行iに値を格納することを意味します。したがって、xを50で100にするループを終了します。次に、average_x(nplot、:)はすべてのnplotを意味し、すべての列で平均(x)を計算しaverage_x(nplot、:)に割り当てます。平均(x)はシミュレーション上の平均を計算することができますか? – David

+0

私は図1のようなものを探していますが、関数とその意味はランダム変数 'z'と' theta'の意味で定義されているので、曲線にランダム性があるべきではありません。それとも、ランダム関数の平均が滑らかな曲線になるのは本当ですか?繰り返しますが、あなたが書いたコードは私が探していたものです。ファイルを保存してから別の3つの数値で平均をプロットする方法を待っています。 – David

+0

ランダム性が見られない理由は、 'z'と' theta'は時間に対して '定数'であり、シミュレーション間でのみ可変であるということです。シミュレーション(つまり、タイムステップごとに50個の値を取る)を平均するので、タイムステップごとに異なる「z」と「theta」はすべて1つの値に崩壊し、ランダム性はありません。 – EBH

関連する問題