error using ode23

조회 수: 8 (최근 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