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

    clear all;
    close all;
    clc;
    
    C=1; % 
    R=2;
    L=1;
    A=1; %amplitude do degrau u=1;
    
    tspan=linspace(0,10,10001); % tempo de simulação [t_inicio t_fim]
    y0=0; %condições iniciais nulas
    dydt0=0;

    upar='impulso';
    [t,y]=ode45(@(t,y) ddt(t,y,C,R,L,A,upar),tspan,[0;A]);
    ys=A.*t.*exp(-t);

    figure(1);

    subplot(3,1,1);
    plot(t,y(:,1),'LineWidth',2.0,'Color','r','LineStyle','-');
    hold on;
    plot(t,ys,'LineWidth',2.0,'Color','b','LineStyle','-.');
    hold off;
    title( 'Resposta ao Impulso - EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 R*C 2*R*C 4*R*C 5*R*C ];
    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,C,R,L,A,upar),tspan,[y0;dydt0]);
    ys=-A.*exp(-t)-A.*t.*exp(-t)+A;

    subplot(3,1,2);
    plot(t,y(:,1),'LineWidth',2.0,'Color','r','LineStyle','-');
    hold on;
    plot(t,ys,'LineWidth',2.0,'Color','b','LineStyle','-.');
    hold off;
    title( 'Resposta ao Degrau- EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 R*C 2*R*C 4*R*C 5*R*C];
    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,C,R,L,A,upar),tspan,[y0;dydt0]);
    ys=2*A.*exp(-t)+A.*t.*exp(-t)+A.*t-2*A;

    subplot(3,1,3);
    plot(t,y(:,1),'LineWidth',2.0,'Color','r','LineStyle','-');
    hold on;
    plot(t,ys,'LineWidth',2.0,'Color','b','LineStyle','-.');
    hold off;
    title( 'Resposta a Rampa - EDO 1a Ordem');
    ylabel('y(t) [V]');
    xlabel('t [s]');
    ax = gca;
    ax.XTick = [0 R*C 2*R*C 4*R*C 5*R*C];
    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,C,R,L,A,upar)
    switch upar
        case 'impulso'
            u=0; %entrada impulso
        case 'degrau'
            u=A;
        case 'rampa'
            u=t*A;
    end
    dydt = [0 1;-1/(L*C) -R/L]*[y(1);y(2)]+[0;1/L]*u; % EDO
end
    
    