i make a code of shooting method for 3rd order differential equation but i dont get the accurate result
6 views (last 30 days)
Show older comments
clear all;
clc;
%%%%%%%%%%%% first guess %%%%%%%%%%%%%%%
f = @(x, y)([y(3); y(2); x * y(1) + (x^3 - 2*x^2 - 5*x - 3) * exp(x)]);
gues1 = 1;
gues3 = 5;
x0_first = [0; gues1; gues3];
x = linspace(0, 1, 100);
[x, x_first] = ode45(f, x, x0_first);
u_10_first = x_first(end, 1);
%%%%%%%%%% 2nd guess %%%%%%%%%%
gues2 = 4;
gues4 = 9;
x0_second = [0; gues2; gues4];
[x, x_second] = ode45(f, x, x0_second);
u_10_second = x_second(end, 1);
%%%%%%%%% Linear interpolation %%%%%%%%%%
sol1 = u_10_first;
sol2 = u_10_second;
targ = 1;
targ2 = -2.718;
% Linear interpolation for the first target
gues5 = gues2 + ((gues1 - gues2) / (sol1 - sol2)) * (targ - sol2);
gues6 = gues4 + ((gues3 - gues4) / (sol1 - sol2)) * (targ2 - sol2);
yi = gues5;
zi = gues6;
x0_final_first_target = [0; yi; zi];
[x, x_final_first_target] = ode45(f, x, x0_final_first_target);
u_10_final_first_target = [x(end,end),x_final_first_target(end, 1)]
% Linear interpolation for the second target
gues7 = gues2 + ((gues1 - gues2) / (sol1 - sol2)) * (targ2 - sol2);
gues8 = gues4 + ((gues3 - gues4) / (sol1 - sol2)) * (targ - sol2);
yi = gues7;
zi = gues8;
x0_final_second_target = [0; yi; zi];
[x, x_final_second_target] = ode45(f, x, x0_final_second_target);
u_10_final_second_target = [x(end,end),x_final_second_target(end, 1)]
% Plotting
figure;
plot(x, x_first(:, 1), 'r', x, x_second(:, 1), 'g', x, x_final_first_target(:, 1), 'k', x, x_final_second_target(:, 1), 'b');
legend('first guess', 'second guess', 'final value (target 1)', 'final value (target 2)');
grid on;
2 Comments
Torsten
on 12 Mar 2024
If the underlying equation is a 3rd order differential equatiom, already your definition for f looks wrong.
Maybe you could state your problem first so that we have a chance to understand your code.
Accepted Answer
Torsten
on 13 Mar 2024
Edited: Torsten
on 13 Mar 2024
y20 = 2;
sol = fsolve(@fun_shooting,y20);
[X,Y] = fun_ode(sol);
plot(X,Y(:,1))
function res = fun_shooting(y2)
[X,Y] = fun_ode(y2);
res = Y(end,2) + exp(1);
end
function [X,Y] = fun_ode(y2)
f = @(x,y)[y(2);y(3);x*y(1)+exp(x)*(x^3-2*x^2-5*x-3)];
y0 = [0;1;y2];
xspan = [0 1];
[X,Y] = ode45(f,xspan,y0);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!