PRACTICAL MATLAB® FOR ENGINEERS PRACTICAL MATLAB

(sharon) #1

192 Practical MATLAB® Applications for Engineers


figure(1)
subplot(3,1,1);
ezplot(vct,[0 2]);ylabel(‘vc(t) in volts’);
title(‘plots of: [vc(t), vr(t) and i(t)] vs t’);
subplot(3,1,2);
vrt=20-vct
ezplot(vrt,[0 2]);ylabel(‘vr(t)in volts’);title(‘ ‘);
subplot(3,1,3);
it = vrt/2;
ezplot(it,[0 2]);ylabel(‘it(t)in amps’);title(‘ ‘);
xlabel(‘time in sec.’);
disp(‘The voltage across C, vc(t) for t >0 is:’);
vct
disp(‘The voltage across R=2 ohms is :’)
vrt
disp(‘The current i(t) is :’)
it
disp(‘********Verify solution*******************’);
echo on
verify = 2*diff(vct,t)+vct
disp(‘*******************************************’)
% numerical solution
clear;
% solution using ode45
t0 = 0;R = 2;
tf = 2;
ts = [t0 tf];
v0 = 10; % initial condition
t = linspace(0,2,100);
[t,v t] = ode45(‘f’,ts,v0);

figure(2)
subplot(3,1,1)
plot(t,vt)
title(‘plots of [vc(t), vr(t), and i(t)] vs t’)
ylabel(‘vc(t) in volts’)
subplot(3,1,2)
vr = 20*ones(1,41)-vt’;
plot(t,vr)
ylabel(‘vr(t) in volts’)
subplot(3,1,3)
it=vr./R;
plot(t,it)
ylabel(‘i(t) in volts ‘)
xlabel(‘time in sec’)
function dvtdt=f(t,vt)
dvtdt=10-0.5*vt;
Free download pdf