"must return a column vector" ODE45

조회 수: 7 (최근 30일)
Rodolfo Agustin Hernandez
Rodolfo Agustin Hernandez 2018년 3월 10일
댓글: Rodolfo Agustin Hernandez 2018년 3월 15일
Hi all, I am trying to solve a free vibration response problem using ode45, and keep getting the same error. My code is as follows:
%%SOLVE SYSTEM OF (TWO) ODES
clear;clc;close all;
%%Set initial conditions
global xst F d Wn Wd
xst = 4.81548E-05
t = [0 (15*pi*50)]
F = 940
d = 0.05
x = 0.00021
M = 220
rmax = 0.997496867
k = F/(x*((((1-rmax^2)^2)+(4*(d^2)*(rmax^2)))^0.5))
W = 15*pi
Wn = (k/M).^0.5
c = 2*d*Wn
Ft = x*(((k.^2)+(c.^2)*(W^2)).^0.5)
Wd = 450.787
%%Calculate solution for single ODE
[t,x] = ode45(@systemODEfunc,[0 50],[xst, 0]);
%%Plot results
plot(t,y(:,1),'- red',t,y(:,2), '- blue')
With the ode45 function being:
function y = systemODEfunc(t,x)
global xst F d Wn Wd
y = zeros(50,2)
%Evaluate x(t)
y(t+1,1) = (exp((-d*Wn*t)))*(xst*cos(Wd*t))
%Evaluate dx(t)
y(t+1,2) = (exp(-d*Wn*t))*(((-d*Wn*(xst*cos(Wd*t)))+(Wd*(-xst*sin(Wd*t)))))
end
No matter what I try I seem to always get the following:
Error using odearguments (line 90)
SYSTEMODEFUNC must return a column vector.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in systemODE (line 22)
[t,x] = ode45(@systemODEfunc,[0 50],[xst, 0]);

채택된 답변

James Tursa
James Tursa 2018년 3월 10일
편집: James Tursa 2018년 3월 10일
The derivative function needs to return a derivative column vector at a single point in time, not construct an array of such derivative vectors inside the function. E.g., something like this
function y = systemODEfunc(t,x)
global xst F d Wn Wd
%Evaluate x(t)
y(1,1) = (exp((-d*Wn*t)))*(xst*cos(Wd*t))
%Evaluate dx(t)
y(2,1) = (exp(-d*Wn*t))*(((-d*Wn*(xst*cos(Wd*t)))+(Wd*(-xst*sin(Wd*t)))))
end
Side comment: Using global variables to pass in parameters to your derivative function is not good practice. Better to pass them in via a function handle. E.g.,
function y = systemODEfunc(t,x,xst,F,d,Wn,Wd)
%Evaluate x(t)
y(1,1) = (exp((-d*Wn*t)))*(xst*cos(Wd*t))
%Evaluate dx(t)
y(2,1) = (exp(-d*Wn*t))*(((-d*Wn*(xst*cos(Wd*t)))+(Wd*(-xst*sin(Wd*t)))))
end
and then in your calling routine simply use this function handle:
@(t,x) systemODEfunc(t,x,xst,F,d,Wn,Wd)
  댓글 수: 1
Rodolfo Agustin Hernandez
Rodolfo Agustin Hernandez 2018년 3월 15일
Thanks very much. This helped me complete my assignment on time.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by