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 日

1 投票

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 月 20 日
Thank you so much! However I've tried using the code & i get the error message "function definition not supported in this context. Create functions in code file." I have MATLAB_R2018b, do I need to download another package or is it somehting else?
Thanks.
Torsten
Torsten 2019 年 2 月 21 日
If you save the above file as "main.m", load it in MATLAB and run it, there should be no problems.
Alexander Kimbley
Alexander Kimbley 2019 年 2 月 25 日
Brilliant, thanks.
Alexander Kimbley
Alexander Kimbley 2019 年 2 月 26 日
Hi, just to double check what are the conditions youve used on Y? Y(0)=0 and what other?
Thanks.
Torsten
Torsten 2019 年 2 月 27 日
I used y(0)=0 and y'(0)=x in "fun_shooting" and adjust x such that y'(pi)=0.
Alexander Kimbley
Alexander Kimbley 2019 年 2 月 27 日
ahh okay, brilliant thanks, you've been a great help!!
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 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2019 年 2 月 19 日

編集済み:

2019 年 2 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by