Finite difference method to solve a nonlinear eqn?

Hello,
I have a second order nonlinear question and I need to solve it for different times by using finite difference method but I don't know how to start it. I am quite new in Matlab. Is there anyone who can help me or at least show me a way to do this?
thank you

6 件のコメント

Torsten
Torsten 2022 年 3 月 25 日
If we knew the equation, maybe we could help.
Onur Metin Mertaslan
Onur Metin Mertaslan 2022 年 3 月 25 日
編集済み: Onur Metin Mertaslan 2022 年 3 月 25 日
y''+(g/l)sin(y)=0, 0<t<T, initial cond: y(0)=y_0 and y'(0)=v_0
thank you
Torsten
Torsten 2022 年 3 月 25 日
編集済み: Torsten 2022 年 3 月 25 日
And what is the finite difference method you are supposed to use ?
Or are you allowed to use ode45, e.g. ?
Onur Metin Mertaslan
Onur Metin Mertaslan 2022 年 3 月 25 日
yes any method, for delta t= 0.01, delta t=0.025 and delta t=0.05.
Torsten
Torsten 2022 年 3 月 25 日
g = 9.81;
L = 1.0;
T = 1.0;
dT = 0.01;
y_0 = pi/2;
v_0 = 0;
f = @(t,y)[y(2);-g/L*sin(y(1))];
tspan = 0:dT:T;
y0 = [y_0;v_0];
[t,y] = ode45(f,tspan,y0);
plot(t,y)
Onur Metin Mertaslan
Onur Metin Mertaslan 2022 年 3 月 25 日
編集済み: Onur Metin Mertaslan 2022 年 3 月 25 日
Thank you so much for this. And I have a question. If it is possible to add linear solution to the graph to compare the results?
The main question is to show the linear and nonlinear solution on the plot for 3 different delta t's, and I found the linear solution by my hand[y=y_0*cos(sqrts(g/l)t)]
I know I wanted so many things but I am really confused now
I mean is to show all results in one plot possible?

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2022 年 3 月 25 日
編集済み: Torsten 2022 年 3 月 25 日

0 投票

g = 9.81;
L = 1.0;
T = 1.0;
dT = 0.01;
y_0 = pi/2;
v_0 = 0;
f = @(t,y)[y(2);-g/L*sin(y(1))];
tspan = 0:dT:T;
y0 = [y_0;v_0];
[t,y] = ode45(f,tspan,y0);
y_linear = v_0/sqrt(g/L)*sin(sqrt(g/L)*t) + y_0*cos(sqrt(g/L)*t);
plot(t,[y(:,1),y_linear])

3 件のコメント

Onur Metin Mertaslan
Onur Metin Mertaslan 2022 年 3 月 25 日
Thanks for everything and it totally works. Is it possible to make such graph? I mean in the script that you made for me gives me only 2 lines and even when I change the dT, graph does not change.
I am sorry again
Torsten
Torsten 2022 年 3 月 25 日
編集済み: Torsten 2022 年 3 月 25 日
The graph won't change because the dt is not the actual stepsize of the solver, but prescribes the output times for ode45. The stepsize is chosen by the solver and computed internally - you can't influence it because it's adaptively chosen and different for each time step to meet a prescribed error tolerance.
If you want a fixed-step solver to solve your equations for which you can prescribe the stepsize, you will have to program it on your own. E.g. explicit Euler is a simple one.
Onur Metin Mertaslan
Onur Metin Mertaslan 2022 年 3 月 25 日
Okay then, thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by