undefined function or variable x

2 vues (au cours des 30 derniers jours)
AVIJIT NEMO
AVIJIT NEMO le 5 Nov 2020
Commenté : Alan Stevens le 5 Nov 2020
Why I am getting undefined function or variable x error here?
% Solution of second-order differential equation
% The function diff2(x,y) is created to evaluate the diff. equation
% the name of the m-file is diff2.m
% the function is defined as:
%
function xdot = diff2(t,x)
is = 2;
c = 50e-6; L = 1/32; r = 10;
k1 = 1/c ; % 1/C
k2 = 1/L ; % 1/L
k3 = 1/(r*c); % 1/RC
xdot(1) = k2*x(2);
xdot(2) = k1*is - k1*x(1) - k3*x(2);
% solution of second-order differential equation
% the function diff2(x,y) is created to evaluate
% the differential equation
% the name of m-file is diff2.m
%
% Transient analysis of RLC circuit using ode function
% numerical solution
t0 = 0;
tf = 30*e^-3;
x0 = [0 20]; % Initial conditions
[t,x] = ode23('diff2',t0,tf,x0);
% Second column of matrix x represent capacitor voltage
plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')

Réponse acceptée

Alan Stevens
Alan Stevens le 5 Nov 2020
Like so (the function must come last in the script):
% solution of second-order differential equation
% the function diff2(x,y) is created to evaluate
% the differential equation
% the name of m-file is diff2.m
%
% Transient analysis of RLC circuit using ode function
% numerical solution
t0 = 0;
tf = 0.03;
x0 = [0 20]; % Initial conditions
[t,x] = ode23(@diff2,[t0 tf],x0);
% Second column of matrix x represent capacitor voltage
plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')
%
function xdot = diff2(t,x)
is = 2;
c = 50e-6; L = 1/32; r = 10;
k1 = 1/c ; % 1/C
k2 = 1/L ; % 1/L
k3 = 1/(r*c); % 1/RC
xdot = zeros(2,1);
xdot(1) = k2*x(2);
xdot(2) = k1*is - k1*x(1) - k3*x(2);
end
  5 commentaires
Alan Stevens
Alan Stevens le 5 Nov 2020
I'm afraid my knowledge of electricity in general is pretty much limited to Ohm's law!
Alan Stevens
Alan Stevens le 5 Nov 2020
Any further contact should be done via the MATLAB forum.

Connectez-vous pour commenter.

Plus de réponses (2)

KSSV
KSSV le 5 Nov 2020
Save the below code in a file as myfunction.m and run it.
function myfunction()
% solution of second-order differential equation
% the function diff2(x,y) is created to evaluate
% the differential equation
% the name of m-file is diff2.m
%
% Transient analysis of RLC circuit using ode function
% numerical solution
t0 = 0;
tf = 30*e^-3;
x0 = [0 20]; % Initial conditions
[t,x] = ode23(@diff2,[t0 tf],x0);
% Second column of matrix x represent capacitor voltage
plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')
end
% Solution of second-order differential equation
% The function diff2(x,y) is created to evaluate the diff. equation
% the name of the m-file is diff2.m
% the function is defined as:
%
function xdot = diff2(t,x)
is = 2;
c = 50e-6; L = 1/32; r = 10;
k1 = 1/c ; % 1/C
k2 = 1/L ; % 1/L
k3 = 1/(r*c); % 1/RC
xdot(1) = k2*x(2);
xdot(2) = k1*is - k1*x(1) - k3*x(2);
end

KSSV
KSSV le 5 Nov 2020
Save the below code in to a file say main.m.
% solution of second-order differential equation
% the function diff2(x,y) is created to evaluate
% the differential equation
% the name of m-file is diff2.m
%
% Transient analysis of RLC circuit using ode function
% numerical solution
t0 = 0;
tf = 30*e^-3;
x0 = [0 20]; % Initial conditions
[t,x] = ode23(@diff2,[t0 tf],x0);
% Second column of matrix x represent capacitor voltage
plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')
Save the below code into a function in a file diff2.
% Solution of second-order differential equation
% The function diff2(x,y) is created to evaluate the diff. equation
% the name of the m-file is diff2.m
% the function is defined as:
%
function xdot = diff2(t,x)
is = 2;
c = 50e-6; L = 1/32; r = 10;
k1 = 1/c ; % 1/C
k2 = 1/L ; % 1/L
k3 = 1/(r*c); % 1/RC
xdot(1) = k2*x(2);
xdot(2) = k1*is - k1*x(1) - k3*x(2);
end
After saving the above two files, now run the file in main.m.
  1 commentaire
AVIJIT NEMO
AVIJIT NEMO le 5 Nov 2020
main
Error using odearguments (line 93)
DIFF2 must return a column vector.
Error in ode23 (line 114)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in main (line 11)
[t,x] = ode23(@diff2,[t0 tf],x0);
Now I am getting this error. Can you please resolve this?

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by