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
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 ,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.