lab_3_template
pdf
keyboard_arrow_up
School
Arizona State University *
*We aren’t endorsed by this school
Course
275
Subject
Mathematics
Date
Apr 3, 2024
Type
Pages
15
Uploaded by ProfessorWater27041
LAB 3 - Your Name - MAT 275
Exercise 1
Read the instructions in your lab pdf file carefully!
Part (a)
Define ODE function f
for your version of the lab.
f =@( t , y ) 0.5* y ; Define vector t
of time-values over the interval in your version of the lab, to compute analytical solution vector.
t = linspace (0 ,1.75 ,100); Create vector of analytical solution values at corresponding t values.
y = exp (0.5* t ); Solve IVP numerically using forward Euler's method with Nsmall timesteps (use variable names as instructed).
[ t7 , y7 ]= euler (f ,[0 ,1.75] ,1 ,7);
Solve IVP numerically using forward Euler's method with Nmed timesteps (use variable names as instructed).
[ t70 , y70 ]= euler (f ,[0 ,1.75] ,1 ,70);
Solve IVP numerically using forward Euler's method with Nlarge timesteps (use variable names as instructed).
[ t700 , y700 ]= euler (f ,[0 ,1.75] ,1 ,700);
Solve IVP numerically using forward Euler's method with Nhuge timesteps (use variable names as instructed).
[ t7000 , y7000 ]= euler (f ,[0 ,1.75] ,1 ,7000);
In the following steps, we define error as exact - numerical solution value at the last time step
eN=y(end)
eN = 2.3989
Compute numerical solution error at the last time step for forward Euler with Nsmall timesteps (use variable names as instructed).
eN7=eN-y7(end)
eN7 = 0.1182
1
Compute numerical solution error at the last time step for forward Euler with Nmed timesteps (use variable names as instructed).
eN70=eN-y70(end)
eN70 = 0.0130
Compute numerical solution error at the last time step for forward Euler with Nlarge timesteps (use variable names as instructed).
eN700=eN-y700(end)
eN700 = 0.0013
Compute numerical solution error at the last time step for forward Euler with Nhuge timesteps (use variable names as instructed).
eN7000=eN-y7000(end)
eN7000 = 1.3117e-04
Compute ratio of errors between N=Nsmall and N=Nmed. r70=eN7/eN70
r70 = 9.1079
Compute ratio of errors between N=Nmed and N=Nlarge.
r700=eN70/eN700
r700 = 9.9015
Compute ratio of errors between N=Nlarge and N=Nhuge. r7000=eN700/eN7000
r7000 = 9.9901
FILL OUT THE TABLE BELOW Display the table of errors and ratios of consecutive errors for the numerical solution at t=tfina
l (the last element in the solution vector). REPLACE
words "Nsmall", "Nmed", "Nlarge" and "Nhuge","Nsmall", "yNmed", "yNlarge" and "yNhuge" , "eNsmall",..."ratio1", ... by the appropriate numbers of steps you were given in your version of the lab and by the appropriate values you computed above. Also, note that in your version of the lab you already have some values entered in the table, to help you check your code is correct. You need to fill out all the cells in the table!
N ={7;70;700;7000};
Approximation={y7(end);y70(end);y700(end);y7000(end)};
error={eN7;eN70;eN700;eN7000};
ratio ={
'N/A'
; r70;r700;r7000};
2
table(N,Approximation,error,ratio)
ans = 4×4 table
N
Approximation
error
ratio
1
7
2.2807
0.1182
'N/A'
2
70
2.3859
0.0130
9.1079
3
700
2.3976
0.0013
9.9015
4
7000
2.3987
1.3117e-04
9.9901
Part (b)
The ratio of error approaches 10, which is the factor by which N is being multiplied by.
Part (c)
since the function is concave up and the tangents will always remained positioned below the curve, the approximations will underestimate.
Exercise 2
Read the instructions in your lab pdf file carefully!
Part (a)
Plot slopefield for the new ODE using the given commands. t = 0:0.3:5; y = -9:1.7:8; % define a grid in t & y directions
[T , Y ] = meshgrid (t , y ); % create 2 d matrices of points in ty - plane
dT = ones ( size ( T )); % dt =1 for all points
dY = -3.1* Y ; % dy = -3.1* y ; this is the ODE
quiver (T ,Y , dT , dY ) % draw arrows (t , y ) - >( t + dt , t + dy )
axis tight % adjust look
hold on
Part (b)
Define vector t of time-values over the given interval to define analytical solution vector.
t = linspace(0,5,100);
Create vector of analytical solution values at corresponding t values.
y=exp(-3.1*t);
Plot analytical solution vector with slopefield from (a). 3
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
plot(t,y,
'k-'
,
'LineWidth'
,2)
Part (c)
Define ODE function.
f = @(t,y) -3.1*y;
Compute numerical solution to IVP with N timesteps using forward Euler.
N = 7;
[tN,yN]= euler(f, [0,5] ,1,N)
tN = 8×1
0
0.7143
1.4286
2.1429
2.8571
3.5714
4.2857
5.0000
yN = 8×1
1.0000
-1.2143
1.4745
-1.7905
2.1741
-2.6400
3.2057
-3.8927
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.
plot(tN,yN, 'ro-'
,
'LineWidth'
,2)
hold off
; %end plotting in this figure window
4
Your response to the essay question for part (c) goes here. because the slope is increasing at each point, the error is as well, causing increasingly inaccurate approximations.
Part (d)
Define new grid of t and y values at which to plot vectors for slope field.
figure
t = 0:0.3:5;
y = -1.2:0.4:2.6;
Plot slope field corresponding to the new grid.
[T , Y ] = meshgrid (t , y ); % create 2 d matrices of points in ty - plane
dT = ones ( size ( T )); % dt =1 for all points
dY = -3.1* Y ; % dy = -3.1* y ; this is the ODE
quiver (T ,Y , dT , dY ) % draw arrows (t , y ) - >( t + dt , t + dy )
axis tight % adjust look
hold on
Define vector t of time-values over the given interval to define analytical soution vector.
t = linspace(0,5,100);
5
Create vector of analytical solution values at corresponding t values.
y=exp(-3.1*t);
Define ODE function.
f=@(t,y) -3.1*y;
Compute numerical solution to IVP with the given timesteps using forward Euler. N=14;
[tN,yN]= euler(f, [0 ,5] ,1,N);
plot numerical solution together with slope field and analytical solution
plot(t,y,
'k-'
,
'LineWidth'
,2)
plot(tN,yN,
'ro-'
,
'LineWidth'
,2)
hold off
;
Your essay answer goes here.
Since the step size is now much smaller, the slope follows the solution curve and approximations are more accurate.
6
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Exercise 3
Read the instructions in your lab pdf file carefully!
Display contents of impeuler M-file.
type 'impeuler.m'
function [t,y] = impeuler(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
f1 = f(t(n),y(:,n));
f2 = f(t(n+1),y(:,n)+h*f1);
y(:,n+1) = y(:,n) + h/2*(f1+f2); % implement Euler's method
end
t = t'; y = y'; % change t and y from row to column vectors
end
Define ODE function. f=@(t, y) 0.5*y;
Compute numerical solution to IVP with Nsmall timesteps using "improved Euler."
[ t7 , y7 ] = impeuler (f ,[0 ,1.75] ,1 ,7);
[ t7 , y7 ]
ans = 8×2
0 1.0000
0.2500 1.1328
0.5000 1.2833
0.7500 1.4537
1.0000 1.6468
1.2500 1.8655
1.5000 2.1132
1.7500 2.3939
How does your output compare to that in the protocol? They should be the same.
7
Exercise 4
Read the instructions in your lab pdf file carefully!
Part (a)
Define ODE function f
for your version of the lab.
f=@(t, y) 0.5*y;
Define vector t
of time-values over the interval in your version of the lab, to compute analytical solution vector.
t = linspace(0,1.75,100);
Create vector of analytical solution values at corresponding t values.
y = exp(0.5*t);
Solve IVP numerically using improved Euler's method with Nsmall timesteps (use variable names as instructed).
[ t7 , y7 ] = impeuler (f ,[0 ,1.75] ,1 ,7)
t7 = 8×1
0
0.2500
0.5000
0.7500
1.0000
1.2500
1.5000
1.7500
y7 = 8×1
1.0000
1.1328
1.2833
1.4537
1.6468
1.8655
2.1132
2.3939
Solve IVP numerically using improved Euler's method with Nmed timesteps (use variable names as instructed).
[ t70 , y70 ] = impeuler (f ,[0 ,1.75] ,1 ,70)
t70 = 71×1
0
0.0250
0.0500
0.0750
0.1000
8
0.1250
0.1500
0.1750
0.2000
0.2250
y70 = 71×1
1.0000
1.0126
1.0253
1.0382
1.0513
1.0645
1.0779
1.0914
1.1052
1.1191
Solve IVP numerically using improved Euler's method with Nlarge timesteps (use variable names as instructed).
[ t700 , y700 ] = impeuler (f ,[0 ,1.75] ,1 ,700)
t700 = 701×1
0
0.0025
0.0050
0.0075
0.0100
0.0125
0.0150
0.0175
0.0200
0.0225
y700 = 701×1
1.0000
1.0013
1.0025
1.0038
1.0050
1.0063
1.0075
1.0088
1.0101
1.0113
Solve IVP numerically using improved Euler's method with Nhuge timesteps (use variable names as instructed).
[ t7000 , y7000 ] = impeuler (f ,[0 ,1.75] ,1 ,7000)
t7000 = 7001×1
0
0.0003
0.0005
0.0008
0.0010
9
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
0.0013
0.0015
0.0018
0.0020
0.0022
y7000 = 7001×1
1.0000
1.0001
1.0003
1.0004
1.0005
1.0006
1.0008
1.0009
1.0010
1.0011
In the following steps, we define error as exact - numerical solution value at the last time step
eN=y(end);
Compute numerical solution error at the last time step for improved Euler with Nsmall timesteps (use variable names as instructed).
eN7=eN-y7(end);
Compute numerical solution error at the last time step for improved Euler with Nmed timesteps (use variable names as instructed).
eN70=eN-y70(end);
Compute numerical solution error at the last time step for improved Euler with Nlarge timesteps (use variable names as instructed).
eN700=eN-y700(end);
Compute numerical solution error at the last time step for improved Euler with Nhuge timesteps (use variable names as instructed).
eN7000=eN-y7000(end);
Compute ratio of errors between N=Nsmall and N=Nmed. r70=eN7/eN70
r70 = 91.8548
Compute ratio of errors between N=Nmed and N=Nlarge.
r700=eN70/eN700
10
r700 = 99.1590
Compute ratio of errors between N=Nlarge and N=Nhuge. r7000=eN700/eN7000
r7000 = 99.9156
FILL OUT THE TABLE BELOW Display the table of errors and ratios of consecutive errors for the numerical solution at t=tfina
l (the last element in the solution vector). REPLACE
words "Nsmall", "Nmed", "Nlarge" and "Nhuge","Nsmall", "yNmed", "yNlarge" and "yNhuge" , "eNsmall",..."ratio1", ... by the appropriate numbers of steps you were given in your version of the lab and by the appropriate values you computed above. N ={7;70;700;7000};
Approximation={y7(end);y70(end);y700(end);y7000(end)};
error={eN7;eN70;eN700;eN7000};
ratio ={
'N/A'
; r70;r700;r7000};
table(N,Approximation,error,ratio)
ans = 4×4 table
N
Approximation
error
ratio
1
7
2.3939
0.0050
'N/A'
2
70
2.3988
5.4151e-05
91.8548
3
700
2.3989
5.4611e-07
99.1590
4
7000
2.3989
5.4657e-09
99.9156
Part (b)
now the ratio of errors approaches 100, and the error is decreasing by a factor of 100.
Exercise 5
Read the instructions in your lab pdf file carefully!
Part (a)
Plot slopefield for the new ODE using the given commands. t = 0:0.3:5; y = -9:1.7:8; % define a grid in t & y directions
[T , Y ] = meshgrid (t , y ); % create 2 d matrices of points in ty - plane
dT = ones ( size ( T )); % dt =1 for all points
dY = -3.1* Y ; % dy = -3.1* y ; this is the ODE
quiver (T ,Y , dT , dY ) % draw arrows (t , y ) - >( t + dt , t + dy )
axis tight % adjust look
hold on
11
Part (b)
Define vector t of time-values over the given interval to define analytical solution vector.
t = linspace(0,5,100);
Create vector of analytical solution values at corresponding t values.
y=exp(-3.1*t);
Plot analytical solution vector with slopefield from (a). plot(t,y,
'k-'
,
'LineWidth'
,2)
Part (c)
Define ODE function.
f = @(t,y) -3.1*y;
Compute numerical solution to IVP with N timesteps using improved Euler.
N = 7
N = 7
[tN,yN]= impeuler(f, [0 ,5] ,1,N);
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.
plot(tN,yN,
'ro-'
,
'LineWidth'
,2)
hold off
; %end plotting in this figure window
12
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Your response to the essay question for part (c) goes here. similar to the first euler method, the graph with this large of a step size is in accurate, but it does not fluctuate above and below the solution curve. Instead, it increases above and away from it.
Part (d)
Define new grid of t and y values at which to plot vectors for slope field.
figure
t = 0:0.3:5;
y = -1.2:0.4:2.6;
Plot slope field corresponding to the new grid.
t = 0:0.3:5; y = -9:1.7:8; % define a grid in t & y directions
[T , Y ] = meshgrid (t , y ); % create 2 d matrices of points in ty - plane
dT = ones ( size ( T )); % dt =1 for all points
dY = -3.1* Y ; % dy = -3.1* y ; this is the ODE
quiver (T ,Y , dT , dY ) % draw arrows (t , y ) - >( t + dt , t + dy )
axis tight % adjust look
hold on
13
Define vector t of time-values over the given interval to define analytical soution vector.
t = linspace(0,5,100);
Create vector of analytical solution values at corresponding t values.
y=exp(-3.1*t);
Define ODE function.
f=@(t,y) -3.1*y;
Compute numerical solution to IVP with the given timesteps using improved Euler. N = 14
N = 14
[tN,yN]= euler(f, [0 ,5] ,1,N);
Plot numerical solution together with slope field and analytical solution
plot(tN,yN,
'ro-'
,
'LineWidth'
,2)
hold off
;
Your essay answer goes here.
14
once again the graph provides more accurate solutions with a smaller step size.
15
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help