error using ode23

9 次查看(过去 30 天)
Adam Makin
Adam Makin 2018-1-21
编辑: Stephen23 2018-1-21
These are my errors im not sure what is causing it.
Error in odearguments (line 90) f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode23 (line 114) odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
code for ode(where t is a vector I've defined as linspace(0,0.2,10000)):
options=odeset('Maxstep', 1e-5);
[t,x]= ode23(@boost,[0:1e-7:0.2], [0 0], options);
  3 个评论
Adam Makin
Adam Makin 2018-1-21
function dx=boost(t,x)
dx=zeros(2,1);
if pwm(t,T,d)==1
dx(1) = v_in/L;
dx(2) = -1 * x(2)/(R*C);
end
if pwm(t,T,d)==0 && x(1)>=0
dx(1) = (v_in - x(2))/L;
dx(2) = (x(1)/C) - (x(2)/R*C);
end
if pwm(t,T,d)==0 && x(1) <0
dx(1) = 0;
dx(2) = -1 * x(2)/(R*C);
end
Adam Makin
Adam Makin 2018-1-21
where pwm is:
function v=pwm(t,T,d)
k = floor(t/T);
v(k*T<=t & t < (k+d)) = 1;
v((k + d)*T <= t & t < (k + 1)*T) = 0;
xlabel("Time(s)");
ylabel("Voltage(V)")
All variables were declared globally in another script that called my ode function.

请先登录,再进行评论。

回答(1 个)

Star Strider
Star Strider 2018-1-21
You have not passed ‘T’ and ‘d’ to ‘boost’ as extra parameters, nor have you defined them in the function. Since ‘boost’ is not an anonymous function, it will not get them from your workspace. You have to pass them as extra arguments if you want ‘boost’ (and ‘pwm’) to use them.
I cannot run your code to test it, since I do not have the ‘pwm’ function. I cannot find it in the current online documentation.
That aside, assuming that the various conditions in your if blocks create discontinuities, note that the derivatives do not exist at discontinuities, so the ODE integration functions will have significant problems with them.
  3 个评论
Star Strider
Star Strider 2018-1-21
First, using global variables are considered very poor programming practice. It can be extremely difficult to debug code that uses them. Other functions can alter them internally, creating chaos in your code.
Second, functions have their own workspaces, so unless you also declare them as global in the functions, the functions do not know they exist.
So instead, pass them as arguments to your functions, so you know what values your functions are receiving, and you know that they are receiving the values.
Stephen23
Stephen23 2018-1-21
编辑:Stephen23 2018-1-21
@Adam Makin: Both of the methods described here are much more reliable than using global variables:

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by