Question: Please help. This is MATLAB code. I'm not able to get MATLAB to properly display charging and discharging of a RC circuit with a DC
Please help. This is MATLAB code. I'm not able to get MATLAB to properly display charging and discharging of a RC circuit with a DC source, using the exact value method, and the ode23 method. The only part that works is Euler's method. The goal is to produce something similar to the picture but for exact values,ode23, and Euler's method. The total run time is 10s, but the charge time can be anytime amount of time between 1 and 10. Once the charge time is reached, (it could be fully charged by then or partially charged), it begins to discharge, and will discharge for the remainder of what's left of the ten seconds.
.
below is my code.
------------------------------------------
format long r=5000; c=200*(10^(-6)); vs=10; tf=1;
%% 2 exact solution for charging and discharging % for charging ti=0; dt=0.01; t=ti:dt:tf;
vcapc = vs*(1-exp((-t)/(r*c)));
plot(t,vcapc,'r')
hold on;
% for discharging
ti2=tf; tf2=10-tf; t2=ti2:dt:tf2; vcapd(1)=vcapd(tf) vcapd = vcapc(tf).*exp((-t2/(-r*c)))
% Graphing
plot(t2,vcapd,'b') hold on
%% 3 ODE23 for charging
dvcdt= @(t,vc) (vs-vc)/(r*c); [t,vc]= ode23(dvcdt, [ti tf],0);
plot(t,vc,'ro','MarkerSize',12) hold on;
% for discharging vc2(1) = vc(end);
dvc2dt= @(t2,vc2) (-vc(end))/(r*c); [t2,vc2]= ode23(dvc2dt, [ti2 tf2],t(end));
plot(t2,vc2,'bo','MarkerSize',12) hold on;
%% 4 Euler's Charging and Discharging t(1)=ti; n =(tf-ti)/dt; vc=zeros(n+1,1); vc(1)=0;
for i = 1:n t(i+1) = t(i)+dt; vc(i+1) = vc(i)+dt.*((vs-vc(i))/(r*c)); end plot(t,vc,'rs') hold on;
% Euler's Discharging
%ti2=tf; %tf2= 2*tf; %t2=ti2:dt:tf2;
t2(1)=ti2; m = 1000-n; vc2=zeros(m+1,1); vc2(1)=vc(n+1);
for j = 1:m t2(j+1) = t2(j)+dt; vc2(j+1) = vc2(j)+dt.*((vc2(j))/(-r*c)); end plot(t2,vc2,'bs')
%% 5 Graphing Labels
grid xlabel('time (s)') ylabel('voltage of capacitor') title('RC Circuit')
RC circuit - rectangular input 10 R = 5000 9 R = 20000 8 Voltage (V) + 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 Times RC circuit - rectangular input 10 R = 5000 9 R = 20000 8 Voltage (V) + 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 Times
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
