error using ode23

5 visualizzazioni (ultimi 30 giorni)
Adam Makin
Adam Makin il 21 Gen 2018
Modificato: Stephen23 il 21 Gen 2018
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 Commenti
Adam Makin
Adam Makin il 21 Gen 2018
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 il 21 Gen 2018
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.

Accedi per commentare.

Risposte (1)

Star Strider
Star Strider il 21 Gen 2018
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 Commenti
Star Strider
Star Strider il 21 Gen 2018
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 il 21 Gen 2018
Modificato: Stephen23 il 21 Gen 2018
@Adam Makin: Both of the methods described here are much more reliable than using global variables:

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by