Solving a differential equation using ode45
1 view (last 30 days)
Show older comments
B1, B2, and B3 are 4-D double, and intial condition are phi = 0 at s = 0, d(phi)/ds = 0 at s = 0. I am trying to solve this equation using ode45. EQUATION IS B1*phi + B2*(d(phi)/ds) - B3*(d^2(phi)/ds^2) = 0.
my code
% Define the linear coefficient matrices B at the specific s value
for i = 1:n_node
B(:,:,:,i) = coefficient_linear(nmp_order, GPr, GPt, GPWr, GPWt, NGPr, NGPt, s(i));
end
% Initialize variables
B1 = B(1,:,:,:); % Coefficient B1
B2 = B(2,:,:,:); % Coefficient B2
B3 = B(3,:,:,:); % Coefficient B3
% Define the anonymous function for the equation
equation = @(s, phi_dphi) [phi_dphi(2); -B1.*phi_dphi(1) + B2.*phi_dphi(2) - B3.*(phi_dphi(2).^2)];
% Define the initial conditions
phi0 = 0; % Initial condition for phi
dphi_ds0 = 0; % Initial condition for d(phi)/ds
% Set the options for ode45 (optional)
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
% Solve the equation using ode45
[t, phi_dphi] = ode45(equation, [S_0, S_L], [phi0; dphi_ds0], options); % error at this line of code
% Extract the solution for phi and d(phi)/ds
phi = phi_dphi(:, 1);
dphi_ds = phi_dphi(:, 2);
% Display the results
disp('Solution:')
disp('---------')
disp('s phi d(phi)/ds')
disp([t, phi, dphi_ds]);
I have bold the line of code and the error is
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
2 Comments
Answers (1)
Sam Chak
on 25 May 2023
The code for the ode45 part can run. No issue with that.
So, I think the problem lies in the computation of the . Try fixing that.
S_0 = 0;
S_L = 20;
% Initialize variables
B1 = 1; % Coefficient B1
B2 = -2; % Coefficient B2
B3 = 0; % Coefficient B3
% Define the anonymous function for the equation
equation = @(s, phi_dphi) [phi_dphi(2); -B1.*phi_dphi(1) + B2.*phi_dphi(2) - B3.*(phi_dphi(2).^2)];
% Define the initial conditions
phi0 = 1; % Initial condition for phi
dphi_ds0 = 0; % Initial condition for d(phi)/ds
% Set the options for ode45 (optional)
options = odeset('RelTol', 1e-6, 'AbsTol', 1e-6);
% Solve the equation using ode45
[t, phi_dphi] = ode45(equation, [S_0, S_L], [phi0; dphi_ds0], options); % error at this line of code
% Extract the solution for phi and d(phi)/ds
phi = phi_dphi(:, 1);
dphi_ds = phi_dphi(:, 2);
% Test plot
plot(t, phi), grid on
% Display the results
disp('Solution:')
disp('---------')
disp('s phi d(phi)/ds')
disp([t, phi, dphi_ds]);
4 Comments
Sam Chak
on 25 May 2023
I'm not too familiar with some of your math notations. But I think in vector field form, the ODEs should be expressed as follows:
If that is true, then your original ode45 code above was incorrect.
If you are unsure about the matrix multiplication B1, B2, B3, perhaps you can plot out B1, B2, B3. You need to at least provide info about the B's. The ode45 part does not look like a problem yet.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!