0% found this document useful (0 votes)
12 views

Modeling and Simulation LabEXPT 2

Modeling and Simulation LabEXPT 2 (6)

Uploaded by

Punit Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Modeling and Simulation LabEXPT 2

Modeling and Simulation LabEXPT 2 (6)

Uploaded by

Punit Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Modeling and Simulation Lab

Experiment No 2
Modelling and Simulation of a Mechanical System of one
variable using MATLAB
Part A

Introduction to functions in MATLAB


Introduction

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

function [y1,...,yN] = myfun(x1,...,xM)

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.

You can save your function:

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:

Any function in the file contains a nested function.

The function is a local function within a function file, and any local function in the file uses the end
keyword.

The function is a local function within a script file.

Examples

Function with One Output

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)

error('Input must be a vector')

end

y = sum(x)/length(x);

end

Call the function from the command line.

z = 1:99;

average(z)

ans =
50

Function in a Script File

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, .

Note: Including functions in scripts requires MATLAB® R2016b or later.

% Compute the value of the integrand at 2*pi/3.

x = 2*pi/3;

y = myIntegrand(x)

% Compute the area under the curve from 0 to pi.

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.

function [m,s] = stat(x)

n = length(x);

m = sum(x)/n;

s = sqrt(sum((x-m).^2/n));

end

Call the function from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];

[ave,stdev] = stat(values)

ave =

47.3400

stdev =

29.4124

Multiple Functions in a Function File

Define two functions in a file named stat2.m, where the first function calls the second.

function [m,s] = stat2(x)

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.

Call function stat2 from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];

[ave,stdev] = stat2(values)

ave =

47.3400

stdev =

29.4124

PART B
Using MATLAB’s Ordinary Differential Equation Solver

Solving one ODE

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 = …;

For Example 2.1 you might write:


function dxdt = conc(t,x,C,tau)
dxdt = (C-x)/tau;

4. Save the M-file.


5. 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 condition, [], other


variables);

For Example 2.1 part b) you would write:

[t, x] = ode45(@conc, [0 25], 0.5, [], 0.517, 3);


The empty brackets in the above statement are necessary and tell MATLAB to skip the
parameters t and x in the M-file conc.

6. Type the command plottools.


7. On the left hand side, click where it says “2D Axes”
8. On the right hand side, click the button “Add Data”
9. In the window that pops up select the appropriate vectors in the drop down menus which
hold the data you want to plot on the x and y axes. In our case we want to plot “t” as the
X Data Source and “x” as the Y Data Source. Then click OK.
10. You can add axis labels and a title by selecting Insert> and the appropriate label.

Solving two ODE’s simultaneously

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; …];

For our example you might write:


function dydt = Volconc(t,y,w1,w2,w,x1,x2,rho)
dydt = [(w1+w2-w)/rho; (w1*(x1-y(2))+w2*(x2-y(2)))/(y(1)*rho)];

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);

For our example you would write:

[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.

7. Type the command plottools.


8. On the left hand side, click where it says “2D Axes”
9. On the right hand side, click the button “Add Data”
10. In the window that pops up select the appropriate vectors in the drop down menus which
hold the data you want to plot on the x and y axes. In our case we want to plot “t” as the
X Data Source and “y” as the Y Data Source. Then click OK.
11. If you don’t want all the functions plotted on the same graph you can select the data you
don’t want on the graph, right click and select Cut. Then you can click “2D Axes” again
to get a new set of axes to plot the other functions.
12. You can add axis labels and a title by selecting Insert> and the appropriate label.

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

You might also like