Question
incsearch: function xb = incsearch(func,xmin,xmax,ns) % incsearch: incremental search root locator % xb = incsearch(func,xmin,xmax,ns): % finds brackets of x that contain sign changes %
incsearch:
function xb = incsearch(func,xmin,xmax,ns) % incsearch: incremental search root locator % xb = incsearch(func,xmin,xmax,ns): % finds brackets of x that contain sign changes % of a function on an interval % input: % func = name of function % xmin, xmax = endpoints of interval % ns = number of subintervals (default = 50) % output: % xb(k,1) is the lower bound of the kth sign change % xb(k,2) is the upper bound of the kth sign change % If no brackets found, xb = []. if nargin
bisect:
function [root, ea, iter]=bisect(func, xl, xu, es, maxit, varargin)
% bisect: root location zeroes
% [root,ea,iter]=bisect(func,xl,xu,es,maxit,p1,p2,...):
% uses bisection method to find the root of func
% input:
% func = name of function
% xl, xu = lower and upper guesses
% es = desired relative error (default = 0.0001%)
% maxit = maximum allowable iterations (default = 50)
% p1,p2,... = additional parameters used by func
% output:
% root = real root
% ea = approximate relative error (%)
% iter = number of iterations
if nargin
test = func(xl,varargin{:})*func(xu,varargin{:});
if test>0,error('no sign change'),end
if nargin
if nargin
iter = 0; xr = xl;
while(1)
xrold=xr;
xr=(xl+xu)/2;
iter=iter+1;
if xr ~=0, ea=abs((xr-xrold)/xr)*100;
test = func(xl,varargin{:})*func(xr,varargin{:});
if test
xu=xr;
elseif test>0
xl=xr;
end
else
ea=0;
end
if ea =maxit, break, end
end
root = xr;
The displacement of a damped spring mass system can be represented with the following function: f(t)=156etcos(12t) Where t is time in seconds and f(t) is the displacement of the mass in meters, and 0t5. a) Plot the above function over the range of interest to understand the motion of the system. Use the fplot function and label the x and y axes appropriately. Have MATLAB output this plot to Figure 1 using the figure command. If you do not know how to do this, type 'help figure' into the command window to pull up the help entry. (2 marks) b) Use the incremental search function (incsearch.m) provided on OWL to get brackets of time intervals when the spring mass system passes through f(t)=6m. Have incsearch.m write the root brackets to an array named brackets. (6 marks) Note: you will need to turn this into a 'roots' problem by manipulating the equation so that we are interested in the case where f(t)=0, essentially you want the function to cross the x axis at the value you are interested in above. DO NOT MODIFY incsearch. m ! c) Use the bisect function (bisect.m) provided to you on OWL, or any appropriate method you like, to solve for the actual time(s) within those intervals when the spring mass system passes through f(t)=6m with an allowable error of 0.75%. Write the results to an array named roots. (6 marks) Hint: This may be best performed inside a loop which will pass all brackets stored in the brackets array into bisect.m (or any other method you like) to find all roots. d) Now that you know the times when the mass hits f(t)=6m, highlight when this occurs on the original unmodified function using red circles. Leave Figure 1 unchanged and plot this in Figure 2 using the f gure command. If you do this correctly, your Figure 2 will contain the original function with red circles whenever the system crosses f(t)=6m. (2 mark) e) Now, turn your logic that you developed in parts (b) and (c) into a function that returns the time(s) at which a mass crosses a position of interest for any anonymous function that is passed in. (4 marks) The function call should be: function [t]=dispTime(ft,tl,tu,disp) Where ft is an anonymous function, tl and tu are the lower and upper limits of time over which the function should be checked, and disp is the displacement you want the function to return the time(s) at which the system crosses that value. Again, do not change either the given incsearch.m or bisect.m functions. Please save the function as an 'embedded function' at the bottom of your script and call it within your script to test it using your original function from part (a). Use all the same parameters as described in the rest of the problem. You can leave your parts (a) through (d) at the top of your file. You should get the same answer as you did in part (c) if you implemented your logic in the function correctly. Hint: You will need to modify an anonymous function inside your function to make it a 'roots' problem; this can be accomplished using simple syntax as shown in the example below: func1=a(x)x func2 =a(x)func1(x)+6 The result of the second line is: func2 =a(x)x+6 which has modified our original function. This will permit you to adjust the function passed in to make it a 'roots' problem and solve for when the mass crosses the displacement passed in
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started