kennethkam.com

Using MATLAB to Control Simulink

The final year of university is taking its toll on me. The act of balancing a research project, a team design project, several reports, numerous job applications, and MSc applications has took all my time away. Fortunately, there’s still a little glimmer of hope in all that work – in the form of MATLAB.

This is a short snippet of code used to interface the simulink package in MATLAB. There’s a model simulating the frequency response to a sinusoidal wave of a first order dynamic system and I wanted a way to use MATLAB to loop over a range of input frequencies into the model. The documentation isn’t even half as good as Django’s but I got there in the end. Here’s the code:

i = 1
for h = [10:10:1000]
    % change the frequency from 10 to 1000
    frequency = h
    % run simulink
    sim('frequencyresponse')
    % output is the response
    % take values from 1 s onwards: (1000th number as step is 0.001)
    steady_output = output(1000:2002)
    % take the maximum value of steady_output to find the steady state gain
    steady_max = max(steady_output)
    % assign the max value into an array
    result(i,1) = steady_max
    i = i + 1
end

The important part was the sim('frequencyresponse') part. ‘frequencyresponse’ is the name of my Simulink model and the sim command runs the model. The rest was easy.

A bit of geeking on a Friday night and good progress made on my project cheers me up.

Comments

  • There are no comments.

Add a Comment