"must return a column vector" ODE45

11 vues (au cours des 30 derniers jours)
Rodolfo Agustin Hernandez
Rodolfo Agustin Hernandez le 10 Mar 2018
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]);

Réponse acceptée

James Tursa
James Tursa le 10 Mar 2018
Modifié(e) : James Tursa le 10 Mar 2018
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 commentaire
Rodolfo Agustin Hernandez
Rodolfo Agustin Hernandez le 15 Mar 2018
Thanks very much. This helped me complete my assignment on time.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by