Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please fix my matlab code so that Improved Euler Method is correct % Define the parameters y 0 = 1 ; h _ values =

Please fix my matlab code so that Improved Euler Method is correct % Define the parameters
y0=1;
h_values =[1/8,1/16,1/32,1/64];
t_end =20;
% Initialize time vector
t =0:max(h_values):t_end;
% Preallocate arrays
n = length(t);
y_improved_euler = zeros(length(h_values), n);
y_rk4= zeros(length(h_values), n);
exact_solution = zeros(1, n);
% Initial conditions
y_improved_euler(:,1)= y0;
y_rk4(:,1)= y0;
exact_solution(1)= y0;
% Define the differential equation function
dydt = @(t, y) y - y.^2+1.14* cos(exp(t/2));
% Improved Euler's method for each time step
for j =1:length(h_values)
h = h_values(j);
for i =1:n-1
k1= h * dydt(t(i), y_improved_euler(j, i));
k2= h * dydt(t(i)+ h, y_improved_euler(j, i)+ k1);
y_improved_euler(j, i+1)= y_improved_euler(j, i)+0.5*(k1+ k2);
end
end
% RK4 method for each time step
for j =1:length(h_values)
h = h_values(j);
for i =1:n-1
k1= h * dydt(t(i), y_rk4(j, i));
k2= h * dydt(t(i)+ h/2, y_rk4(j, i)+ k1/2);
k3= h * dydt(t(i)+ h/2, y_rk4(j, i)+ k2/2);
k4= h * dydt(t(i)+ h, y_rk4(j, i)+ k3);
y_rk4(j, i+1)= y_rk4(j, i)+(1/6)*(k1+2*k2+2*k3+ k4);
end
end
% Exact solution using numerical integration
opts = odeset('RelTol',1e-8, 'AbsTol', 1e-8);
[t_exact, y_exact]= ode45(dydt, t, y0, opts);
% Plot Improved Euler's method approximations
figure;
hold on;
for j =1:length(h_values)
plot(t, y_improved_euler(j,:),'-', 'LineWidth', 1.5);
end
plot(t_exact, y_exact, 'k--', 'LineWidth', 1.5);
title("Improved Euler's Method Approximation");
xlabel('t');
ylabel('y');
legend('h =1/8','h =1/16','h =1/32','h =1/64', 'Exact Solution');
grid on;
% Plot RK4 method approximations
figure;
hold on;
for j =1:length(h_values)
plot(t, y_rk4(j,:),'-', 'LineWidth', 1.5);
end
plot(t_exact, y_exact, 'k--', 'LineWidth', 1.5);
title("RK4 Method Approximation");
xlabel('t');
ylabel('y');
legend('h =1/8','h =1/16','h =1/32','h =1/64', 'Exact Solution');
grid on;
% Discussion of results
disp("Results Discussion:");
disp("Improved Euler's method generally improves accuracy with smaller h values due to its higher-order approximation.");
disp("RK4 method consistently shows higher accuracy compared to Improved Euler's method across all tested h values.");
disp("Exact solution closely matches RK4 method, validating its accuracy.");

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

More Books

Students also viewed these Databases questions

Question

which term best describes a market structure of limited competition

Answered: 1 week ago