Undefined function or variable x when using ode45?
Show older comments
I'm trying to use ode45 to solve a fairly complicated system of first order odes. When I run the script it says there is an undefined variable 'x' in the function.
Code:
function dx = differentials(t,x)
%Initialize vector
dx = zeros(1:5);
%Define New Variables
D = c*(exp(2*sigma_l*Lc*(f2l*x(2)-f1l*x(1)))-1);
Rp = Pin*((1-exp(sigma_p*Lc*(f2p*x(2)-f1p*x(1))))/(h*freq_p*Lc*Ap));
rc = (-c*ln(1-Toc))/(2*Lr);
%Define differential equations
dx(1) = -Rp + (D*x(5))/(2*Lc)+W21*x(2)+W31*x(3)+W41*x(4)+Cup*(x(2))^2;
dx(2) = Rp - (D*x(5))/(2*Lc)-W21*x(2)+W32*x(3)+W42*x(4)-2*Cup*(x(2))^2;
dx(3) = -W31*x(3)-W32*x(3)+W43*x(4);
dx(4) = -W41*x(4)-W42*x(4)-W43*x(4)+Cup*(x(2))^2;
dx(5) = (D*x(5))/(2*Lc)-rc*x(5)+phi_sp;
end
%Solve Equations
[T,X] = ode45(@differentials,[0,t_length],N0);
All constants mentioned have been defined already. I've checked that they are not the problem. The error comes in at the first mention of x in the function. What am I doing wrong?
Answers (1)
"zeros(1:5)" does not do what you expect, because you want "zeros(5, 1)".
Please copy the complete error message, when you discuss a problem in the forum. There might be important details in the message.
I guess, that you call the function differentials anywhere and forgot the "@". The debugger or the complete error message will reveal the crashing line. Type this in the command window:
dbstop if error
Then run the program again and locate the problem.
I do not understand where the values of c, sigma_L, Lc, f21 etc should come from. Where did you define them? In the shown code the missing definition will cause further errors.
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!