ODE45 shooting method
83 ビュー (過去 30 日間)
古いコメントを表示
Frederik Rolighed Christensen
2020 年 4 月 10 日
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
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
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 件のコメント
その他の回答 (1 件)
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
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!