Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS A MATLAB ASSIGNMENT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

ID: 3857991 • Letter: T

Question

THIS IS A MATLAB ASSIGNMENT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Function F.m

--------------------------------------------------------------------------------

ExampleEuler.m

function [ Time, Y_values ] = EulersMethod( y_initial, t_min, t_max, step_size )
%EulersMethod:: Computes Euler's method to numerically integrate a
%differential equation.
% This function assumes that you have a file "F.m" which defines your
% differential equation.
% The output of this function is a pair of vectors "Time" and "Y_values",
% the result of the Euler's method calculation.
% The input variables of this function are as follows:
% y_initial - The initial y-value which you want to integrate.
% t_min - The initial time from which you want to start integrating.
% t_max - The time at which you want to stop integrating.
% step_size - The step size you use in Euler's Method.


% First we calculate the number of steps we want to use.
number_of_steps = 1+ (t_max - t_min) / step_size;
% It is possible that to end exactly at t_max, we need a non-integer number
% of steps. If so, we round t_max up slightly.
if number_of_steps ~= ceil(number_of_steps )
disp('Choice of t_max does not produce an integer number of steps')
disp('Rounding t_max up slightly')
number_of_steps = ceil(number_of_steps );
end

% We create vectors "Time" and "Y_values" wherein we will store the values
% of t_k and y_k.
Time = 1:number_of_steps;
Y_values = 1:number_of_steps;

% We initialize some 'local' variable with the initial start time, and the initial
% y value. These variables will be updated inside of the loop below.
t = t_min;
y = y_initial;


for i = 1:number_of_steps;
% We store the 'local' values of t and y.
Time(i) = t;
Y_values(i) = y;
  
% We calculate the slope at the point (t,y).
slope = F(t,y);
  
% We update our values of t and y.
t = t + step_size;
y = y + step_size * slope;
end

2. Below is a slope field for the differential equation y'(1)-y * sin(t * /2). a) Modify the function F. m in MAIL AB so that it is defined as F(t,y) : y*sin(t * /2). Use this function in the command window to help you compute Euler's method for 6 steps starting at (yo, to) - (1,0) with step size 1. Plot your result on the graph above. 6 b) Modify the EzampleBuler.m file so that you numerically integrate the initial condition (30,4)-(1,0) until tirne 25. Do this for step sizes 0.5, 0.1 and 0.01. c) For each of the step sizes, what is the value Euler's method produces for y(25)? Include three significant digits. What pattern do you notice?

Explanation / Answer

For the Differential Equation

dy(t)/dt = y sin(pi * t/2)

The matlab code should be

syms y(t)
ode = diff(y,t) == y *sin(pi * t/2)
ode(t) = diff(y(t), t) == y(t) *sin(pi * t/2)
ySol(t) = dsolve(ode)

This should be written in F.m

Now ,