How do I plotting Matlab functions, ode23, exact value functions side by side?

5 vues (au cours des 30 derniers jours)
Please help. 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 is inputed and 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.
This is what i've been able to come up with so far.
%%%%%%%%%%%%%%%%%%%%%%%%%
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')
.

Réponse acceptée

Alan Stevens
Alan Stevens le 18 Fév 2021
Like this
format long
r=5000;
c=200*(10^(-6));
vs=10;
tf=3;
%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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(end).*exp((-(t2-ti2)/(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);
% figure
plot(t,vc,'ro','MarkerSize',12)
hold on;
% for discharging
%vc2(1) = vc(end);
dvc2dt= @(t2,vc2) -vc2/(r*c);
[t2,vc2]= ode23(dvc2dt, [ti2 tf2],vc(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')

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by