%% exemplo 1
figure('Position',[500 500 550 350])
Tb = 400;
heat = @(x,y) [y(2);-0.05*(200-y(1))];

% primeiro tiro
s = 0;
[t,y]=ode45(heat,[0 10],[300,s]); 
Tb1=y(end,1);
plot(t,y(:,1),'--','LineWidth',3); hold on;

% segundo tiro
heat_ = @(x,y) [y(2);-0.05*(-y(1))];
s = 1;
[t,y]=ode45(heat_,[0 10],[0,s]); 
Tb2=y(end,1);
plot(t,y(:,1),'--','LineWidth',3);

% tiro fatal
s = (Tb-Tb1)/Tb2;
[t,y]=ode45(heat,[0 10],[300,s]); 
plot(t,y(:,1),'k','LineWidth',3);

% format
axis([0 10 0 700]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',16)
str='$x [m]$';
xlabel(str,'interpreter','latex','FontSize',20);
str='Temperatura T [K]';
ylabel(str,'interpreter','latex','FontSize',20);

h = legend('primeiro','segundo','solu\c{c}\~{a}o');
set(h,'Interpreter','latex','FontSize',16,'Location','northwest')

tightfig;


%% exemplo 2
figure('Position',[500 500 550 350])
beta = 0;
alpha = 1;
a = 0; 
b = 1;
f = @(x,y) [y(2);-4*y(2)-13*y(1)];

% primeiro tiro
s = 0;
[t,y]=ode45(f,[a b],[alpha,s]); 
beta1=y(end,1);
plot(t,y(:,1),'--','LineWidth',3); hold on;

% segundo tiro
s = 1;
[t,y]=ode45(f,[a b],[0,s]); 
beta2=y(length(y));
plot(t,y(:,1),'--','LineWidth',3);

% tiro fatal
s = (beta-beta1)/beta2;

[t,y]=ode45(f,[a b],[alpha,s]); 

plot(t,y(:,1),'k','LineWidth',3);

% format
axis([0 1 -.5 3.5]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',16)
str='$t$';
xlabel(str,'interpreter','latex','FontSize',20);
str='x';
ylabel(str,'interpreter','latex','FontSize',20);

h = legend('primeiro','segundo','solu\c{c}\~{a}o');
set(h,'Interpreter','latex','FontSize',16,'Location','northeast')

tightfig;

%% exemplo 3
figure('Position',[500 500 550 350])
beta = 0;
alpha = 0;
a = 0; 
b = 1;
f = @(x,y) [y(2);2*y(1)+x*(5-x)];

% primeiro tiro
s = 0;
[t,y]=ode45(f,[a b],[alpha,s]); 
beta1=y(end,1);
plot(t,y(:,1),'--','LineWidth',3); hold on;

% segundo tiro
f_ = @(x,y) [y(2);2*y(1)];
s = 1;
[t,y]=ode45(f_,[a b],[0,s]); 
beta2=y(length(y));
plot(t,y(:,1),'--','LineWidth',3);

% tiro fatal
s = (beta-beta1)/beta2;

[t,y]=ode45(f,[a b],[alpha,s]); 

plot(t,y(:,1),'k','LineWidth',3);

% format
axis([0 1 -.5 3.5]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',16)
str='$t$';
xlabel(str,'interpreter','latex','FontSize',20);
str='x';
ylabel(str,'interpreter','latex','FontSize',20);

h = legend('primeiro','segundo','solu\c{c}\~{a}o');
set(h,'Interpreter','latex','FontSize',16,'Location','northeast')

tightfig;
%% y'' + y = 0, y(0) = 0, y(1) = 1
opts=odeset('RelTol',1e-9,'AbsTol',1e-11);
figure('Position',[500 500 1500 300])
beta = 1;
alpha = 0;
a = 0; 
b = pi;
f = @(x,y) [y(2);-y(1)];

% primeiro tiro
s = 0;
[t,y]=ode45(f,[a b],[alpha,s],opts); 
beta1=y(end,1);
subplot(1,3,1); plot(t,y(:,1),'--','LineWidth',3); hold on;
% format
axis([0 pi -1 1]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',20)
str='$t$';
xlabel(str,'interpreter','latex','FontSize',24);
str='x';
ylabel(str,'interpreter','latex','FontSize',24);
str='primeiro tiro';
title(str,'interpreter','latex','FontSize',24);

% segundo tiro
s = 1;
[t,y]=ode45(f,[a b],[0,s],opts); 
beta2=y(length(y));
subplot(1,3,2); plot(t,y(:,1),'--','LineWidth',3);
% format
axis([0 pi 0 1]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',20)
str='$t$';
xlabel(str,'interpreter','latex','FontSize',24);
str='x';
ylabel(str,'interpreter','latex','FontSize',24);
str='segundo tiro';
title(str,'interpreter','latex','FontSize',24);

% tiro fatal
s = (beta-beta1)/beta2;
[t,y]=ode45(f,[a b],[alpha,s],opts); 
subplot(1,3,3); plot(t,y(:,1),'k','LineWidth',3);
% format
axis([0 pi -8e10 0]);
ax = gca;
set(ax,'TickLabelInterpreter','latex');
ax.TickLength = [0.02 0.02];
set(gca,'fontsize',20)
str='$t$';
xlabel(str,'interpreter','latex','FontSize',24);
str='x';
ylabel(str,'interpreter','latex','FontSize',24);
str='resposta numerica';
title(str,'interpreter','latex','FontSize',24);

tightfig;