i could not fix the error part !

2 views (last 30 days)
sevgul demir
sevgul demir on 6 Dec 2021
Answered: Star Strider on 6 Dec 2021
% 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
% 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 = 30e-3;
x0 = [0 20]; % Initial conditions
[t,x] = ode23('diff2',t0,tf,x0);
% Second column of matrix x represent capacitor voltage
subplot(211), plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')
% Transient analysis of RLC circuit from Example 5.5
t2 =0:1e-3:30e-3;
vt = -6.667*exp(-1600*t2) + 26.667*exp(-400*t2);
subplot(212), plot(t2,vt)
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 4.5, 'Results from Example 5.5')
this is the codes of my work... i think in the line 26 something going wrong.... could you help me the fix this ?

Answers (1)

Star Strider
Star Strider on 6 Dec 2021
The problems were that the ode23 call has to be:
[t,x] = ode23(@diff2,[t0,tf],x0);
so it refers to ‘diff2’ correctly as a function handle, and the ‘tspan’ variables are now a (1x2) vector.
and the function must be at the end of the script. The function also has to return a column vector, so I fixed that problem with:
xdot(1,:) = k2*x(2);
xdot(2,:) = k1*is - k1*x(1) - k3*x(2);
It now runs without error.
% 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 = 30e-3;
x0 = [0 20]; % Initial conditions
[t,x] = ode23(@diff2,[t0,tf],x0);
% Second column of matrix x represent capacitor voltage
subplot(211), plot(t,x(:,2))
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 7, 'State Variable Approach')
% Transient analysis of RLC circuit from Example 5.5
t2 =0:1e-3:30e-3;
vt = -6.667*exp(-1600*t2) + 26.667*exp(-400*t2);
subplot(212), plot(t2,vt)
xlabel('Time, s'), ylabel('Capacitor voltage, V')
text(0.01, 4.5, 'Results from Example 5.5')
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
.

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!