Main Content

このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。

生成コードを再コンパイルしないバッチ シミュレーションの実行

この例では、生成コードを再コンパイルせずにバッチ シミュレーションを実行する方法を説明します。この例では、MAT ファイルのデータを読み取ることによって、入力信号データとモデル パラメーターを変更します。前半の部分 (手順 1 ~ 5) では、伝達関数の減衰比を変更することにより、10 個のパラメーター セットが Simulink® モデルから作成されます。10 個のパラメーター セットは MAT ファイルに保存され、RSim の実行ファイルは指定されたパラメーター セットをこのファイルから読み取ります。この例の後半の部分 (手順 6 ~ 7) では、5 個の信号データ チャープ セットが次第に高くなる周波数と共に作成されます。どちらの部分でも、RSim の実行ファイルはシミュレーションのセットを実行し、固有のシミュレーション結果を含む出力 MAT ファイルを作成します。最後に、実行の複合が MATLAB® の図に表示されます。

Simulink 環境で複数のシミュレーションをすばやく実行するには、RSim の代わりにラピッド アクセラレータの使用を検討してください。アクセラレーションとはを参照してください。

手順 1. 準備

この例ではファイルを作成するため、現在のディレクトリ内が書き込み可能であることを確認します。

[stat, fa] = fileattrib(pwd);
if ~fa.UserWrite
    disp('This script must be run in a writable directory');
    return;
end

モデルを開き、RSim ターゲットを使用するように設定します。操作のグラフィカルな説明および他の RSim ターゲット関連オプションの詳細については、システム ターゲット ファイルの構成 (Embedded Coder)を参照してください。

mdlName = 'rtwdemo_rsimtf';
open_system(mdlName);
cs = getActiveConfigSet(mdlName);
cs.switchTarget('rsim.tlc',[]);

MAT ファイル rsim_tfdata.mat はローカル ディレクトリにあることが必要です。

if ~isempty(dir('rsim_tfdata.mat')),
    delete('rsim_tfdata.mat');
end
str1 = fullfile(matlabroot,'toolbox','rtw','rtwdemos','rsimdemos','rsim_tfdata.mat');
str2 = ['copyfile(''', str1, ''',''rsim_tfdata.mat'',''writable'')'];
eval(str2);

手順 2. モデルのビルド

モデルの RSim 実行可能ファイルをビルドします。ビルド プロセスの間、モデルについて構造的なチェックサムが計算され、生成された実行可能ファイルに組み込まれます。このチェックサムを使用して、実行可能ファイルに渡されたパラメーター セットに互換性があることを確認します。

evalin('base','w = 70;')
evalin('base','theta = 1.0;')
disp('Building compiled RSim simulation.')
slbuild(mdlName);
Building compiled RSim simulation.
### Starting build procedure for: rtwdemo_rsimtf
### Successful completion of build procedure for: rtwdemo_rsimtf

Build Summary

Top model targets built:

Model           Action                        Rebuild Reason                                    
================================================================================================
rtwdemo_rsimtf  Code generated and compiled.  Code generation information file does not exist.  

1 of 1 models built (0 models already up to date)
Build duration: 0h 0m 10.025s

手順 3. 既定のパラメーター セットの取得および 10 個のパラメーター セットの作成

disp('Creating rtP data files')
for i=1:10
  % Extract current rtP structure using new damping factor.
  [rtpstruct]=evalin('base','rsimgetrtp(''rtwdemo_rsimtf'');');
  savestr = strcat('save params',num2str(i),'.mat rtpstruct');
  eval(savestr);
  evalin('base','theta = theta - .1;');
end
disp('Finished creating parameter data files.')
Creating rtP data files
Finished creating parameter data files.

手順 4. 新しい パラメーター セットを使用した 10 個の RSim シミュレーションの実行、および結果のプロット

figure
for i=1:10
  % Bang out and run a simulation using new parameter data
  runstr = ['.', filesep, 'rtwdemo_rsimtf -p params',num2str(i),'.mat', ' -v'];
  [status, result] = system(runstr);
  if status ~= 0, error(result); end
  % Load simulation data into MATLAB for plotting.
  load rtwdemo_rsimtf.mat;
  axis([0 1 0 2]);
  plot(rt_tout, rt_yout)
  hold on
end

このプロットは、それぞれが別々の減衰比を使用する 10 個のシミュレーションを示します。

手順 5. 時間ベクトルおよび初期周波数ベクトルの設定

シミュレーション結果のウィンドウ化およびスペクトル解析を実行する場合、時間ベクトルには 4096 個のポイントがあります。

dt = .001;
nn = [0:1:4095];
t = dt*nn; [m,n] = size(t);
wlo = 1; whi = 4;
omega = [wlo:((whi-wlo)/n):whi - (whi-wlo)/n];

手順 6. MAT ファイルの 5 個の信号データのセットの作成

チャープ データをもつ .mat ファイルを作成します。

disp('This part of the example illustrates a sequence of 5 plots. Each')
disp('plot shows an input chirp signal of certain frequency range.')
for i = 1:5
  wlo = whi; whi = 3*whi; % keep increasing frequencies
  omega = [wlo:((whi-wlo)/n):whi - (whi-wlo)/n];
  % In a real application we recommend shaping the chirp using
  % a windowing function (hamming or hanning window, etc.)
  % This example does not use a windowing function.
  u      = sin(omega.*t);
  tudata = [t;u];
  % At each pass, save one more set of tudata to the next
  % .mat file.
  savestr = strcat('save sweep',num2str(i),'.mat tudata');
  eval(savestr);
  % Display each chirp. Note that this is only input data.
  % Simulations have not been run yet.
  plotstr = strcat('subplot(5,1,',num2str(i),');');
  eval(plotstr);
  plot(t,u)
  pause(0.3)
end
This part of the example illustrates a sequence of 5 plots. Each
plot shows an input chirp signal of certain frequency range.

手順 7. 新しい信号データを使用した RSim コンパイル済みシミュレーションの実行

元の信号データ (rsim_tfdata.mat) を、sweep1.mat、sweep2.mat などのファイルで置き換えます。

disp('Starting batch simulations.')
for i = 1:5
  % Bang out and run the next set of data with RSim
  runstr = ['.', filesep, 'rtwdemo_rsimtf -f rsim_tfdata.mat=sweep', ...
            num2str(i),'.mat -v -tf 4.096'];
  [status, result] = system(runstr);
  if status ~= 0, error(result); end
  % Load the data to MATLAB and plot the results.
  load rtwdemo_rsimtf.mat
  plotstr = strcat('subplot(5,1,',num2str(i),');');
  eval(plotstr);
  plot(rt_tout, rt_yout); axis([0 4.1 -3 3]);
end
zoom on
% cleanup
evalin('base','clear w theta')
disp('This part of the example illustrates a sequence of 5 plots. Each plot')
disp('shows the simulation results for the next frequency range. Using the')
disp('mouse, zoom in on each signal to observe signal amplitudes.')
close_system(mdlName, 0);
Starting batch simulations.
This part of the example illustrates a sequence of 5 plots. Each plot
shows the simulation results for the next frequency range. Using the
mouse, zoom in on each signal to observe signal amplitudes.