%% examples class 6_10_2017
%%
clear all 
close all
clc

N=1000;
x=linspace(1,1000,N);
f=(1+1./x).^x;

figure()
subplot(3,1,1)
hold on
plot(x,f)
plot(x,exp(1)*ones(length(x),1),'r--')
xlabel('x')
ylabel('f(x)')
hold off
subplot(3,1,2)
hold on
plot(1./x,f)
plot(1./x,exp(1)*ones(length(x),1),'r--')
xlabel('x^{-1}')
ylabel('f(x)')
hold off
subplot(3,1,3)
hold on
plot(log(x),log(f))
plot(log(x),log(exp(1)*ones(length(x),1)),'r--')
xlabel('log(x)')
ylabel('log(f(x))')
hold off

%%
clear all
close all
clc

format long
exact=exp(1)

N=5;
x=(0:1:N);
numerical(5)=sum(1./factorial(x));


N=6;
x=(0:1:N);
numerical(6)=sum(1./factorial(x));

N=7;
x=(0:1:N);
numerical(7)=sum(1./factorial(x));

% in order to see the asymptotic behaviour we need loops!!!!! (next)

%% loop for
clear all
clc

N=10;
for i=1:N
   i 
   j=2*i
end

%%
clear all
clc

N=10;
for i=1:N
   x(i)=i;
   % very different from
   xp=i;
   xp2=i+sin(i);
   y(i)=log(i);
end

x
xp
y
%% exercise 14.0.28
clear all
clc
M=[1 9 2 23; 2 8 13 2; -2 9 7 1; 45 2 82 -11];
M
% one way is by double loop (not the most efficient)
M2=M;
for i=2:4
   for j=1:4
       M2(i,j)=M(i,j)-M(i-1,j);
   end
end
M2

% more efficient would be with 1 loop only:
M3(1,:)=M(1,:);

for i=2:4
   M3(i,:)=M(i,:)-M(i-1,:);
end
M3

%% exercise 14.0.29
clear all
clc

x=linspace(0,2,20);

f=x.^2
%or f=x.*x
% i could define it equivalently but less efficiently via a loop
for k=1:length(x)
    f2(k)=x(k)^2;
end
f2

%% terminating loops
clear all
clc
% for example you want to terminate a loop when a condition is met
for i=1:10
    i
    x(i)=log(i)
    if(log(i)>1.6)
        break
    end
end

%N.B. this is done with loop while!!!!!! but first we need to understand
%how  the condition if works

%% conditions (if)
clear all
clc

x=input('enter value x')
y=input('enter value y')

if x>y
    disp('x is larger than y')
    i=1
elseif x<y
    disp('x is smaller than y')
    i=2
else
    disp('x is equal to y')
    i=3
end
    
%% exercise 15.0.34
clear all
close all
clc

N=500;
x=linspace(-2*pi,2*pi,N);
f=sin(x);

g=f;
for i=1:N
   if f(i)<0
       g(i)=0;
   end
end

figure()
hold on
plot(x,f,'b--')
plot(x,g,'r','Linewidth',2)
xlabel('x')
legend('f(x)=sin(x)','half-wave rectified','Location','SouthWest')
hold off

%% in the next class we see the while loop
%% DERIVATIVES
clear all
close all
clc
format short
x0=2;

df_exct=2*x0+5

dx=0.1;


% f=x^2+5*x
% first order derivative; error=O(dx)

f1=x0^2+5*x0;
f2=(x0+dx)^2+5*(x0+dx);
f3=(x0-dx)^2+5*(x0-dx);
%( see next introducing anonimous functions )

df1_x0right=(f2-f1)/dx
df1_x0left=(f1-f3)/(-dx)

% second order derivative; error=O(dx^2) so no error for a polynom of order
% 2!!!!

df_2_x0center=(f2-f3)/(2*dx)

%% same exercise with anonymous functions
clear all
clc

f=@(x)(x.^2+5*x);

f(1.1)

% N.B. i have used .^ because i can in this way use f also for vectors
% as in the following
a=[1 2]
f(a)

%%
clear all
clc

x0=2;
df_exct=2*x0+5
dx=0.1;

f=@(x)(x.^2+5*x);

% first order derivatives
df1_x0right=(f(x0+dx)-f(x0))/dx
df1_x0left=(f(x0-dx)-f(x0))/(-dx)

% second order derivative (here exact)

df_2_x0center=(f(x0+dx)-f(x0-dx))/(2*dx)

%% exercise 6.0.11 with study of covergence
clear all
close all
clc

f=@(x)(x.^3+5*x.^2);

% Taylor expansion
x0=2;
taylor1=@(x)(f(x0)+(3*x0^2+10*x0)*(x-x0))
taylor2=@(x)(f(x0)+(3*x0^2+10*x0)*(x-x0)+0.5*(6*x0+10)*(x-x0).^2)

% plot
x=linspace(-3,3,100);
figure()
subplot(1,2,1)
hold on
plot(x,f(x))
plot(x,taylor1(x),'r-')
plot(x0,f(x0),'sg')
legend('f(x)','1^{st} order Taylor expansion','Location','Best')
plot(x0*ones(10,1),linspace(-10,f(x0),10),'g--')
plot(linspace(-3,x0,10),f(x0)*ones(10,1),'g--')
axis([-3,3,-10,80])
hold off
subplot(1,2,2)
hold on
plot(x,f(x))
plot(x,taylor2(x),'r-')
plot(x0,f(x0),'sg')
legend('f(x)','2^{nd} order Taylor expansion','Location','Best')
plot(x0*ones(10,1),linspace(-10,f(x0),10),'g--')
plot(linspace(-3,x0,10),f(x0)*ones(10,1),'g--')
axis([-3,3,-10,80])
hold off

% numerical values of the derivative at x0
N=100;
dx=linspace(10^(-4),1,N);

exact=3*x0^2+10*x0;

f=@(x)(x.^3+5*x.^2);

df1_right=(f(x0+dx)-f(x0))./(dx);
df1_left=(f(x0-dx)-f(x0))./(-dx);
df2=(f(x0+dx)-f(x0-dx))./(2*dx);

figure()
hold on
plot(dx,df1_right)
plot(dx,df1_left)
plot(dx,df2)
plot(dx,exact*ones(N,1),'k--')
legend('1^{st} order right','1^{st} order left', '2^{nd} order')
xlabel('dx')
hold off