ODE45 shooting method

조회 수: 79 (최근 30일)
Frederik Rolighed Christensen
Frederik Rolighed Christensen 2020년 4월 10일
답변: Bùi Danh 2023년 8월 3일
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
darova
darova 2020년 4월 10일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 10일
편집: Ameer Hamza 2020년 4월 10일
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
Frederik Rolighed Christensen
Frederik Rolighed Christensen 2020년 4월 11일
Exactly, I figured it out at last. Thanks for the reply though!
/Frederik
Ameer Hamza
Ameer Hamza 2020년 4월 11일
Glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bùi Danh
Bùi Danh 2023년 8월 3일
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

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by