42 lines
982 B
Matlab
42 lines
982 B
Matlab
clear;
|
|
|
|
model_name = "rc_circuit";
|
|
|
|
stopTime = 5;
|
|
V_in = 10;
|
|
R = 1e3;
|
|
C = 1e-4;
|
|
freq = 1;
|
|
timestep = 0.01;
|
|
|
|
ts = (0:timestep:stopTime)';
|
|
vs = sin(2*pi*freq * ts);
|
|
|
|
sim_in = Simulink.SimulationInput(model_name);
|
|
% Note: the following has effect only if the system mask has been created
|
|
% for the settable parameters.
|
|
% Using Simulink.Parameter(R) in the above doesn't work.
|
|
sim_in = sim_in.setModelParameter("R", num2str(R));
|
|
|
|
% for model workspace variables, it's important to specifiy the workspace
|
|
% it's not necessary for the variable to be in the mask or to be argument
|
|
sim_in = sim_in.setVariable("C", C, Workspace=model_name);
|
|
sim_in = sim_in.setModelParameter("StopTime", num2str(stopTime));
|
|
|
|
% note: to be compatible with port size, ts must be a column vector
|
|
sim_in = sim_in.setExternalInput([ts, vs]);
|
|
|
|
% simulate
|
|
res = sim(sim_in);
|
|
|
|
figure;
|
|
subplot(2, 1, 1);
|
|
plot(ts, vs);
|
|
xlabel('Time (seconds)');
|
|
ylabel('V_{in} (Volt)');
|
|
|
|
subplot(2, 1, 2);
|
|
plot(res.yout{1}.Values);
|
|
|
|
|