King fahd university of petroleum & minerals college of computer sciences & engineering


Experiment 8: MATLAB m-files and their use in system simulation



Download 184.48 Kb.
Page6/7
Date13.06.2017
Size184.48 Kb.
#20355
1   2   3   4   5   6   7

Experiment 8: MATLAB m-files and their use in system simulation.

MATLAB functionalities are exploited for running simulations for systems having varying parameters. The parameter variation is also used during the designing stage of a system, when the system responses are checked under varying conditions. MATLAB m-files are introduced to save and rerun the work done on MATLAB command window. Models created in Simulink can be executed in MATLAB command window or alternately by using an m-file.


Programming in MATLAB using M-files

MATLAB has a feature called M-files that can simplify its operational use. These are flies containing MATLAB statements, which can be saved and re-executed by a single MATLAB command. Three types of statements are particularly useful with Simulink.


• The values of the parameters of a model can be saved in an M-file and changed by editing this file if needed.

• Commands to execute a model can be included, so that the results of a simulation are created.

• Plot commands and other post simulation data analysis and display commands can be used to cause the production, printing, or storage of the desired product.
M-files are produced with a text editor; there is one built into MATLAB that is accessible from the toolbar or through the edit command.
The Simulink model for a mass-spring system, shown below, was developed in the last experiment. This model is used as a reference in this experiment to develop the understanding in programming MATLAB m-files.

Modify the program by writing the variables M, K and B in place of the values in the Simulink model. Save the model as exlmodel.mdl (You can use any other name but then use the same name in all the following m-files).


Loading variable values through MATLAB:

Simulink programs are run in MATLAB command window, even if executed by the run button in the Simulink window. So if the variables are defined in MATLAB and then Simulink program is executed, the Simulink program will take the variable values from the MATLAB command window.


Given below is a file that will set up the MATLAB workspace by establishing the values of the parameters needed for the Simulink simulation of the given model.

M-file for parameter values

% This file is named exl_parameter.m.

% Everything after a % sign on a line is a comment that

% is ignored by M This file establishes the

% parameter values for exl_model.mdl.

%

M2; %kg



K= 16; %N/m

B=4; % Ns/m


Plotting the outputs in MATLAB:

The file to create the plots of the output is given below. Create the file and save it by the name given below.


M-file to produce the plot

% This file is named exl_plot.m.

% It makes a plot of the data produced by exl_model.mdl.

plot(t,x); grid % Plots x for the case with B=4.

xlabel(’Time (s)’);

ylabel (‘Displacement (m) ')


A semicolon in a physical line ends the logical line, and anything after it is treated as if it were on a new physical line. A semicolon at the end of a line that generates output to the command window suppresses the printing of that output.
Program Execution:

Follow the following steps to execute these files:


• Enter the command exlparameter in the command window. This will load the parameter values of the model.

• Open the Simulink model exl_model.mdl and start the simulation by clicking on the toolbar entry Simulation> Start.

• Enter the command exl_ plot in the command window to make the plot.
Making Subplots in MATLAB:

When two or more variables are being studied simultaneously, it is frequently desirable to plot them one above the other on separate axes, as can be done for displacement and velocity in. This is accomplished with the subplot command. The following M-file uses this command to produce both plots of displacement and velocity.


M-flle to make subplots

% This file is named exlplot2.m.

% It makes both plots, displacement and velocity.

% Execute exlparameter.m first. subplot(2,l,1);

plot(t,x); grid % Plots x for the case with B=4. xlabel (‘Time (s) ‘) ;

ylabel (‘Displacement (m) ‘); subplot(2,1,2);

plot(t,v); grid % Plots v for the case with B=4. xlabel(’Time (s)’);

ylabel(’Velocity (mis)’);



Multiple runs in MATLAB:

If a complex plot is desired, in which several runs are needed with different parameters, this can all be accomplished by executing ex1_model (to load parameters) followed by this M-file. Entering the command ex1_plots in the command window results in multiple runs with varying values if B and will plot the results.


M-file to produce multiple runs and their plots

% This file is named exl_plots.m.

% It plots the data produced by exl_model.mdl for

% several values of B. Execute exl_parameter.m first.

sim(’exl_model’) % Has the same effect as clicking on

% Start on the toolbar.

plot(t,x) % Plots the initial run with B=4

hold on % Plots later results on the same axes % as the first.

B = 8; % New value of B; other parameter values % stay the same.

sim(exl_model % Rerun the simulation with new B value.

plot(t,x) % Plots new x on original axes.

B 12; sim(’exl_model’);plot(t,x)

B = 25; sim(’exl_model’ ) ;plot(t,x)

hold off
REPORT:


Copy of your programs

Results of the programs

Comments on the results

Comparison of results with analytical values where possible




Experiment # 9: Simulation of systems having relative displacements with other moving body



INTRODUCTION
In a linear translational system such as the two-mass system shown below in the figure, the displacement variable associated with each mass can be expressed with respect to a fixed reference position. However, sometimes the position of a mass is measured with respect to some other moving object rather than a fixed point. This is known as relative displacement.
Let x denote the position of mass M1 with respect to fixed reference point and z denote the relative displacement of mass M2 with respect to mass M1. The positive direction for both displacements is towards right. The two springs are neither stretched nor compressed when x=z=0.


z

x

K2

K1

M1

M2

B1

B2

B3

Fa(t)


PROCEDURE:
• Draw the free body diagram for the two-mass system

• Write the modeling equation describing the system from the free body diagram

• Compare the equations with the equations given below



• Solve the equations for the highest derivative of the output.

• Draw the corresponding Simulink diagram.

• Use the To Workspace blocks for t, fa(t), x, and z in order to allow MATLAB to plot the desired responses. Set the save format to array in block parameters.


EXERCISE 1:
Plot the first 10 seconds of the response when the applied force fa(t) increases from 0 to 100 N at t= 1sec.. The parameter values are: M1 = M2 = 5 kg, B1 = B2= 20 N.s/m, B3=50

N.s/m, and K1 = K2 = 100 N/m. Plot both x and z on the same axes.



PROCEDURE:
• Use Step block to provide the input fa(t).

• In the Step block, set the initial and final values and the time of step when it occurs.

• Select the duration of the simulation to be 10 seconds from the Simulation > Parameters entry on the toolbar

• Write an m-file setting the system parameters,

• Include system execution commands in the m-file

• Also include plotting statements in the same m-file.

• Run the simulation in MATLAB command window and show your work.
EXERCISE 2:
From exercise 1, change the values of B2 =30 N.s/m and B3 =20 N.s/m and K1 =20 N/m.

Rerun the simulation and compare the responses.


PROCEDURE:
• Make modifications in your m-file to change the parameters given above.

• Run your simulation again

• Compare the results of Exercise 1 and 2 and comment on its physical interpretation.
EXERCISE 3:
Repeat Exercise 1 when the displacement of each mass is expressed with respect to its own fixed reference position. Let x1 and x2 denote the displacements of M1 and M2 respectively with the positive senses to the right.
PROCEDURE:
• Draw the free body diagram for the two-mass system

• Write the modeling equation describing the system from the free body diagram

• Compare the equations with the equations given below



• Solve the equations for the highest derivative of the output.

• Draw the corresponding Simulink diagram.

• Use the To Workspace blocks for t, fa(t), x1, and x2 in order to allow MATLAB to plot the desired responses. Set the save format to array in block parameters.

• Now use the procedure of Exercise 1 to run the simulation

• Comment on the output and compare with the output of Exercise 1.




Download 184.48 Kb.

Share with your friends:
1   2   3   4   5   6   7




The database is protected by copyright ©ininet.org 2024
send message

    Main page