ODE45 shooting method

87 visualizzazioni (ultimi 30 giorni)
Frederik Rolighed Christensen
Risposto: Bùi Danh il 3 Ago 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 Commento
darova
darova il 10 Apr 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?

Accedi per commentare.

Risposta accettata

Ameer Hamza
Ameer Hamza il 10 Apr 2020
Modificato: Ameer Hamza il 10 Apr 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 Commenti
Frederik Rolighed Christensen
Exactly, I figured it out at last. Thanks for the reply though!
/Frederik
Ameer Hamza
Ameer Hamza il 11 Apr 2020
Glad to be of help.

Accedi per commentare.

Più risposte (1)

Bùi Danh
Bùi Danh il 3 Ago 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

Community Treasure Hunt

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

Start Hunting!

Translated by