How to use ode45 on a second-order ODE, but without a function file (just one script)?

I am given the following second-order ODE and I want to use ode45 and by not creating a function file.
Capture.PNG
I am given the equation above, a timespan t, and initial y and dy values.
I first set up my dydt into a matrix [y1' y2']"
dydt = [y(2); eps*((y(1)^2) - 1)*y(2) - y(1)]
func= dydt(t,y)
and tried to evaluate such ode45(@func, t, [y0, dy0]), but it gives me an error
I also tried setting it up as just one function:
f = @(y1,y2) eps*((y1^2) - 1)*y2 - y1;
Here, it gives me the error that I am using the same function, which was previously used as a variable:
ode45(@f, t, [y0, dy0])
Help please?

 採用された回答

Star Strider
Star Strider 2019 年 10 月 8 日
You are almost there!
Try this:
dydt = @(t,y) [y(2); eps*((y(1)^2) - 1)*y(2) - y(1)];
ic = [1 1]*eps;
tspan = [0 10];
[T,Y] = ode45(dydt, tspan, ic);
figure
plot(T,Y)
grid
That worked when I tried it. Change the initial conditions vector ‘ic’ to match your initial conditions, and ‘tspan’ to get the result you want.

4 件のコメント

EB
EB 2019 年 10 月 8 日
編集済み: EB 2019 年 10 月 8 日
Awesome, thank you!
Can I ask for an explanation on what the outputs T and Y are and why there are two output values for Y? Are they just the y and y' values with the tspan I have it set calculating for?
Star Strider
Star Strider 2019 年 10 月 8 日
My pleasure!
Of course!
The MATLAB ODE solvers calculate values for the independent variable (usually time, so ‘T’ here) and dependent variable vector or matrix (here ‘Y’).
Are they just the y and y' values with the tspan I have it set calculating for?
Yes! The ‘y'’ value is the first column of ‘Y’, and ‘y’ is the second column, corresponding to the rows of your ‘dydt’ function.
EB
EB 2019 年 10 月 9 日
I see, that makes sense. Thanks again!!
Star Strider
Star Strider 2019 年 10 月 9 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

製品

タグ

質問済み:

EB
2019 年 10 月 8 日

コメント済み:

2019 年 10 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by