Question

In: Advanced Math

Solve using matlab code The initial value problem dydx−y= 2 cosx, y(0) =−2 has the exact...

Solve using matlab code

The initial value problem dydx−y= 2 cosx, y(0) =−2

has the exact solution y(x) =−e^x −√2 cos (x+π4).

Use the Euler method to solve the initial value problem for 0≤x≤2 using n=10,50,100,200 and plot solutions in one graph.

Repeat #1 using the Runge-Kutta method and plot solutions in one graph with the exact solution

Solutions

Expert Solution

MATLAB Script:

close all
clear
clc

% Given
f = @(x,y) 2*cos(x) + y; % Given ODE
x0 = 0; xf = 2; % Intervals of x
y0 = -2; % Initial condition
y_exact = @(x) -exp(x) - sqrt(2)*cos(x + pi/4); % Exact solution

% Euler Method Solutions
n1 = 10;
h1 = (xf - x0)/n1; % Step Size 1
x1 = x0:h1:xf;
y1 = my_euler(x0, y0, xf, h1, f);

n2 = 50;
h2 = (xf - x0)/n2; % Step Size 2
x2 = x0:h2:xf;
y2 = my_euler(x0, y0, xf, h2, f);

n3 = 100;
h3 = (xf - x0)/n3; % Step Size 3
x3 = x0:h3:xf;
y3 = my_euler(x0, y0, xf, h3, f);

n4 = 200;
h4 = (xf - x0)/n4; % Step Size 4
x4 = x0:h4:xf;
y4 = my_euler(x0, y0, xf, h4, f);

% Plotting
figure, plot(x4, y_exact(x4)), hold on % Plot exact solution
plot(x1, y1, x2, y2, x3, y3, x4, y4), hold off % Plot Euler Method solutions
xlabel('x'), ylabel('y(x)'), title('Euler''s Method')
legend('Exact Solution', 'Euler''s Method (n = 10)', ...
'Euler''s Method (n = 50)', 'Euler''s Method (n = 100)', ...
'Euler''s Method (n = 200)', 'Location', 'southwest')

% RK4 Method Solutions
n1 = 10;
h1 = (xf - x0)/n1; % Step Size 1
x1 = x0:h1:xf;
y1 = my_rk4(x0, y0, xf, h1, f);

n2 = 50;
h2 = (xf - x0)/n2; % Step Size 2
x2 = x0:h2:xf;
y2 = my_rk4(x0, y0, xf, h2, f);

n3 = 100;
h3 = (xf - x0)/n3; % Step Size 3
x3 = x0:h3:xf;
y3 = my_rk4(x0, y0, xf, h3, f);

n4 = 200;
h4 = (xf - x0)/n4; % Step Size 4
x4 = x0:h4:xf;
y4 = my_rk4(x0, y0, xf, h4, f);

% Plotting
figure, plot(x4, y_exact(x4)), hold on % Plot exact solution
plot(x1, y1, x2, y2, x3, y3, x4, y4), hold off % Plot RK4 Method solutions
xlabel('x'), ylabel('y(x)'), title('RK4 Method')
legend('Exact Solution', 'RK4 Method (n = 10)', ...
'RK4 Method (n = 50)', 'RK4 Method (n = 100)', ...
'RK4 Method (n = 200)', 'Location', 'southwest')

function y = my_euler(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
y(i+1) = y(i) + h*f(t(i), y(i)); % Euler Update
end
end

function y = my_rk4(t0, y0, tf, h, f)
y(1) = y0;
t = t0:h:tf;
for i = 1:length(t)-1
k1 = f(t(i), y(i));
k2 = f(t(i) + 0.5*h, y(i) + 0.5*h*k1);
k3 = f(t(i) + 0.5*h, y(i) + 0.5*h*k2);
k4 = f(t(i) + h, y(i) + k3*h);
y(i + 1) = y(i) + (1/6)*(k1 + 2*k2 + 2*k3 + k4)*h; % RK4 Update
end
end

Plots:


Related Solutions

Solve the initial value problem: y''+2y'+y = x^2 , y(0)=0 , y'(0) = 0
Solve the initial value problem: y''+2y'+y = x^2 , y(0)=0 , y'(0) = 0
Solve the initial value problem: y'' + y = cos(x) y(0) = 2 y'(0) = -3...
Solve the initial value problem: y'' + y = cos(x) y(0) = 2 y'(0) = -3 y' being the first derivative of y(x), y'' being the second derivative, etc.
(2x−y3−yex)dx=(2+ex+3xy2 )dy, y(0)=2. Solve the initial value problem and verify exact.
(2x−y3−yex)dx=(2+ex+3xy2 )dy, y(0)=2. Solve the initial value problem and verify exact.
Consider the initial value problem y′ = 18x − 3y, y(0) = 2 (a) Solve it...
Consider the initial value problem y′ = 18x − 3y, y(0) = 2 (a) Solve it as a linear 1st order ODE with the method of the integrating factor. (b) Solve it using a substitution method. (c) Solve it using the Laplace transform.
solve the initial value problem using the method of undetermined coefficients y''+9y=sin3t, y(0)=0, y'(0)=2. Please list...
solve the initial value problem using the method of undetermined coefficients y''+9y=sin3t, y(0)=0, y'(0)=2. Please list every step for algebra.
solve the initial value problem Y" + 2Y' - Y = 0, Y(0)=0,Y'(0) = 2sqrt2
solve the initial value problem Y" + 2Y' - Y = 0, Y(0)=0,Y'(0) = 2sqrt2
Solve the initial value problem. y'=(y^2)+(2xy)+(x^2)-(1), y(0)=1
Solve the initial value problem. y'=(y^2)+(2xy)+(x^2)-(1), y(0)=1
Solve the initial value problem: y''' - 12y'' + 48y' - 72y = 0 ; y(0)...
Solve the initial value problem: y''' - 12y'' + 48y' - 72y = 0 ; y(0) = 1, y'(0) = 0, y''(0) = 0
Using MATLAB: Consider the following Boundary Value Differential Equation: y''+4y=0 y(0)=-2 y(π/4)=10 Which has the exact...
Using MATLAB: Consider the following Boundary Value Differential Equation: y''+4y=0 y(0)=-2 y(π/4)=10 Which has the exact solution: y(x)= -2cos(2x)+10sin(2x) Create a program that will allow the user to input the step size (in x), and two guesses for y'(0). The program will then use the Euler method along with the shooting method to solve this problem. The program should give the true error at y(π/8). Run your code with step sizes of π/400 and π/4000 and compare the errors. Chose...
Solve the following initial value problem: y'''-4y''+20y'=-102e^3x, y(0)=3, y'(0)=-2, y''(0)=-2
Solve the following initial value problem: y'''-4y''+20y'=-102e^3x, y(0)=3, y'(0)=-2, y''(0)=-2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT