second order variable coefficients ODE -so confused !!!
Show older comments
Hi matlab person:
Nowdays, I am solving a second order variable coefficients ODE {x^2 y''+x y'-sin(y)cos(y)+x sin(y)^2-x^2 sin(y)cos(y)=0}. I feel there is no analytical solution, so I want to use ODE to get numerical solution.But I don't know how I should solve second order variable coefficients ODE through ODE45.
Any suggestion, any help is ok.
Thanks very much.
equation: x^2 y''+x y'-sin(y)cos(y)+x sin(y)^2-x^2 sin(y)cos(y)=0
boundary condition: y(0)=0;y'(0)=-0.126
4 Comments
Yash
on 12 Jul 2012
CHECK MY RUNGE KUTTA CODE FOR THAT, MIGHT HELP, ITS FOR TWO VARIABLES
Following matlab functions will be used. function dxdt = f(t,x,y) % main function dxdt =2*y+x+t;
function dydt = g(t,x,y) % main function dydt =sin(x)+exp(t);
function [tout,yout,xout] = rk2variable(f,g,tspan,y0,N,x0) h = (tspan(2)-tspan(1))/N; t = tspan(1); tout = t; y = y0(:); yout = y.'; x = x0(:); xout = x.'; for n=1:N k1 =feval(f,t,x,y); j1=feval(g,t,x,y); k2=feval(f,t+h/2, x + (h/2)*k1, y + (h/2)*j1); j2=feval(g, t + h/2, x + (h/2)*k1, y + (h/2)*j1); k3=feval(f, t + h/2, x + (h/2)*k2, y + (h/2)*j2); j3=feval(g, t + h/2, x + (h/2)*k2, y + (h/2)*j2); k4=feval(f, t + h, x + h* k3, y + h* j3); j4=feval(g, t + h, x + h* k3, y + h* j3); x = x + (h/6)*(k1 + 2*k2 + 2*k3 + k4); y = y + (h/6)*(j1 + 2*j2 + 2*j3 + j4); t = t+h; yout = [yout; y.']; tout = [tout; t]; xout = [xout; x.']; end
This m file will be used to compute the values at different values of h.
clear tmin = 0; tmax = 1; y0 = 0; h =0.01; N = round((tmax-tmin)/h); x0=0; [tout,yout,xout] = rk2variable(@f,@g,[tmin,tmax],y0,N,x0); plot(tout,yout,tout,xout) legend('Y','X')
Yash
on 12 Jul 2012
Following matlab functions will be used.
function dxdt = f(t,x,y) % main function
dxdt =2*y+x+t;
function dydt = g(t,x,y) % main function
dydt =sin(x)+exp(t);
function [tout,yout,xout] = rk2variable(f,g,tspan,y0,N,x0)
h = (tspan(2)-tspan(1))/N;
t = tspan(1); tout = t;
y = y0(:); yout = y.';
x = x0(:); xout = x.';
for n=1:N
k1 =feval(f,t,x,y);
j1=feval(g,t,x,y);
k2=feval(f,t+h/2, x + (h/2)*k1, y + (h/2)*j1);
j2=feval(g, t + h/2, x + (h/2)*k1, y + (h/2)*j1);
k3=feval(f, t + h/2, x + (h/2)*k2, y + (h/2)*j2);
j3=feval(g, t + h/2, x + (h/2)*k2, y + (h/2)*j2);
k4=feval(f, t + h, x + h* k3, y + h* j3);
j4=feval(g, t + h, x + h* k3, y + h* j3);
x = x + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
y = y + (h/6)*(j1 + 2*j2 + 2*j3 + j4);
t = t+h;
yout = [yout; y.']; tout = [tout; t];
xout = [xout; x.'];
end
This m file will be used to compute the values at different values of h.
clear
tmin = 0; tmax = 1; y0 = 0; h =0.01;
N = round((tmax-tmin)/h);
x0=0;
[tout,yout,xout] = rk2variable(@f,@g,[tmin,tmax],y0,N,x0);
plot(tout,yout,tout,xout)
legend('Y','X')
Jan
on 12 Jul 2012
@petter: Please consider, that the term "urgent" is not apropriate, when the answers are craeted by volunteers. Even the "please" does not shift the tone back to a friendly level sufficently.
Answers (1)
You can convert the ODE system of order k for a problem of dimension d into a system of order 1 and dimension k*d. Instructions can be found at Wikipedia or in your Numerics script.
Then the documentation of ODE45 will help you to implement this in Matlab, especially the examples on "doc ode45".
1 Comment
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!