Session 3 Question Answers Question 1 Enter the example code and test the program. Try changing the step size 0.01 and see if you get a better results. The code for the example %Using Euler's method to solve y'(t) = f(t,y) y(0) = y0 %1. Initialize the values % 1.1 set the initial value of y. y = 1; % 1.2 set the start and end value of t tstart = 0; tend = 1; % 1.3 set the step size (using dt for delta t) dt = 0.1; % 1.4 Initialise vectors to store the data to plot T = tstart:dt:tend; % Vector of values of t. n = length(t); % Find the total number of point Y = zeros(1,n); % Vector to store the values of y. %2. Repeat for each time step for k = 1:n %3. Store the current values for plotting later Y(k) = y; %4. Evaluate the current value of f(t,y) f = -y; %5. User Euler's formula to evaluate y for the next step y = y + dt*f; end %6. End repeat 1
%7. Plot the date plot(t,y,'r') xlabel('t') ylabel('y') grid %Plot the actual result as a comparison Y2 = exp(-t); hold on plot(t,y2,'b'); legend({'euler's Method','Actual value'}) dt = 0.1 dt = 0.01 2
Question 2 (a) Write a program that solves. dy dt = f (t, y) = cos(t) y (0)= y 0 =0 For 0 t 2π with a step size δ t = 0.01 (b) Compare the solution above with the analytic solution. y (t)=sin(t) Hint : To start, copy the previous program. Answer %Using Euler's method to solve y'(t) = f(t,y) y(0) = y0 %1. Initialize the values % 1.1 set the initial value of y. y = 0; % 1.2 set the start and end value of t tstart = 0; tend = 2*pi; % 1.3 set the step size (using dt for delta t) dt = 0.01; % 1.4 Initialise vectors to store the data to plot T = tstart:dt:tend; % Vector of values of t. n = length(t); % Find the total number of point Y = zeros(1,n); % Vector to store the values of y. %2. Repeat for each time step for k = 1:n %3. Store the current values for plotting later Y(k) = y; %4. Evaluate the current value of f(t,y) f = cos(t(k)); %5. User Euler's formula to evaluate y for the next step y = y + dt*f; end %6. End repeat 3
%7. Plot the date plot(t,y,'r') xlabel('t') ylabel('y') grid %Plot the actual result as a comparison Y2 = sin(t); hold on plot(t,y2,'b'); legend({'eulers Method','Actual value'}) 4
Question 3 (a) The velocity of the object dropped from hight is defined by the differential equation dv dt = g c m v2 v(0)=0 Where The acceleration due to gravity g = 9.81 m/s 2 The drag constant c = 0.3 Ns 2 /m 2 The mass m = 80 kg Write a program that uses Euler's method to calculate the velocity of the object. For 0 t 25 with a step size δ t = 0.01 Answer %Using Euler's method to solve V'(t) = g c/m v^2 v(0) = 0 % Model Parameters c = 0.3; % The drag coefficient Nm ^2s^-2 g = 9.81; % Acceleration due to gravity m/s^2 m = 80; % The mass of the object in kg %1. Initialize the values % 1.1 set the initial value of v. v = 0; % 1.2 set the start and end value of t tstart = 0; tend = 25; % 1.3 set the step size (using dt for delta t) dt = 0.01; % 1.4 Initialise vectors to store the data to plot T = tstart:dt:tend; % Vector of values of t. n = length(t); % Find the total number of point V = zeros(1,n); % Vector to store the values of v. 5
%2. Repeat for each time step for k = 1:n %3. Store the current values for plotting later V(k) = v; %4. Evaluate the current value of f(t,v) f = g - c * v.^2 /m; %5. User Euler's formula to evaluate v for the next step v = v + dt*f; end %6. End repeat %7. Plot the date plot(t,v,'r') title('velocity of a dropped object') xlabel('t') ylabel('v') grid 6
(b) Use the data cursor to measure the velocity after 25 seconds. The velocity should approach, but not be greater than the terminal velocity. dv is zero at when v = terminal velocity. dt Calculate the terminal velocity and compare it with v(25). dv dt = g c m v2 = 0 v 2 = m g c v = m g c v = 80 9.81 0.3 = 51.1468 m/s Where v is the terminal velocity. The acceleration due to gravity g = 9.81 m/s 2 The drag constant c = 0.3 Ns 2 /m 2 The mass m = 80 kg On the graph, the data cursor, shows that v(25) = 51.14 m/s 7
Question 4 Use the above top down design to write a program to plot the velocity and hight of the ball. The acceleration is a constant a = -9.81 m/s 2. The initial value of v = 25 m/s. The initial value of y = 0. Plot both v and y in separate graphs on the same figure. Each graph should be labelled. The height should be an inverted parabola. The ball should hit the ground before 6 seconds. Answer %Using Euler,s Method to Plot a Ball Thrown Upwards. %1. Initialize the values % Model Parameters a = -9.81; % Acceleration m/s^2 % 1.1 set the initial value of v and y. v = 25; % The initial velocity m/s y = 0; % The initial height meters % 1.2 set the start and end value of t tstart = 0; tend = 6; % 1.3 set the step size (using dt for delta t) dt = 0.01; % 1.4 Initialise vectors to store the data to plot T = tstart:dt:tend; % Vector of values of t. n = length(t); % Find the total number of point V = zeros(1,n); % Vector to store the values of v. Y = zeros(1,n); % Vector to store the values of y. 8
%2. Repeat for each time step for k = 1:n %3. Store the current values of both y and v for plotting later V(k) = v; Y(k) = y; %4. Use Euler's method to find the position at the next step y = y + dt * v; %5. Use Euler's method to find the velocity at the next step v = v + dt * a; end %6. End of repeat %7. Plot the date % height Graph subplot(2,1,1) plot(t,y,'b') title ('Height') xlabel('t') ylabel('y') grid % Velocity Graph subplot(2,1,2) plot(t,v,'r') title ('Velocity') xlabel('t') ylabel('v') grid %Top Graph % Bottom Graph 9
10