# Differential Equations using Matlab, lab3

%% Lab 3 – Your Name – MAT 275 Lab

% Introduction to Numerical Methods for Solving ODEs

%% Exercise 1

% Part (a)

% NOTE: We often define the right-hand side of an ODE as some function, say

% f, in terms of the input and output variables in the ODE. For example,

% dy/dt = f(t,y) = 2y. Although the ODE does not explicitly depend on t, it does

% so implicitly since y is a function of t. Therefore, t is an input

% variable of f.

% Define ODE function f for ODE dy/dt = f(t,y) = 2y.

% Define vector t of time-values over the interval [0,0.5] to compute

% analytical solution vector.

% Create vector of analytical solution values at corresponding t values.

% NOTE: For the following steps, please use notation consistent with the

% protocol–e.g., use t5 to represent the time vector produced from Euler’s

% method using 5 timesteps and use y500 to represent the numerical solution

% vector produced from Euler’s method using 500 timesteps. In the following

% comments, I refer to “Euler’s method” as “forward Euler’s method,” which

% is more precise since there is also a backward Euler’s method. I use IVP

% as an abbreviation for initial-value problem, which for our purposes here

% means an ODE with associated initial condition.

% Solve IVP numerically using forward Euler’s method with 5 timesteps.

% Solve IVP numerically using forward Euler’s method with 50 timesteps.

% Solve IVP numerically using forward Euler’s method with 500 timesteps.

% Solve IVP numerically using forward Euler’s method with 5000 timesteps.

% Compute numerical solution error at t=0.5 for forward Euler with 5

% timesteps.

% Here, we define error as |analytical solution value – numerical solution

% value|.

% Compute numerical solution error at t=0.5 for forward Euler with 50

% timesteps.

% Compute numerical solution error at t=0.5 for forward Euler with 500

% timesteps.

% Compute numerical solution error at t=0.5 for forward Euler with 5000

% timesteps.

% Compute ratio of errors between N=5 and N=50.

% Compute ratio of errors between N=50 and N=500.

% Compute ratio of errors between N=500 and N=5000.

% NOTE: To complete the following table, simply run this section after

% you’ve finished the steps above and copy and paste your errors and ratios

% into the follwing display commands. Make sure you re-align each column

% after entering in values so that it looks pretty.

% Display table of errors (and ratios of consecutive errors) for the

% numerical solution at t=0.5 (the last element in the solution vector).

disp(‘———————————————-‘)

disp(‘| N | approximation | error | ratio |’)

disp(‘|——|—————–|———-|——–|’)

disp(‘| 5 | 7.4650 | 0.6899 | N/A |’)

disp(‘| 50 | | 0.0801 | 8.6148 |’)

disp(‘| 500 | | | |’)

disp(‘| 5000 | | | |’)

disp(‘———————————————-‘)

%%

% Part (b). Your answer here. Notice that by not adding a title to this

% section and skipping to the next line to start our comment, this comment

% will display in black (not green) when we publish it.

%%

% Here, we can add another paragraph to our essay answer for part (b) by

% once again partitioning off a new section with no title and beginning our

% comment on the second line of the section.

%%

% Part (c). Your answer here.

%% Exercise 2

% Part (a)

% Plot slopefield for dy/dt = -2y.

%%

% Part (b)

% Define vector t of time-values over the interval [0,10] to define

% analytical soution vector.

% Create vector of analytical solution values at corresponding t values.

% NOTE: The “hold on” command in the segment of code to plot the slope

% field indicates to MATLAB to add future plots to the same window unless

% otherwise specified using the “hold off” command.

% Plot analytical solution vector with slopefield from (a).

%%

% Part (c)

% NOTE: Recall we can define the right-hand side of an ODE as some function

% f. In the case of a one-dimensional autonomous ODE, we may define f such

% that dy/dt = f(t,y).

% Define ODE function.

% Compute numerical solution to IVP with 8 timesteps using forward Euler.

% Plot numerical solution with analytical solution from (b) and slopefield

% from (a) using circles to distinguish between the approximated data

% (i.e., the numerical solution values) and actual (analytical) solution.

hold off; % end plotting in this figure window

%%

% Your response to the essay question for part (c) goes here. Please do not

% answer the question with “the numerical solution is inaccurate because

% the stepsize is too big” or “because there are not enough points in the

% numerical solution.” What is it about this particular solution that makes

% forward Euler so inaccurate for this stepsize? Think about

%%

% Part (d)

% Define new grid of t and y values at which to plot vectors for slope

% field.

% Plot slope field for dy/dt = -2y corresponding to the new grid.

% Define vector t of time-values over the interval [??what??] to define

% analytical soution vector.

% Create vector of analytical solution values at corresponding t values.

% Define ODE function.

% Compute numerical solution to IVP with 16 timesteps using forward Euler.

%%

% Your essay answer goes here.

%% Exercise 3

% Display contents of impeuler M-file.

type ‘impeuler.m’

% NOTE: Make sure you document your code in impeuler.m appropriately for

% full credit.

% Define ODE function for dy/dt = f(t,y) = 2y.

% Compute numerical solution to IVP with 5 timesteps using “improved

% Euler.”

%%

% How does your output compare to that in the protocol?

%% Exercise 4

% NOTE: Here, we repeat all the steps from exercise 1, except using

% “improved Euler” instead of forward Euler (or regular Euler). Make sure

% to document your code appropriately.

%% Exercise 5

% NOTE: Here, we repeat all the steps from exercise 2, except using

% improved Euler rather than forward Euler. Remember code doc.

%%

## Needs help with similar assignment?

We are available 24x7 to deliver the best services and assignment ready within 3-4 hours? Order a custom-written, plagiarism-free paper

Get Answer Over WhatsApp Order Paper Now