ODE45 shooting method
85 次查看(过去 30 天)
显示 更早的评论
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

更多回答(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!