Given the differential equation dy/dx=2xy; with the i.c., y(1)=1 Solve the equat
ID: 646841 • Letter: G
Question
Given the differential equation dy/dx=2xy; with the i.c., y(1)=1 Solve the equation to derive y(x) for x=1 to 1.5 using the (a) explicit, (b) implicit, (c) modified Euler, and the (d) predictor corrector method and compare to the (e) analytical solution. in a table, provide the absolute and relative errors of each of the numerical solutions at each x. Solve for step sizes of 0.1 and 0.05 Write a MATLAB program to solve the problem and have MATLAB generate a table of the solutions and the errors. Using MATLAB, generate 6 plots. (remember to provide a title for each plot, label the axis and label all of the curves on each plot) Plot 1: Plot of each of the 4 numerical methods and the analytical solution for time step 0.1 Plot 2: Plot of each of the 4 numerical methods and the analytical solution for time step 0.05 Plots 3-6: For each of the 4 numerical methods plot the solution using step size 0.1 and 0.05. For this plot, use the subplot function so that the 4 graphs are on the same page.Explanation / Answer
% compute the chebyshev differentiation matrix and x-grid
[D1,x] = cheb(N);
% boundary condition
D1 = D1(1:end-1,1:end-1); x = x(1:end-1);
% compute the derivatives at x (i.e. at the chebyshev grid points)
f = 2*x.*exp(x.^2 - 1);
% solve
u = D1;
% set the boundary condition
u = lsqr(D1,f);
Where cheb.m is from Trefethen (spectral methods in matlab)
function [D,x] = cheb(N)
% check the base case
if N == 0; D = 0; x = 1; return; end
% create the Chebyshev grid
x = cos(pi*(0:N)/N)';
c = [2; ones(N-1,1);2].*(-1).^(0:N)';
X = repmat(x,1,N+1);
dX = X-X';
D = (c*(1./c)')./(dX+(eye(N+1)));
D = D - diag(sum(D'));
/***************************************************************/
Euler Method
function E=euler(f,a,b,ya,M)
%Input - f is the function entered as a string 'f'
% - a and b are the left and right endpoints
% - ya is the initial condition y(a)
% - M is the number of steps
%Output - E=[T' Y'] where T is the vector of abscissas and
% Y is the vector of ordinates
h=(b-a)/M;
T=zeros(1,M+1);
Y=zeros(1,M+1);
T=a:h:b;
Y(1)=ya;
for j=1:M
Y(j+1)=Y(j)+h*feval(f,T(j),Y(j));
end
E=[T' Y'];
YY=euler(f,a,b,ya,M)
plot(yy(:,1), yy(:,2))
/************************************/
function [ x, y ] = forward_euler ( f_ode, xRange, yInitial, numSteps )
% [ x, y ] = forward_euler ( f_ode, xRange, yInitial, numSteps ) uses
% Euler
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.