Getting an error with the ODE45 function.

Despirte my best attempts, i cannot figure out why i'm getting the ODE45 error.
This first Part of the code is in a seperate file.
function dxdt = HW4_CB(t,x) - Getting line error stating "Input argument 't' might be unused, althought a later one is used. Consider replacing it by ~."
global A b u
dxdt = A*x + b*u;
%circuit parameters%
R = 1000; % ohms
L = 0.01; %Henries
C = 1e-6; %Farads
V = 10; % Volts
t0 = 0; %define the initial time
tf = 0.001; %define the final time
A = [-1/R*C 1/C; -1/L 0];
x0= [0 0 0]; %initial conditions
b = [0 1/L];
u = 0;
[t,x] = ode45(@HW4_CB,[t0, tf], x0); This is where i'm receiveing the error.
%Plot the Capacitor Voltage%
figure(1)
plot(t,x(:,1))
xlabel('Time (sec)')
ylabel('Voltage V_C(t) (V)')
%Plot the Inductor Current%
figure(2)
plot(t,x(:,2))
xlabel('Time (sec)')
ylabel ('Current i_L(t) (A)')
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in HW4_Problem_2_CB (line 20)
[t,x] = ode45('HW4_CB',[t0, tf], x0);

 Respuesta aceptada

James Tursa
James Tursa el 15 de Oct. de 2019
Editada: James Tursa el 15 de Oct. de 2019
You've got your calling code mixed in with your derivative code. You need to code this differently. E.g., one way:
In a file called File HW4_Problem_2_CB.m:
%circuit parameters%
R = 1000; % ohms
L = 0.01; %Henries
C = 1e-6; %Farads
V = 10; % Volts
t0 = 0; %define the initial time
tf = 0.001; %define the final time
A = [-1/R*C 1/C; -1/L 0];
x0= [0 0 0]; %initial conditions
b = [0 1/L];
u = 0;
f = @(t,x) HW4_CB(t,x,A,b,u); % pass in A,b,u with different function handle instead of global
[t,x] = ode45(f,[t0, tf], x0); % use our different function handle here
%Plot the Capacitor Voltage%
figure(1)
plot(t,x(:,1))
xlabel('Time (sec)')
ylabel('Voltage V_C(t) (V)')
%Plot the Inductor Current%
figure(2)
plot(t,x(:,2))
xlabel('Time (sec)')
ylabel ('Current i_L(t) (A)')
In a separate file called HW4_CB.m
function dxdt = HW4_CB(t,x,A,b,u) % pass in A,b,u in arguments instead of global
dxdt = A*x + b*u;
return
end

Más respuestas (1)

Chanae Bruno
Chanae Bruno el 15 de Oct. de 2019
Editada: Chanae Bruno el 15 de Oct. de 2019

0 votos

I made the changes and I'm still getting the same ODE45 error. Could it be the input arguement 't' in the HW4_CB that's causing the ODE45 error?

3 comentarios

James Tursa
James Tursa el 15 de Oct. de 2019
Editada: James Tursa el 15 de Oct. de 2019
You are not required to use 't' in your derivative function ... that is simply a warning that can be ignored. (In fact, you are not required to use any of the input arguments, including x). Or you could use ~ as the first input argument instead of t to get rid of the warning. E.g.,
function dxdt = HW4_CB(~,x,A,b,u) % pass in A,b,u in arguments instead of global
dxdt = A*x + b*u;
return
end
Chanae Bruno
Chanae Bruno el 15 de Oct. de 2019
I will try removing the t and x from the function and see if it works. Thank you for your help!
James Tursa
James Tursa el 15 de Oct. de 2019
No, that is not what I meant. In general, you are not required to use any of the input arguments. But in your case, you obviouisly use x so it must remain as an input argument.

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Oct. de 2019

Comentada:

el 15 de Oct. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by