Runge Kutta 4th order
0 comentarios
Respuesta aceptada
Más respuestas (12)
0 comentarios
function [xRK, yRK] = runge_kutta(y0, xspan, h) xRK = xspan(1):h:xspan(2); yRK = zeros(size(xRK)); yRK(1) = y0;
for i = 1:(length(xRK)-1) k1 = h * (-2*xRK(i)^3 + 12*xRK(i)^2 - 20*xRK(i) + 8.5); k2 = h * (-2*(xRK(i)+h/2)^3 + 12*(xRK(i)+h/2)^2 - 20*(xRK(i)+h/2) + 8.5); k3 = h * (-2*(xRK(i)+h/2)^3 + 12*(xRK(i)+h/2)^2 - 20*(xRK(i)+h/2) + 8.5); k4 = h * (-2*(xRK(i)+h)^3 + 12*(xRK(i)+h)^2 - 20*(xRK(i)+h) + 8.5); yRK(i+1) = yRK(i) + (k1 + 2*k2 + 2*k3 + k4) / 6; end end
% Run Runge-Kutta method [xRK, yRK] = runge_kutta(10, [0, 4], 0.5); plot(xRK, yRK, '-o'); legend('Analytical Solution', 'Euler Explicit', 'Runge-Kutta');
0 comentarios
function [xI, yEuler2] = euler_implicit(y0, xspan, h) xI = xspan(1):h:xspan(2); yEuler2 = zeros(size(xI)); yEuler2(1) = y0;
for i = 1:(length(xI)-1) % Solve for yEuler2(i+1) using fsolve y_guess = yEuler2(i); % Initial guess for fsolve f = @(y) y - yEuler2(i) - h * (-2*xI(i+1)^3 + 12*xI(i+1)^2 - 20*xI(i+1) + 8.5); yEuler2(i+1) = fsolve(f, y_guess); end end
% Run Euler's implicit method [xI, yEuler2] = euler_implicit(10, [0, 4], 0.5); plot(xI, yEuler2, '-o'); legend('Analytical Solution', 'Euler Explicit', 'Runge-Kutta', 'Euler Implicit');
0 comentarios
0 comentarios
function [xI, yEuler2] = euler_implicit_manual(y0, xspan, h) xI = xspan(1):h:xspan(2); yEuler2 = zeros(size(xI)); yEuler2(1) = y0;
for i = 1:(length(xI)-1) % Use an iterative approach to estimate the next y value y_current = yEuler2(i); x_next = xI(i+1); y_next_guess = y_current; % Initial guess for next y tol = 1e-6; % Tolerance level for convergence
% Iterate until convergence while true % Implicit Euler formula: y_new = y_old + h * f(x_new, y_new) y_new = y_current + h * (-2*x_next^3 + 12*x_next^2 - 20*x_next + 8.5); if abs(y_new - y_next_guess) < tol break; end y_next_guess = y_new; end yEuler2(i+1) = y_new; end end
% Run Euler's implicit method using the manual iteration [xI, yEuler2] = euler_implicit_manual(10, [0, 4], 0.5); plot(xI, yEuler2, '-o'); legend('Analytical Solution', 'Euler Explicit', 'Runge-Kutta', 'Euler Implicit (Manual)');
0 comentarios
0 comentarios
% Define the rate constants k1 = 2.1; % L/(mol.s) k2 = 1.5; % L/(mol.s) k3 = 1.3; % L/(mol.s)
% Initial concentrations x0 = 1.0; % mol/L y0 = 0.2; % mol/L z0 = 0.0; % mol/L
% Time span for the simulation tspan = [0 3]; % seconds
% Define the system of ODEs as a function handle function dydt = reaction_system(t, y) x = y(1); y = y(2); z = y(3);
dxdt = -k1*x + k2*y*z; dydt = k1*x - k2*y*z - 2*k3*y^2; dzdt = k3*y^2;
dydt = [dxdt; dydt; dzdt]; end
% Solve the system of ODEs using ode45 [t, sol] = ode45(@reaction_system, tspan, [x0, y0, z0]);
% Extract the solutions for x, y, and z xVal = sol(:,1); yVal = sol(:,2); zVal = sol(:,3);
% Plot the concentrations over time figure; plot(t, xVal, '-o', 'DisplayName', 'X'); hold on; plot(t, yVal, '-x', 'DisplayName', 'Y'); plot(t, zVal, '-s', 'DisplayName', 'Z'); xlabel('Time (s)'); ylabel('Concentration (mol/L)'); title('Concentration of Species X, Y, Z over Time'); legend('show'); grid on;
0 comentarios
k1 = 2.1; k2 = 1.5; k3 = 1.3;
x0 = 1.0; y0 = 0.2; z0 = 0.0;
tspan = [0 3];
function dydt = reaction_system(t, y) x = y(1); y = y(2); z = y(3);
dxdt = -k1*x + k2*y*z; dydt = k1*x - k2*y*z - 2*k3*y^2; dzdt = k3*y^2;
dydt = [dxdt; dydt; dzdt]; end
[t, sol] = ode45(@reaction_system, tspan, [x0, y0, z0]);
xVal = sol(:,1); yVal = sol(:,2); zVal = sol(:,3);
figure; plot(t, xVal, '-o', 'DisplayName', 'X'); hold on; plot(t, yVal, '-x', 'DisplayName', 'Y'); plot(t, zVal, '-s', 'DisplayName', 'Z'); xlabel('Time (s)'); ylabel('Concentration (mol/L)'); title('Concentration of Species X, Y, Z over Time'); legend('show'); grid on;
0 comentarios
k1 = 10^-2; k2 = 10^4; k3 = 10^2;
x0 = 1.0; y0 = 0.2; z0 = 0.0;
tspan = [0 1000];
function dydt = reaction_system(t, y) x = y(1); y = y(2); z = y(3);
dxdt = -k1*x + k2*y*z; dydt = k1*x - k2*y*z - 2*k3*y^2; dzdt = k3*y^2;
dydt = [dxdt; dydt; dzdt]; end
[t1, sol1] = ode45(@reaction_system, tspan, [x0, y0, z0]); [t2, sol2] = ode15s(@reaction_system, tspan, [x0, y0, z0]);
xVal1 = sol1(:,1); yVal1 = sol1(:,2); zVal1 = sol1(:,3);
xVal2 = sol2(:,1); yVal2 = sol2(:,2); zVal2 = sol2(:,3);
figure; subplot(3,1,1); plot(t1, xVal1, '-o', 'DisplayName', 'X - ode45'); hold on; plot(t2, xVal2, '-x', 'DisplayName', 'X - ode15s'); xlabel('Time (s)'); ylabel('X Concentration (mol/L)'); legend('show');
subplot(3,1,2); plot(t1, yVal1, '-o', 'DisplayName', 'Y - ode45'); hold on; plot(t2, yVal2, '-x', 'DisplayName', 'Y - ode15s'); xlabel('Time (s)'); ylabel('Y Concentration (mol/L)'); legend('show');
subplot(3,1,3); plot(t1, zVal1, '-o', 'DisplayName', 'Z - ode45'); hold on; plot(t2, zVal2, '-x', 'DisplayName', 'Z - ode15s'); xlabel('Time (s)'); ylabel('Z Concentration (mol/L)'); legend('show');
0 comentarios
0 comentarios
0 comentarios
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!