'Cannot call or index into a temporary array' error message when setting up a set of coupled odes for ode45.

2 次查看(过去 30 天)
I am trying to solve a set of coupled first order odes with respect to time,t. In some of the equations, I need to evaluate some of the functions I am solving for at a shifted time.
An simple example of what I am trying to do is as follows:
Let y be a function of t, y = y(t). The the ode has a term like this:
dy/dt = (arbitrary function of time)*y(t-epsilon), where epsilon is an arbitrary constant.
Here is part of my code:
function ydot = ODE(t,y,k,epsilon,angle)
ydot=zeros(12,1);
ydot(5) = -(1/k(3))*y(5)+ k(9)*y(6)-k(10)*y(5)+Theta_xy(t,epsilon,angle)*y(1)(t-5*sqrt(2*epsilon));
%Pexy
ydot(6) = -(1/k(3))*y(6)+ k(9)*y(8)-k(9)*y(6)+k(10)*y(5)-k(9)*y(6)+Theta_xy(t,epsilon,angle)*y(2)(t-5*sqrt(2*epsilon));
%Pixy
ydot(7) = -(1/k(6))*y(7)+k(11)*y(8) -k(12)*y(7)+Theta_xy(t,epsilon,angle)*y(3)(t-5*sqrt(2*epsilon));
%Lexy
ydot(8) = -(1/k(6))*y(8)+k(8)*y(6)-k(7)*y(8)+k(12)*y(7)-k(11)*y(8)+Theta_xy(t,epsilon,angle)*y(4)(t-5*sqrt(2*epsilon));
Where k is an array of constants and the components of y are functions we want to solve for in ode45.
The troublesome terms are the final terms in each equation. How do I deal with this?

采纳的回答

Steven Lord
Steven Lord 2018-8-14
Let y be a function of t, y = y(t). The the ode has a term like this:
dy/dt = (arbitrary function of time)*y(t-epsilon), where epsilon is an arbitrary constant.
Then you don't have a system of Ordinary Differential Equations or ODEs.
You have a system of Delay Differential Equations or DDEs. Use a DDE solver.

更多回答(1 个)

Nicholas Galang
Nicholas Galang 2018-8-14
编辑:Nicholas Galang 2018-8-14
The problem is that when you call y(1) that creates a temporary array which you cannot index into. In order to get the value in a multidimensional array you must use the syntax y(i,j) instead of y(i)(j). If you are trying to multiply the two numbers together, you must explicitly use the * symbol because the following line of code
y(1)(t-5*sqrt(2*epsilon))
is creating a temporary array at y(1) then trying to access the value at t-5*sqrt(2*epsilon). In order to multiply the two numbers use the following syntax.
y(1)*(t-5*sqrt(2*epsilon))

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by