2nd degree ODE with ode45

Hello everybody,
this is a great occasion to explain MATLAB's ODE solving strategies to less experienced users like me.
I'm trying to solve a 2nd degree ODE u'' = (1/t)*u' - 4*(t^2)*u with ode45; on [1,20].
Inspired by the documentation for ode45, I tried:
function dy = rigid(x,y)
function ddy = rigid(x,dy)
dy = @(x) diff(y)
ddy = @(x) (1/x)*dy(x) - 4*(x^2)*y(x)
tspan = [1 20]
y0 = [sin(1)+cos(1); 1*cos(1)-1*sin(1)]
[X,Y] = ode45(@rigid,tspan,y0)
plot(X,Y)
which my MATLAB accepts without error messages, but doesn't produce any plot. Could you give me a hint what could be wrong?
Thank you!

답변 (1개)

bym
bym 2012년 5월 24일

2 개 추천

Well, pretty close. You need to define the ODE as a system of first order ODE's realizing the function arguments can be a matrix (example without using anonymous functions):
function dudt = rigid(t,x)
dudt = [x(2);x(2)./t-4.*t.^2*x(1)]; % function named rigid.m
in the script you would then use
tspan = [1 20];
y0 = [sin(1)+cos(1); 1*cos(1)-1*sin(1)];
[X,Y] = ode45(@rigid,tspan,y0);
plot(X,Y);

댓글 수: 4

Nina
Nina 2012년 5월 25일
Thank you! It makes, sense, now, at least, I'm getting an error message, but now, I really don't understand what's the problem.
function du = rigid(t,u)
du = zeros(3,1); %a column vector
du(1) = du(3)./4.*(t^2) - (t.^(-3))*du(2); %this is line 5
du(2) = @(t) du(3).*t + du(1)*4.*(t.^3)
du(3) = @(t) du(2)./t - du(1)*(4.*t.^2)
[T,U] = ode45(@rigid,[1 20],[sin(1)+cos(1) cos(1)-sin(1) -3*cos(1)-5*sin(1)]);
plot(T,U)
Matlab response is "Error using Ch16Pr20b (line 5)
Not enough input arguments."
Do you know what could be the missing argument?
Nina
Nina 2012년 5월 25일
Actually, a more simple code would be
function xdot = fun1(t,x);
xdot(2) = (1/t)* x(2) - 4*t.^2* x(1);
xdot(1) = x(2);
xdot=xdot(:);
X0 = [sin(1)+cos(1) cos(1)-sin(1) -3*cos(1)-5*sin(1)];
t0 = 1;
tf = 20;
options = [];
[t,y] = ode45(@fun1, [t0,tf],X0, options);
plot(t,y)
But the error message remains the same: not enough input arguments...
Sean de Wolski
Sean de Wolski 2012년 5월 25일
I don't know about the error but you're not going to be able to stack two anonymous functions into a vector du. You will get and error on lines 6/7 for trying this.
Walter Roberson
Walter Roberson 2012년 5월 25일
Storing anonymous functions in [object] vectors used to be supported (but not in numeric vectors). But it led to ambiguous syntax, so now one is required to use cell arrays to store anonymous functions.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

질문:

2012년 5월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by