ODE45 for a second order differential equation

I have a second order differential equation : y''=(2*y)+(8*x)*(9-x); Boundary Conditions y(0)=0 , y(9)=0 Need to solve the diff eq using ode45.
I've tried watching a bunch of tutorials but I just cannot seem to figure out how the function is written as a column vector [y';y'']. I don't understand it at all and that might make this query vague too.
Hope someone can help with the code or the explanation on how to solve the above.
Thank you

 Akzeptierte Antwort

Torsten
Torsten am 23 Apr. 2018

3 Stimmen

[x,y] = ode45(@fun,[0 9],[0 -28]);
function dy = fun(x,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = 2*y(1)+8*x*(9-x);
But for a boundary value problem like yours, you will have to use "bvp4c" instead of "ode45".
Best wishes
Torsten.

5 Kommentare

Remston Martis
Remston Martis am 23 Apr. 2018
Bearbeitet: Remston Martis am 23 Apr. 2018

Error :

>> [x,y] = ode45(@fun,[0 9],[0 -28]);
Attempt to execute SCRIPT ode45 as a function:
C:\Users\remst\Desktop\ode45.m

using bvp4c says too many input arguments.

Put

function main
[x,y] = ode45(@fun,[0 9],[0 -28]);
function dy = fun(x,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = 2*y(1)+8*x*(9-x);

in one file, name it main.m and execute it as a function file.

Best wishes

Torsten.

Remston Martis
Remston Martis am 23 Apr. 2018
Bearbeitet: Remston Martis am 23 Apr. 2018
Just doesn't seem to work .
Error Message: Attempt to execute SCRIPT ode45 as a function: C:\Users\remst\Desktop\ode45.m
Error in main (line 2) [x,y] = ode45(@fun,[0 9],[0 -28]);
I tried running the script and I tried calling the function in the commany window. Gives me the same error.
Torsten
Torsten am 23 Apr. 2018
Bearbeitet: Torsten am 23 Apr. 2018
There seems to be a duplicate of ode45 on your desktop - rename or remove it.
Brilliant. Thank you Torsten!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Stephan
Stephan am 22 Apr. 2018
Bearbeitet: Stephan am 22 Apr. 2018

1 Stimme

Hi,

transform a n-th order ode into a system of n 1st order ode's to solve it.

Matlab documentation example: https://de.mathworks.com/help/matlab/math/solve-nonstiff-odes.html

More examples here: https://www.google.de/url?sa=t&source=web&rct=j&url=http://www.math.poly.edu/courses/ma2132/Notes/MA2132EquationsToSystems.pdf&ved=2ahUKEwiQ_tCRq83aAhWMMewKHQ7CC1IQFjALegQIBxAB&usg=AOvVaw07Im9F42d9LrfvRIzL35Pd

If you read this i guess you can quickly solve your problem.

2 Kommentare

Remston Martis
Remston Martis am 23 Apr. 2018
Bearbeitet: Remston Martis am 23 Apr. 2018

I keep getting these errors: Attempt to execute SCRIPT ode45 as a

function: C:\Users\remst\Desktop\ode45.m

Error in ode45 (line 3) [x,y] = ode45(@fun,[0 9],[0 -28]);

My function declarations are:

function fun=fun(x,y)
fun=zeroes(2,1);
fun(1)=y(2);
fun(2)=(2*y(1))+8*x*(9-x);

Calling the function :

[x,y] = ode45(@fun,[0 9],[0 -28]);

Where am I going wrong? I've tried 10000s of ways

Ebraheem Menda
Ebraheem Menda am 30 Jun. 2021
Bearbeitet: Ebraheem Menda am 30 Jun. 2021
In this case you have declared the name of the function and its output both with the same name 'fun'. That is the problem it seems.

Melden Sie sich an, um zu kommentieren.

NARSIRAM GURJAR
NARSIRAM GURJAR am 16 Sep. 2019

0 Stimmen

C:\Users\remst\Desktop\ode45.m
save this file with different name

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by