Answered step by step
Verified Expert Solution
Question
1 Approved Answer
matlab Here is a template for the exercise %% Exercise 1 % Read the instructions in your lab pdf file carefully! % Part (a) %
matlab
Here is a template for the exercise
%% Exercise 1 % Read the instructions in your lab pdf file carefully! % 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) = 3.25y. 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 your version of the lab. % Define vector t of time-values over the interval in your version of the lab, % to compute analytical solution vector. % Create vector of analytical solution values at corresponding t values. % NOTE: In your version of the lab you were given table in problem1(a), which % you need to fill out. This table has four different values of N - call them % Nsmall, Nmed, Nlarge and Nhuge (where, of course, Nsmall
and this for the function
function [t,y] = euler(f,tspan,y0,N) % Solves the IVP y' = f(t,y), y(t0) = y0 in the time interval tspan = [t0,tf] % using Euler's method with N time steps. % Input: % f = name of inline function or function M-file that evaluates the ODE % (if not an inline function, use: euler(@f,tspan,y0,N)) % For a system, the f must be given as column vector. % tspan = [t0, tf] where t0 = initial time value and tf = final time value % y0 = initial value of the dependent variable. If solving a system, % initial conditions must be given as a vector. % N = number of steps used. % Output: % t = vector of time values where the solution was computed % y = vector of computed solution values. m = length(y0); t0 = tspan(1); tf = tspan(2); h = (tf-t0)/N; % evaluate the time step size t = linspace(t0,tf,N+1); % create the vector of t values y = zeros(m,N+1); % allocate memory for the output y y(:,1) = y0'; % set initial condition for n=1:N y(:,n+1) = y(:,n) + h*f(t(n),y(:,n)); % implement Euler's method end t = t'; y = y'; % change t and y from row to column vectors endEXERCISES 1. (a) If you haven't already done so, enter the following commands f=0(t,y) y; t-linspace (0,2,100); y-exp (t): %define exact solution of the ODE [t100,y100]-euler (f, [0,2],1,100); %solve the ODE using Euler w/ 100 steps Determine the Euler's approximation for N 1000 and N 10000 and fill in the following table with the values of the approximations (yN(end)), errors (eN-y (end) -yN(end)) and ra- tios (eNprevious/eN) of consecutive errors at t-2. Some of the values have already been entered based on the computations we did above Include the table in your report, as well as the MATLAB commands used to find the entries ratio eNprev/eN approxima- error tion yN (end) 10 6.1917 1.1973 N/A 100 1000 10000 0.1444 8.2911 (b) Examine the last column. How does the ratio of consecutive errors relate to the number of steps used? Your answer to this question should confirm the fact that Euler's method is a first-order method. That is, every time the step size is decreased by a factor of k, the error is also reduced (approximately) by the same factor, kk (c) Recall the geometrical interpretation of Euler's method based on the tangent line. Using this geometrical interpretation, can you explain why the Euler approximations yv underestimate the solution y (that is, yN 0) in this particular example
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started