Modeling and Simulation LabEXPT 2
Modeling and Simulation LabEXPT 2
Experiment No 2
Modelling and Simulation of a Mechanical System of one
variable using MATLAB
Part A
Functions are a very useful concept to perform specific computations. They facilitate the user to go
beyond the predefined functions and write code that is for specific purpose and can be called and
recalled easily . The users are being familiarized with the syntax and usage in the following examples.
Syntax
Description
example
function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts inputs x1,...,xM and
returns outputs y1,...,yN. This declaration statement must be the first executable line of the function.
Valid function names begin with an alphabetic character, and can contain letters, numbers, or
underscores.
In a function file which contains only function definitions. The name of the file should match the name of
the first function in the file.
In a script file which contains commands and function definitions. Functions must be at the end of the
file. Script files cannot have the same name as a function in the file. Functions are supported in scripts in
R2016b or later.
Files can include multiple local functions or nested functions. For readability, use the end keyword to
indicate the end of each function in a file. The end keyword is required when:
The function is a local function within a function file, and any local function in the file uses the end
keyword.
Examples
Define a function in a file named average.m that accepts an input vector, calculates the average of the
values, and returns a single result.
function y = average(x)
if ~isvector(x)
end
y = sum(x)/length(x);
end
z = 1:99;
average(z)
ans =
50
Define a script in a file named integrationScript.m that computes the value of the integrand at and
computes the area under the curve from 0 to . Include a local function that defines the integrand, .
x = 2*pi/3;
y = myIntegrand(x)
xmin = 0;
xmax = pi;
f = @myIntegrand;
a = integral(f,xmin,xmax)
function y = myIntegrand(x)
y = sin(x).^3;
end
y=
0.6495
a=
1.3333
Function with Multiple Outputs
Define a function in a file named stat.m that returns the mean and standard deviation of an input
vector.
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end
[ave,stdev] = stat(values)
ave =
47.3400
stdev =
29.4124
Define two functions in a file named stat2.m, where the first function calls the second.
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end
function m = avg(x,n)
m = sum(x)/n;
end
Function avg is a local function. Local functions are only available to other functions within the same file.
[ave,stdev] = stat2(values)
ave =
47.3400
stdev =
29.4124
PART B
Using MATLAB’s Ordinary Differential Equation Solver
dy
=.. .
1. Write the ODE in the form dt
dx C∗−x
=
For Example 2.1, the equation is dt τ
2. Create a new M-file by selecting File>New>M-File
3. Write the ODE in M-file form using the following format:
function tempname = actualname(time, function, othervar)
tempname = …;
dy
=.. .
1. Write both ODE’s in the form dt
For our example, the equations are
dV 1
= ( w +w −w )
dt ρ 1 2
dx w1 w
= ( x1 −x )+ 2 ( x 2 −x )
dt Vρ Vρ
2. On paper, set up a vector that will contain all of the functions for which you want to
solve. This vector will have a corresponding first derivative vector that holds the
derivative functions from step 1.
[ y1 y 2 y 3 .. . ] [ dy 1
dt
dy 2
dt
dy 3
dt
.. . ]
For our example we will use the vectors:
[V x ] [ dV
dt
dx
dt ]
3. Create a new M-file by selecting File>New>M-File
4. Write the ODE’s in M-file form using the following format:
function tempname = actualname(time, vectorfunction, othervar)
tempname = [function1; function2; function3; …];
Notice that the vector which will hold our solutions for both V and x is called y. We
must therefore refer to V as y(1) and x as y(2) in the function statement.
5. Save the M-file.
6. In the command window, use the following commands to solve the ODE and store it in a
vector:
[t, y] = ode45(@actualname, [time range],[initial condition1; initial
condition2;…], [], other variables);
[t,y]=ode45(@Volconc,[0 25],[2;0.5],[],400,200,700,0.4,0.75,900);
The empty brackets in the above statement are necessary and tell MATLAB to skip the
parameters t and y in the M-file Volconc.
For more information on using the ODE solver, use MATLAB’s help system and select:
MATLAB>Mathematics>Differential Equations>Initial Value Problems for ODEs and DAEs
For more information on graphics and using plottools, use MATLAB’s help system and select:
MATLAB>Graphics>MATLAB Plotting Tools