syms theta(t) m r EGR Q u k taustall A B C t;
thetadot(t) = diff(theta(t),t);
ode = 0.5*m*(r^2)*diff(theta(t),t,2)+(((-Q*EGR)+u)*diff(theta(t),t))+(k*(r^2)*theta(t))+(taustall*EGR) == (((A/2)*sin((B*t)+C))+(A/2))*r
pretty(ode)
cond1 = theta(0) == 0;
cond2 = thetadot(0) == 0;
conds = [cond1 cond2];
solntheta(t) = dsolve(ode,conds);
solntheta = simplify(solntheta);
diffsolntheta = diff(solntheta,t);
forcingfunc = (((A/2)*sin((B*t)+C))+(A/2));
m = 0.1;
r = 0.025;
EGR = 1;
Q = (0.003531/513.3);
u = 0.01;
k = 30.7;
taustall = 0.003531;
A = 0.45;
B = 6/(2*pi);
C = (3*pi)/2;
t = 0:0.1:20;
subsofsolntheta = subs(solntheta);
subsofdiffsolntheta = subs(diffsolntheta);
subsofforcingfunc = subs(forcingfunc);
powerinmW = abs(1000*(((-0.003531/513.3).*subsofdiffsolntheta)+0.003531).*subsofdiffsolntheta);
powerinW = abs((((-0.003531/513.3).*subsofdiffsolntheta)+0.003531).*subsofdiffsolntheta);
currentinmilliAmps = 1000*(sqrt(powerinW./3000));
Voltage = (currentinmilliAmps/1000)*3000;
FUN = matlabFunction(powerinW);
y = feval(FUN, t);
energyinJ = cumtrapz(t,y);
figure
hold on
plot(t,subsofforcingfunc,':k','LineWidth',3)
plot(t,subsofsolntheta,'-b','LineWidth',2)
plot(t,subsofdiffsolntheta,'-m','LineWidth',2)
plot(t,powerinmW,'--r','LineWidth',2,'MarkerIndices',1:15:length(subsofsolntheta))
plot(t,currentinmilliAmps,'--*c','LineWidth',3,'MarkerIndices',1:15:length(subsofsolntheta))
plot(t,Voltage,'--gv','LineWidth',3,'MarkerIndices',1:15:length(subsofsolntheta))
title('Forcing function, theta(t), thetadot(t), Power, Current, & Voltage (rectified) vs. t');
xlim([0 20]);
xlabel('t');
ylabel({'Forcing function, theta(t) (in radians), thetadot(t) (in radians/sec)';'Power (mW), Current (mA), Voltage (V)'});
legend('Forcingfunc','theta(t)','thetadot(t)','Power in mW','Current in mA','Voltage in V','Location','northeast');
hold off
figure
hold on
plot(t,powerinmW,'--r','LineWidth',2,'MarkerIndices',1:15:length(subsofsolntheta))
plot(t,energyinJ)
title('Power in mW and Energy in J vs. t');
xlim([0 20]);
xlabel('t');
ylabel({'Power in mW, Energy in mJ'});
legend('Power in mW','Energy in mJ','Location','northeast');
hold off