COMPUTER SCIENCE ASSIGNMENT #1 Solution

$35.00 $24.00

  This is a really large class and the logistics of grading assignments are challenging. Me and the markers require your help in making this process go smoothly. Please ensure that your assignments conform to the following requirements – any violation will result in getting a zero for the particular assignment.   All assignments should…

You’ll get a: . zip file solution

 

 
Categorys:

Description

5/5 – (3 votes)

 

This is a really large class and the logistics of grading assignments are challenging. Me and the markers require your help in making this process go smoothly. Please ensure that your assignments conform to the following requirements – any violation will result in getting a zero for the particular assignment.

 

All assignments should be submitted electronically through the ConneX course website and shoud be SINGLE PDF FILES. No other formats will be accepted.. Handwrit-ten answers are ok but they will need to be scanned and merged into a single pdf le together with any code examples and associated plots.

 

The assignment number, student name and student number should be clearly visible on the top of every page of your assignment submission.

 

PLEASE DO NOT COPY THE ASSIGNMENT DESCRIPTION IN YOUR SUBMISSION

 

The asnwers to the questions should be in the same order as in the assignment speci-cation.

 

Some of the questions of the assignments are recycled from previous years but typically with small changes in either the description or the numbers. Any submission that contains numbers from previous years in any questions will be immediately graded with zero.

 

Any assignment related email questions should have a subject line of the form CSC349A Assignment X, where X is the number of the corresponding assignment.

 

A MATLAB function M- le for Euler’s method (as described in class and on pages 17-19 of the textbook) for solving the di erential equation

 

dvdt = g           mcv

 

(this is (1.9) on page 14) is given below.       As discussed in class, this numerical method

is obtained by approximating                                                              at time ti by v(ti+1)  v(ti) , which results in the computed

ti+1    ti

approximation

h           c            i

v(tt+1) = v(ti) +  g               mv(ti) (ti+1    ti):

 

To create a MATLAB function M- le, either type edit in the Command Window or select HOME ! New ! Script or select HOME ! New ! Function (the latter gives you a template for creating a function).

 

Each of these options will open a new window (an Editor window) in which to type in the MATLAB statements for Euler’s method. Enter the following. Statements starting with % are comments, documenting the MATLAB code.

 

function Euler(m,c,g,t0,v0,tn,n)

 

  • print headings and initial conditions

 

fprintf(‘values of t                        approximations v(t)\n’)

 

fprintf(‘%8.3f’,t0),fprintf(‘%19.4f\n’,v0)

 

  • compute step size h h=(tn-t0)/n;

 

  • set t,v to the initial values t=t0;

 

v=v0;

 

  • compute v(t) over n time steps using Euler’s method for i=1:n

 

v=v+(g-c/m*v)*h; t=t+h; fprintf(‘%8.3f’,t),fprintf(‘%19.4f\n’,v)

 

end

 

To save your M- le, select

 

EDITOR ! Save ! Save As…

At the top of this window, it should say

 

File name: Euler.m

 

Save as type: MATLAB          les (*.m)

 

Select

 

Save

 

to save your   le, and close the Editor window.

 

In order to use the above function, you must specify values for the 7 local parameters in the function Euler:

 

 

 

 

m is the mass of the falling object

 

c is the drag coe   cient

 

g is the gravity constant

 

t0 is the initial time, v0 is the initial velocity

 

tn is the nal time at which the velocity is to be computed n is the number of time steps into which [t0; tn] is divided

 

 

Thus, in the function Euler, the step size h = (tn t0)=n is computed, and Euler’s method is used to compute an approximation to the solution v(t) of the di erential equation at the n points (values of time)

 

t0 + h; t0 + 2h; t0 + 3h; t0 + 4h; : : : ; t0 + nh = tn:

 

For example, in order to use Euler to solve the problem given in Example 1.2 on page 17 and to save your results in a le for printing, you could enter the following (in the MATLAB Command Window):

 

diary     lename

 

Euler ( 68.1 , 12.5 , 9.81, 0 , 0 , 12 , 6 )

 

 

fthe desired results should appear hereg

 

diary o

 

 

There are three questions and each subquestion (for example Q1b) is worth 2 points.

 

The total number of points for this assignment is 20.

 

Question #1. – 8 marks

 

 

  • Create a working copy of the MATLAB function Euler in your installation of MATLAB. DELIVERABLES: A copy of the M-FILE in your pdf.

 

  • Use Euler to solve the di erential equation using m = 86:2, c = 12:5 and initial

 

conditions v(0) = 0 on the time interval [0; 12] using 15 time steps and g = 9:81.

 

DELIVERABLES: Your answer should include the function call to Euler and the resulting output.

 

  • Use Euler for a falling parachutist with the same parameters as Q1b but with a gravi-tational constant of 3.71 (as would be the case if the parachutist was falling on Mars).

 

DELIVERABLES: Your answer should include the function call to Euler and the resulting output.

 

 

 

 

  • Use MATLAB to compute the relative error j”tj in the computed approximation at time t = 12, using the constants from Q1b. To do this, use the true (exact) solution:

 

  gm (1  e ct  
v(t) =   m ) (1)
c

 

DELIVERABLES: A copy of the commands and output.

 

Note that in MATLAB ex is computed as exp(x) and jxj as abs(x).

 

Question #2 – 6 Marks.

 

In our mathematical model of a falling parachutist, instead of assuming that air resistance is linearly proportional to velocity (that is, FU = cv), you might choose to model the upward force on the parachutist as a second-order relationship,

 

FU =     kv2

 

where k is a second-order drag coe  cient. This leads to the following di erential equation

 

dvdt = g           mkv2

 

  • Modify the MATLAB function Euler in Question 1 so that it will use Eulers method to solve this di erential equation. Use the function header

 

Euler2(m , k , g, t0 , v0 , tn , n)

 

DELIVERABLES: A copy of the M-FILE in your pdf.

 

  • Use Euler2 to compute a numerical approximation to the above di erential equation using m = 73:5, k = 0:234 and initial condition v(0) = 0 on the time interval [0; 18] using 72 time steps.

 

DELIVERABLES: The function call to Euler2 and the resulting output.

 

  • Use the fact the exact analytic solution of this problem is
v(t) = r       tanh r       t!
        m
  k
      gm     gk
                     

 

 

 

to compute (either in MATLAB or using your calculator) the relative error in the computed solution at t = 18.

 

Question #3 – 4 Marks

 

The function e x can be approximated by its McLaurin series expansion as follows (note the alternating + and ):

 

e x    1  x + x2 x3   xn
        + : : :  
2! 3! n!

 

 

4Alternatively, note that e x = e1x . Thus, e x can also be apporximated by 1 over the McLaurin series expansion of ex. That is,

 

 

e x     1      
             
1 + x + x2 + x3 + : : : + xn  
    n!
2! 3!  

 

Approximate e 2 using both approaches above for n = 1; 2; 3; 4 and 5. Note, n is the degree of the polynomial not the number of terms. So here you use 2 terms, then 3 terms,

 

…, and nally 6 terms. Compare each approximation to the true value of e 2 = 0:135335:::, using the true relative error. What conclusions can you make about the two approaches?

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

5