Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with MATLAB code Consider the IVP y'= 3y, y(0) = 2 for 0 t 5. The exact solution of the ODE is y

Need help with MATLAB code

Consider the IVP y'= 3y, y(0) = 2 for 0 t 5.

The exact solution of the ODE is y = 2e-3t

The goal of this exercise is to visualize how Eulers method is related to the slope field of the differential equation. In order to do this we will plot the direction field together with the approximations and the exact solution.

(a) To plot the slope field we will use the MATLAB commands meshgrid and quiver. Enter the following commands:

t = 0:.3:5; y = -20:2:25 ; % define grid of values in t and y directio [T,Y]=meshgrid(t,y); % creates 2d matrices of points in the ty-plane dT = ones(size(T)); % dt=1 for all points dY = -3*Y; % dy = -3*y; this is the ODE quiver(T,Y,dT,dY) % draw arrows (t,y)->(t+dt, t+dy) axis tight % adjust look hold on

After running these commands you should get the graph of the slope field.

(b) Use linspace to generate a vector of 100 t-values between 0 and 5. Evaluate the solution y at these t-values and plot it in black together with the direction field (use linewidth,2).

(c) Enter the function defining the ODE as anonymous. Use euler.m with N = 6 to determine the approximation to the solution. Plot the approximated points in red (use ro-,linewidth,2 as line-style in your plot), together with the exact solution and the direction field. Below is the euler.m function file.

.................................................................................................................................................................

 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 end 

..................................................................................................................................................................

Based on the slope field and the geometrical meaning of Eulers method explain why the approximations are so inaccurate for this particular value of the stepsize.

(d) Open a new figure by typing figure. Plot again the direction field but in a different window: t = 0:.3:5; y = -1:.4:2 ; Repeat part (b) and repeat part (c) but this time with N = 12.

Comment on the result from a geometrical point of view.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases And Python Programming MySQL MongoDB OOP And Tkinter

Authors: R. PANNEERSELVAM

1st Edition

9357011331, 978-9357011334

More Books

Students also viewed these Databases questions