Getting an error with the ODE45 function.
3 views (last 30 days)
Show older comments
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);
0 Comments
Accepted Answer
James Tursa
on 15 Oct 2019
Edited: James Tursa
on 15 Oct 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
0 Comments
More Answers (1)
Chanae Bruno
on 15 Oct 2019
Edited: Chanae Bruno
on 15 Oct 2019
3 Comments
James Tursa
on 15 Oct 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.
See Also
Categories
Find more on Get Started with MATLAB 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!