Shooting Method On Harmonic Equation

조회 수: 2 (최근 30일)
Alexander Kimbley
Alexander Kimbley 2019년 2월 19일
편집: Torsten 2019년 2월 28일
I'm really new to Matlab, so this may be ridicously easy what I'm about to ask, but bare with me please.
I'm trying to integrate the Harmonic equation y'' +(a^2)*y=0 with a=2.4, with BC y(0)=y'(pi)=0. I'm doing this to try and "shoot" for the actual value of y at y=pi which we obviously can find analytically but I need to get my head around the code so I can apply this to a more complicated problem.
Thanks.

채택된 답변

Torsten
Torsten 2019년 2월 20일
function main
ydot0_start = 1.0;
a = 2.4;
iflag = 0;
sol = fzero(@(x)fun_shooting(x,a,iflag),ydot0_start);
iflag = 1;
y_at_pi = fun_shooting(sol,a,iflag)
end
function res = fun_shooting(x,a,iflag)
fun_ode = @(t,y)[y(2);-a^2*y(1)];
tspan = [0,pi];
y0 = [0;x];
[t,y] = ode45(fun_ode,tspan,y0);
if iflag == 0
res = y(end,2);
else
res = y(end,1);
end
end
  댓글 수: 8
Alexander Kimbley
Alexander Kimbley 2019년 2월 27일
So how would I go about changing the conditions to y(0)=0, y'(0)=1, where we alter 'a' to get y(pi)=0 to 10^-8 accuaracy say, assuming we dont actually know the true value of a.
Thanks.
Torsten
Torsten 2019년 2월 28일
편집: Torsten 2019년 2월 28일
function main
a0 = 4.5;
a = fzero(@fun_shooting,a0)
end
function res = fun_shooting(x)
fun_ode = @(t,y)[y(2);-x^2*y(1)];
tspan = [0,pi];
y0 = [0;1];
[t,y] = ode45(fun_ode,tspan,y0);
res = y(end,1);
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by