% EES-10 - 2S2023
% Simulação temporal RC

    clear all;
    close all;
    clc;
    
    RC=1000*0.00001; % 1kOnhm, 10uF
    A=1; %amplitude do degrau u=1;
    
    tspan=linspace(0,5*RC,1001); % tempo de simulação [t_inicio t_fim]
    y0=0; %condições iniciais nulas

    upar='impulso';
    [t,y]=ode45(@(t,y) ddt(t,y,RC,A,upar),tspan,A);

    figure(1);

    subplot(3,1,1);
    plot(t,y,'LineWidth',2.0,'Color','r','LineStyle','-');
    title( 'Resposta ao Impulso - EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 RC 2*RC 4*RC 5*RC ];
    ax.YTick = [0 0.63*A 0.86*A 0.98*A 0.99*A];
    ax.XTickLabel = {'0','RC','2RC','4RC','5RC'};
    ax.YTickLabel = {'0','0.63A','0.86A','0.98A',''};
    grid on;

    upar='degrau';
    [t,y]=ode45(@(t,y) ddt(t,y,RC,A,upar),tspan,y0);

    subplot(3,1,2);
    plot(t,y,'LineWidth',2.0,'Color','r','LineStyle','-');
    title( 'Resposta ao Degrau- EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 RC 2*RC 4*RC 5*RC ];
    ax.YTick = [0 0.63*A 0.86*A 0.98*A 0.99*A];
    ax.XTickLabel = {'0','RC','2RC','4RC','5RC'};
    ax.YTickLabel = {'0','0.63A','0.86A','0.98A',''};
    grid on;

    upar='rampa';
    [t,y]=ode45(@(t,y) ddt(t,y,RC,A,upar),tspan,y0);

    subplot(3,1,3);
    plot(t,y,'LineWidth',2.0,'Color','r','LineStyle','-');
    title( 'Resposta a Rampa - EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 RC 2*RC 4*RC 5*RC ];
    ax.YTick = [0 0.63*A 0.86*A 0.98*A 0.99*A];
    ax.XTickLabel = {'0','RC','2RC','4RC','5RC'};
    ax.YTickLabel = {'0','0.63A','0.86A','0.98A',''};
    grid on;



function dydt = ddt(t,y,RC,A,upar)
    switch upar
        case 'impulso'
            u=0; %entrada impulso
        case 'degrau'
            u=A;
        case 'rampa'
            u=t*A;
    end
    dydt = -1/(RC)*y+1/(RC)*u; % EDO
end
    
    