coupled differntial equation using ode45
I’m getting an error while solving this coupled differential equation usually the error is showing issues with vertical concatenation. here’s the equation i’m tring to solve with mu0 = exp(-T0) and Boundary conditions as : U(y = -1) = 0 and U(y= 1) = 0 and T0 = 0 at y = -1 and T0 = 1 at y = 1.
here’s my code:
% Main script to solve the velocity and temperature profile
clear;
clc;
% Define constants
G = 1; % Source term (example value)
Na = 1; % Nusselt number (example value)
% Define the domain for y
y_span = [-1 1];
%% Step 1: Solve Velocity Equation to get U and dU/dy
% The velocity equation is:
% d/dy (mu0 * dU/dy) = G
% Define the velocity equation as a system of two first-order ODEs
function dU = velocity_ode(y, U, mu0, G)
dU = [U(2); (1/mu0) * G]; % U(1) = U, U(2) = dU/dy
end
% Initial conditions for velocity at y = -1
U_initial = [0; 0]; % U = 0 and dU/dy = 0 at y = -1 (you can adjust this)
% Solve the velocity equation using ode45
[y_vel, U_sol] = ode45(@(y, U) velocity_ode(y, U, mu0, G), y_span, U_initial);
% Extract dU/dy from the solution
dU_dy = U_sol(:, 2); % This is the derivative of U with respect to y
% Plot the velocity profile and its derivative
figure;
subplot(2,1,1);
plot(y_vel, U_sol(:, 1), ‘b-‘, ‘LineWidth’, 2); % U(y)
xlabel(‘y’);
ylabel(‘U(y)’);
title(‘Velocity Profile’);
subplot(2,1,2);
plot(y_vel, dU_dy, ‘r-‘, ‘LineWidth’, 2); % dU/dy(y)
xlabel(‘y’);
ylabel(‘dU/dy’);
title(‘Velocity Gradient Profile’);
%% Step 2: Solve Temperature Equation using dU/dy from Step 1
% Temperature equation: d^2T0/dy^2 + Na * mu0 * (dU/dy)^2 = 0
% Define the temperature equation as a system of two first-order ODEs
function dT = temperature_ode(y, T, dU_dy, mu0, Na)
dT = zeros(2,1); % Initialize the output vector
% Interpolate dU/dy from the previously computed solution
dUdy_squared = interp1(y_vel, dU_dy.^2, y, ‘linear’, ‘extrap’);
% First equation: dT0/dy = T(2)
dT(1) = T(2);
% Second equation: d^2T0/dy^2 = -Na * mu0 * (dU/dy)^2
dT(2) = -Na * mu0 * dUdy_squared;
end
% Initial conditions for temperature at y = -1
T0_initial = [0; 0]; % T0 = 0 and dT0/dy = 0 at y = -1 (adjust second value if needed)
mu0 = ex(-T0); % Viscosity (example value)
% Solve the temperature equation using ode45
[y_temp, T_sol] = ode45(@(y, T) temperature_ode(y, T, dU_dy, mu0, Na), y_span, T0_initial);
% Plot the temperature profile
figure;
plot(y_temp, T_sol(:, 1), ‘r-‘, ‘LineWidth’, 2); % T0
xlabel(‘y’);
ylabel(‘T_0(y)’);
title(‘Temperature Profile’);
Please help me here, thanks in advanceI’m getting an error while solving this coupled differential equation usually the error is showing issues with vertical concatenation. here’s the equation i’m tring to solve with mu0 = exp(-T0) and Boundary conditions as : U(y = -1) = 0 and U(y= 1) = 0 and T0 = 0 at y = -1 and T0 = 1 at y = 1.
here’s my code:
% Main script to solve the velocity and temperature profile
clear;
clc;
% Define constants
G = 1; % Source term (example value)
Na = 1; % Nusselt number (example value)
% Define the domain for y
y_span = [-1 1];
%% Step 1: Solve Velocity Equation to get U and dU/dy
% The velocity equation is:
% d/dy (mu0 * dU/dy) = G
% Define the velocity equation as a system of two first-order ODEs
function dU = velocity_ode(y, U, mu0, G)
dU = [U(2); (1/mu0) * G]; % U(1) = U, U(2) = dU/dy
end
% Initial conditions for velocity at y = -1
U_initial = [0; 0]; % U = 0 and dU/dy = 0 at y = -1 (you can adjust this)
% Solve the velocity equation using ode45
[y_vel, U_sol] = ode45(@(y, U) velocity_ode(y, U, mu0, G), y_span, U_initial);
% Extract dU/dy from the solution
dU_dy = U_sol(:, 2); % This is the derivative of U with respect to y
% Plot the velocity profile and its derivative
figure;
subplot(2,1,1);
plot(y_vel, U_sol(:, 1), ‘b-‘, ‘LineWidth’, 2); % U(y)
xlabel(‘y’);
ylabel(‘U(y)’);
title(‘Velocity Profile’);
subplot(2,1,2);
plot(y_vel, dU_dy, ‘r-‘, ‘LineWidth’, 2); % dU/dy(y)
xlabel(‘y’);
ylabel(‘dU/dy’);
title(‘Velocity Gradient Profile’);
%% Step 2: Solve Temperature Equation using dU/dy from Step 1
% Temperature equation: d^2T0/dy^2 + Na * mu0 * (dU/dy)^2 = 0
% Define the temperature equation as a system of two first-order ODEs
function dT = temperature_ode(y, T, dU_dy, mu0, Na)
dT = zeros(2,1); % Initialize the output vector
% Interpolate dU/dy from the previously computed solution
dUdy_squared = interp1(y_vel, dU_dy.^2, y, ‘linear’, ‘extrap’);
% First equation: dT0/dy = T(2)
dT(1) = T(2);
% Second equation: d^2T0/dy^2 = -Na * mu0 * (dU/dy)^2
dT(2) = -Na * mu0 * dUdy_squared;
end
% Initial conditions for temperature at y = -1
T0_initial = [0; 0]; % T0 = 0 and dT0/dy = 0 at y = -1 (adjust second value if needed)
mu0 = ex(-T0); % Viscosity (example value)
% Solve the temperature equation using ode45
[y_temp, T_sol] = ode45(@(y, T) temperature_ode(y, T, dU_dy, mu0, Na), y_span, T0_initial);
% Plot the temperature profile
figure;
plot(y_temp, T_sol(:, 1), ‘r-‘, ‘LineWidth’, 2); % T0
xlabel(‘y’);
ylabel(‘T_0(y)’);
title(‘Temperature Profile’);
Please help me here, thanks in advance I’m getting an error while solving this coupled differential equation usually the error is showing issues with vertical concatenation. here’s the equation i’m tring to solve with mu0 = exp(-T0) and Boundary conditions as : U(y = -1) = 0 and U(y= 1) = 0 and T0 = 0 at y = -1 and T0 = 1 at y = 1.
here’s my code:
% Main script to solve the velocity and temperature profile
clear;
clc;
% Define constants
G = 1; % Source term (example value)
Na = 1; % Nusselt number (example value)
% Define the domain for y
y_span = [-1 1];
%% Step 1: Solve Velocity Equation to get U and dU/dy
% The velocity equation is:
% d/dy (mu0 * dU/dy) = G
% Define the velocity equation as a system of two first-order ODEs
function dU = velocity_ode(y, U, mu0, G)
dU = [U(2); (1/mu0) * G]; % U(1) = U, U(2) = dU/dy
end
% Initial conditions for velocity at y = -1
U_initial = [0; 0]; % U = 0 and dU/dy = 0 at y = -1 (you can adjust this)
% Solve the velocity equation using ode45
[y_vel, U_sol] = ode45(@(y, U) velocity_ode(y, U, mu0, G), y_span, U_initial);
% Extract dU/dy from the solution
dU_dy = U_sol(:, 2); % This is the derivative of U with respect to y
% Plot the velocity profile and its derivative
figure;
subplot(2,1,1);
plot(y_vel, U_sol(:, 1), ‘b-‘, ‘LineWidth’, 2); % U(y)
xlabel(‘y’);
ylabel(‘U(y)’);
title(‘Velocity Profile’);
subplot(2,1,2);
plot(y_vel, dU_dy, ‘r-‘, ‘LineWidth’, 2); % dU/dy(y)
xlabel(‘y’);
ylabel(‘dU/dy’);
title(‘Velocity Gradient Profile’);
%% Step 2: Solve Temperature Equation using dU/dy from Step 1
% Temperature equation: d^2T0/dy^2 + Na * mu0 * (dU/dy)^2 = 0
% Define the temperature equation as a system of two first-order ODEs
function dT = temperature_ode(y, T, dU_dy, mu0, Na)
dT = zeros(2,1); % Initialize the output vector
% Interpolate dU/dy from the previously computed solution
dUdy_squared = interp1(y_vel, dU_dy.^2, y, ‘linear’, ‘extrap’);
% First equation: dT0/dy = T(2)
dT(1) = T(2);
% Second equation: d^2T0/dy^2 = -Na * mu0 * (dU/dy)^2
dT(2) = -Na * mu0 * dUdy_squared;
end
% Initial conditions for temperature at y = -1
T0_initial = [0; 0]; % T0 = 0 and dT0/dy = 0 at y = -1 (adjust second value if needed)
mu0 = ex(-T0); % Viscosity (example value)
% Solve the temperature equation using ode45
[y_temp, T_sol] = ode45(@(y, T) temperature_ode(y, T, dU_dy, mu0, Na), y_span, T0_initial);
% Plot the temperature profile
figure;
plot(y_temp, T_sol(:, 1), ‘r-‘, ‘LineWidth’, 2); % T0
xlabel(‘y’);
ylabel(‘T_0(y)’);
title(‘Temperature Profile’);
Please help me here, thanks in advance ode45, matlab, coupledode MATLAB Answers — New Questions