SAMPLE QUESTION:
Exercise 1: Consider the function
f (x,C)=
sin(C x)
Cx
(a) Create a vector x with 100 elements from -3*pi to 3*pi. Write f as an inline or anonymous function
and generate the vectors y1 = f(x,C1), y2 = f(x,C2) and y3 = f(x,C3), where C1 = 1, C2 = 2 and
C3 = 3. Make sure you suppress the output of x and y's vectors. Plot the function f (for the three
C's above), name the axis, give a title to the plot and include a legend to identify the plots. Add a
grid to the plot.
(b) Without using inline or anonymous functions write a function+function structure m-file that does
the same job as in part (a)
SAMPLE LAB WRITEUP:
MAT 275 MATLAB LAB 1 NAME: __________________________
LAB DAY and TIME:______________
Instructor: _______________________
Exercise 1
(a)
x = linspace(-3*pi,3*pi); % generating x vector - default value for number
% of pts linspace is 100
f= @(x,C) sin(C*x)./(C*x) % C will be just a constant, no need for ".*"
C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands
y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % supressing the y's
plot(x,y1,'b.-', x,y2,'ro-', x,y3,'ks-') % using different markers for
% black and white plots
xlabel('x'), ylabel('y') % labeling the axis
title('f(x,C) = sin(Cx)/(Cx)') % adding a title
legend('C = 1','C = 2','C = 3') % adding a legend
grid on
Command window output:
f =
@(x,C)sin(C*x)./(C*x)
C1 =
1
C2 =
2
C3 =
3
(b)
M-file of structure function+function
function ex1
x = linspace(-3*pi,3*pi); % generating x vector - default value for number
% of pts linspace is 100
C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands
y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % function f is defined below
plot(x,y1,'b.-', x,y2,'ro-', x,y3,'ks-') % using different markers for
% black and white plots
xlabel('x'), ylabel('y') % labeling the axis
title('f(x,C) = sin(Cx)/(Cx)') % adding a title
legend('C = 1','C = 2','C = 3') % adding a legend
grid on
end
function y = f(x,C)
y = sin(C*x)./(C*x);
end
Command window output:
C1 =
1
C2 =
2
C3 =
3
More instructions for the lab write-up:
1) You are not obligated to use the 'diary' function. It was presented only for you convenience. You
should be copying and pasting your code, plots, and results into some sort of "Word" type editor that
will allow you to import graphs and such. Make sure you always include the commands to generate
what is been asked and include the outputs (from command window and plots), unless the pr.
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
1. SAMPLE QUESTION:
Exercise 1: Consider the function
f (x,C)=
sin(C x)
Cx
(a) Create a vector x with 100 elements from -3*pi to 3*pi.
Write f as an inline or anonymous function
and generate the vectors y1 = f(x,C1), y2 = f(x,C2) and y3 =
f(x,C3), where C1 = 1, C2 = 2 and
C3 = 3. Make sure you suppress the output of x and y's
vectors. Plot the function f (for the three
C's above), name the axis, give a title to the plot and include
a legend to identify the plots. Add a
grid to the plot.
(b) Without using inline or anonymous functions write a
function+function structure m-file that does
the same job as in part (a)
SAMPLE LAB WRITEUP:
MAT 275 MATLAB LAB 1
NAME: __________________________
LAB DAY and TIME:______________
Instructor: _______________________
2. Exercise 1
(a)
x = linspace(-3*pi,3*pi); % generating x vector - default value
for number
% of pts linspace is 100
f= @(x,C) sin(C*x)./(C*x) % C will be just a constant, no need
for ".*"
C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands
y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % supressing the y's
plot(x,y1,'b.-', x,y2,'ro-', x,y3,'ks-') % using different markers
for
% black and white plots
xlabel('x'), ylabel('y') % labeling the axis
title('f(x,C) = sin(Cx)/(Cx)') % adding a title
legend('C = 1','C = 2','C = 3') % adding a legend
grid on
Command window output:
f =
@(x,C)sin(C*x)./(C*x)
C1 =
1
C2 =
2
C3 =
3. 3
(b)
M-file of structure function+function
function ex1
x = linspace(-3*pi,3*pi); % generating x vector - default value
for number
% of pts linspace is 100
C1 = 1, C2 = 2, C3 = 3 % Using commans to separate commands
y1 = f(x,C1); y2 = f(x,C2); y3 = f(x,C3); % function f is defined
below
plot(x,y1,'b.-', x,y2,'ro-', x,y3,'ks-') % using different markers
for
% black and white plots
xlabel('x'), ylabel('y') % labeling the axis
title('f(x,C) = sin(Cx)/(Cx)') % adding a title
legend('C = 1','C = 2','C = 3') % adding a legend
grid on
end
function y = f(x,C)
y = sin(C*x)./(C*x);
end
Command window output:
C1 =
1
C2 =
4. 2
C3 =
3
More instructions for the lab write-up:
1) You are not obligated to use the 'diary' function. It was
presented only for you convenience. You
should be copying and pasting your code, plots, and results
into some sort of "Word" type editor that
will allow you to import graphs and such. Make sure you
always include the commands to generate
what is been asked and include the outputs (from command
window and plots), unless the problem
says to suppress it.
2) Edit this document: there should be no code or MATLAB
commands that do not pertain to the
exercises you are presenting in your final submission. For
each exercise, only the relevant code that
performs the task should be included. Do not include error
messages. So once you have determined
either the command line instructions or the appropriate script
file that will perform the task you are
given for the exercise, you should only include that and the
associated output. Copy/paste these into
your final submission document followed by the output
(including plots) that these MATLAB
instructions generate.
3) All code, output and plots for an exercise are to be grouped
5. together. Do not put them in appendix, at
the end of the writeup, etc. In particular, put any mfiles you
write BEFORE you first call them.
Each exercise, as well as the part of the exercises, is to be
clearly demarked. Do not blend them all
together into some sort of composition style paper,
complimentary to this: do NOT double space.
You can have spacing that makes your lab report look nice,
but do not double space sections of text
as you would in a literature paper.
4) You can suppress much of the MATLAB output. If you need
to create a vector, "x = 0:0.1:10" for
example, for use, there is no need to include this as output in
your writeup. Just make sure you
include whatever result you are asked to show. Plots also do
not have to be a full, or even half page.
They just have to be large enough that the relevant structure
can be seen.
5) Before you put down any code, plots, etc. answer whatever
questions that the exercise asks first.
You will follow this with the results of your work that
support your answer.
MATLAB sessions: Laboratory 5
MAT 275 Laboratory 5
The Mass-Spring System
In this laboratory we will examine harmonic oscillation. We
will model the motion of a mass-spring
system with differential equations.
6. Our objectives are as follows:
1. Determine the effect of parameters on the solutions of
differential equations.
2. Determine the behavior of the mass-spring system from the
graph of the solution.
3. Determine the effect of the parameters on the behavior of the
mass-spring.
The primary MATLAB command used is the ode45 function.
Mass-Spring System without Damping
The motion of a mass suspended to a vertical spring can be
described as follows. When the spring is
not loaded it has length `0 (situation (a)). When a mass m is
attached to its lower end it has length `
(situation (b)). From the first principle of mechanics we then
obtain
mg︸︷︷︸
downward weight force
+ −k(` − `0)︸ ︷︷ ︸
upward tension force
= 0. (L5.1)
The term g measures the gravitational acceleration (g ' 9.8m/s2 '
32ft/s2). The quantity k is a spring
constant measuring its stiffness. We now pull downwards on the
mass by an amount y and let the mass
go (situation (c)). We expect the mass to oscillate around the
7. position y = 0. The second principle of
mechanics yields
mg︸︷︷︸
weight
+ −k(` + y − `0)︸ ︷︷ ︸
upward tension force
= m
d2(` + y)
dt2︸ ︷︷ ︸
acceleration of mass
, i.e., m
d2y
dt2
+ ky = 0 (L5.2)
using (L5.1). This ODE is second-order.
(a) (b) (c) (d)
y
`
`0
m
k
γ
9. y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,’b+-’,t,v,’ro-’); % time series for y and v
grid on;
%-----------------------------------------
function dYdt = f(t,Y,omega0)
y = Y(1); v = Y(2);
dYdt = [ v ; -omega0^2*y ];
Note that the parameter ω0 was passed as an argument to ode45
rather than set to its value ω0 = 3
directly in the function f. The advantage is that its value can
easily be changed in the driver part of the
program rather than in the function, for example when multiple
plots with different values of ω0 need
to be compared in a single MATLAB figure window.
0 1 2 3 4 5 6 7 8 9 10
-1.5
-1
-0.5
0
0.5
1
11. function.
(f) How does the size of the mass m and the stiffness k of the
spring affect the motion?
Support your answer first with a theoretical analysis on how ω0
– and therefore the period
of the oscillation – is related to m and k, and then graphically
by running LAB05ex1.m first
with m = 5 and k = 9 and then with m = 1 and k = 18. Include
the corresponding graphs.
2. The energy of the mass-spring system is given by the sum of
the potential energy and kinetic
energy. In absence of damping, the energy is conserved.
(a) Plot the quantity E = 1
2
mv2 + 1
2
ky2 as a function of time. What do you observe? (pay close
attention to the y-axis scale and, if necessary, use ylim to get a
better graph). Does the graph
confirm the fact that the energy is conserved?
(b) Show analytically that dE
dt
= 0.(Note that this proves that the energy is constant).
(c) Plot v vs y (phase plot). Does the curve ever get close to the
origin? Why or why not? What
does that mean for the mass-spring system?
Mass-Spring System with Damping
12. When the movement of the mass is damped due to viscous
effects (e.g., the mass moves in a cylinder
containing oil, situation (d)), an additional term proportional to
the velocity must be added. The
resulting equation becomes
m
d2y
dt2
+ c
dy
dt
+ ky = 0 or
d2y
dt2
+ 2p
dy
dt
+ ω20y = 0 (L5.4)
by setting p = c
2m
. The program LAB05ex1 is updated by modifying the function
f:
function LAB05ex1a
13. m = 1; % mass [kg]
k = 9; % spring constant [N/m]
c = 1; % friction coefficient [Ns/m]
omega0 = sqrt(k/m); p = c/(2*m);
y0 = 0.4; v0 = 0; % initial conditions
[t,Y] = ode45(@f,[0,10],[y0,v0],[],omega0,p); % solve for
0<t<10
y = Y(:,1); v = Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,’b+-’,t,v,’ro-’); % time series for y and v
grid on;
%-------------------------------------------
function dYdt = f(t,Y,omega0,p)
y = Y(1); v = Y(2);
dYdt = [ v ; ?? ]; % fill-in dv/dt
3. Fill in LAB05ex1a.m to reproduce Fig. L5b and then answer
the following questions.
(a) For what minimal time t1 will the mass-spring system
satisfy |y(t)| < 0.02 for all t > t1? You
can answer the question either by magnifying the MATLAB
figure using the magnify button
(include a graph that confirms your answer), or use the
15. m(i)=max(abs(y(i:end)));
end
i = find(m<0.02); i = i(1);
disp([’|y|<0.02 for t>t1 with ’ num2str(t(i-1)) ’<t1<’
num2str(t(i))])
(b) What is the maximum (in magnitude) velocity attained by
the mass, and when is it attained?
Answer by using the magnify button and include the
corresponding picture.
(c) How does the size of c affect the motion? To support your
answer, run the file LAB05ex1.m
for c = 2, c = 6 and c = 10. Include the corresponding graphs
with a title indicating the
value of c used.
(d) Determine analytically the smallest (critical) value of c such
that no oscillation appears in
the solution.
4. (a) Plot the quantity E = 1
2
mv2 + 1
2
ky2 as a function of time. What do you observe? Is the
energy conserved in this case?
(b) Show analytically that dE
dt
17. % add commands to LAB05ex1 to compute and plot E(t). Then
use ylim([~,~]) to change the yaxis limits.
% You don't need to include this code but at least one plot of
E(t) and a comment must be
% included!
%B) 2 pts | write out main steps here
% first differentiate E(t) with respect to t using the chain rule.
Then
% make substitutions using the expression for omega0 and using
the
% differential equation
%C) 3 pts | show plot and comment
%% EX 3 10 pts
%A) 3 pts | modify the system of equations in LAB05ex1a
% write the t value and either a) show correponding graph or b)
explain given matlab
% commands
%B) 2 pts | write t value and max |V| value; include figure
%note: velocity magnitude is like absolute value!
%C) 3 pts | include 3 figures here + comments.
% use title('text') to attach a title to the figure
%D) 2 pts | What needs to happen (in terms of the characteristic
equation)
%in order for there to be no oscillations? Impose a condition on
the
%characteristic equation to find the critical c value. Write out
main steps
%% EX4 10 pts
% A) 5 pts | include 1 figure and comment
%B) 2 pts
18. % again find dE/dt using the chain rule and make substitutions
based on the
% differential equation. You should reach an expression for
dE/dt which is
% in terms of y'
%C) 3 pts | include one figure and comment
Exercise (1):
function LAB05ex1
m = 1; % mass [kg]
k = 9; % spring constant [N/m]
omega0=sqrt(k/m);
y0=0.4; v0=0; % initial conditions
[t,Y]=ode45(@f,[0,10],[y0,v0],[],omega0); % solve for 0<t<10
y=Y(:,1); v=Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'b+-',t,v,'ro-'); % time series for y and v
grid on;
%------------------------------------------------------
function dYdt= f(t,Y,omega0)
y = Y(1); v= Y(2);
dYdt = [v; -omega0^2*y];
Exercise (1a):
function LAB05ex1a
m = 1; % mass [kg]
19. k = 9; % spring constant [N/m]
c = 1; % friction coefficient [Ns/m]
omega0 = sqrt(k/m); p = c/(2*m);
y0 = 0.4; v0 = 0; % initial conditions
[t,Y]=ode45(@f,[0,10],[y0,v0],[],omega0,p); % solve for
0<t<10
y=Y(:,1); v=Y(:,2); % retrieve y, v from Y
figure(1); plot(t,y,'b+-',t,v,'ro-'); % time series for y and v
grid on
%------------------------------------------------------
function dYdt= f(t,Y,omega0,p)
y = Y(1); v= Y(2);
dYdt = [v; ?? ]; % fill-in dv/dt