ODE45 shooting method

73 visualizaciones (últimos 30 días)
Frederik Rolighed Christensen
Frederik Rolighed Christensen el 10 de Abr. de 2020
Respondida: Bùi Danh el 3 de Ag. de 2023
Hello
I want to solve a system of 1st order ODE's using ODE45. To apply the shooting method I want to solve for the inital values z0 = [7 z]. The last y-value of the interval y(2) should then be a function of z. I want to plot this y-end-value function with z = linspace(-60,0,60).
In the following I am trying to find this function, but without luck.
clc; clear
syms z
tspan = [0 2];
z0 = [7 z];
[x,y] = ode45(@fun,tspan,z0)
, where @fun is:
function dy = fun(x,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = y(1)^2/7 - x/7 - y(2)/7;
end
  1 comentario
darova
darova el 10 de Abr. de 2020
ode45 is numerical solver. You can't find solution if you use symbolic variables
You have second order ODE (second derivative). Where is the second initial/boundar condition?

Iniciar sesión para comentar.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 10 de Abr. de 2020
Editada: Ameer Hamza el 10 de Abr. de 2020
It appears that a symbolic solution to you equation does not exist and ode45 cannot solve in term of a symbolic variable. To solve it for different initial conditions, you will need to use a for-loop.
z = linspace(-60,0,60);
y2_last = zeros(size(z));
for i=1:numel(z)
tspan = [0 2];
z0 = [7 z(i)];
[x,y] = ode45(@fun,tspan,z0);
y2_last(i) = y(end,2);
end
plot(z, y2_last);
function dy = fun(x,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = y(1)^2/7 - x/7 - y(2)/7;
end
  2 comentarios
Frederik Rolighed Christensen
Frederik Rolighed Christensen el 11 de Abr. de 2020
Exactly, I figured it out at last. Thanks for the reply though!
/Frederik
Ameer Hamza
Ameer Hamza el 11 de Abr. de 2020
Glad to be of help.

Iniciar sesión para comentar.

Más respuestas (1)

Bùi Danh
Bùi Danh el 3 de Ag. de 2023
function shooting_method()
% Set the initial guess for initial condition
y0_guess = 0;
% Set the shooting parameter guess
lambda_guess = 1;
% Set the desired boundary condition
yf_desired = 2;
% Set the tolerance for convergence
tol = 1e-6;
% Set the maximum number of iterations
max_iter = 100;
% Initialize the error and iteration counter
error = 1;
iter = 1;
% Main loop
while error > tol && iter <= max_iter
% Solve the initial value problem using the guessed initial condition
[t, y] = ode45(@ode_func, [0, 1], [y0_guess, lambda_guess]);
% Get the final value of y from the solution
yf = y(end, 1);
% Compute the error
error = abs(yf - yf_desired);
% Compute the derivative of y at t=1
y_deriv = y(end, 2);
% Compute the update for the initial condition guess
y0_guess = y0_guess - (yf - yf_desired) / y_deriv;
% Update the iteration counter
iter = iter + 1;
end
% Print the results
disp(['Final value of y: ', num2str(yf)]);
disp(['Number of iterations: ', num2str(iter)]);
% Plot the solution
plot(t, y(:, 1));
xlabel('t');
ylabel('y');
title('Solution of the boundary value problem');
end
function dydt = ode_func(t, y)
% Define the ODE system
dydt = zeros(2, 1);
dydt(1) = y(2);
dydt(2) = y(1) + t;
end

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by