Please show all work Page 2 of 7 1. Give an ODE: dyld(x.y)y + 1, with the initia
ID: 2087517 • Letter: P
Question
Please show all work
Page 2 of 7 1. Give an ODE: dyld(x.y)y + 1, with the initial condition y 2@x Q Numerically solve this ODE and compute y value at x-2 with the step size of 1 using Explicit Euler's Method (20%) Midpoint Method (20%) a. b. c. Compare your numerical solutions with the analytical solution at xe-2 and calculate the relative error , %-(y?ne-ynumerical )/ytrue x1 0090 . Use the true value of y(2)-2.271 for your calculation (6%) Explain what you find through the error analysis. (4%) d. Instruction: In order to compute the errors precisely, all your calculation need to have decimals after the floating point, e.g, 2.271. Your error analysis results must have 1 decimal after the floating point, e.g., 2.5%, without keeping the required accuracy during the calculation could cause the completely different values in your calculation answers and could lead to missing partial or all points in the following problems and can cause some penalty in your score.Explanation / Answer
The matlab code to solve the question is given below. It contains 3 parts. The first part, 'main.m', contains the code that provide problem specific inputs, calls the corresponding functions and gives results. The functions used to calculate the solution using Explicit Eulers method and Midpoint methods are 'Eulermethod.m' and 'Midpoint_x.m'
Matlab Code:
Main.m
%Main function to caluculate solution of a differential equation using
%Explicit Eulers method that is defined in Eulermethod.m and Midpoint
%method defined in Midpiont_x.m
clc
close all
clear all
F = @(y,x) x-y+1; % Anonymous function to calculate output of the differential equation
x0 = 0; % Start time
xfinal = 2; % Stop time
h =1; % step size
y0 = 2; % Initil value of the output
[xe,ye] = Eulermethod(F,x0,xfinal,h,y0); % Calculate output using eulers method
[xm,ym] = Midpoint_x(F,x0,xfinal,h,y0); % Calculate output using midpoint method
exact_sol = 2.271;
err_E = (exact_sol-ye(end))*100/exact_sol % Error using Eulers method
err_M = (exact_sol-ym(end))*100/exact_sol % Error using Midpoint method
Eulermethod.m
% Function to calculate output in next input step using Euler method.
% Outputs
% x = input vector with sampling input steps
% y = solution of the differential equation
% Inputs
% F = f(y,x) Anonymous function to calculate output of the differential equation
% x0 = Initial input
% xfinal = final input
% h = step size
% y0 = Initil value of the output
function [x,y] = Eulermethod (F,x0,xfinal,h,y0)
x=x0:h:xfinal; % input vector
y(1)= y0; % Initial y output
for i=1:length(x)-1
y(i+1) = y(i)+h*F(y(i),x(i)); % Calculation of output value
end
Midpoint_x.m
% Function to calculate output in next input step using midpoint method.
% Outputs
% x = input vector with sampling input steps
% y = solution of the differential equation
% Inputs
% F = f(y,x) Anonymous function to calculate output of the differential equation
% x0 = Initial input
% xfinal = final input
% h = step size
% y0 = Initil value of the output
function [x,y] = Midpoint_x (F,x0,xfinal,h,y0)
x=x0:h:xfinal; % input vector
y(1)= y0; % Initial y output
for i=1:length(x)-1
s1 = F(y(i),x(i));
s2 = F(y(i)+h*s1/2,x(i)+h/2);
y(i+1) = y(i)+h*s2; % Calculation of output value
end
The error values for Explicit eulers method and midpoints method are 11.9% and -10.1%
This implies that the value calculated by Explicit Eulers method is lower than exact solution and the value calculated by Midpoint method is higher than the exact solution
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.