for i=1:11 h(i)=0; end

for i=1:10 sig(i)=10; end
for i=11:20 sig(i)=20; end
a1=-2
b1=2
noi1 = floor(a1 + (b1+1-a1)*rand(20,1));
sig=sig+noi1

a1 = -10;
b1 = 10;
noise = floor(a1 + (b1+1-a1)*rand(200,1));

signal(1:20)=100;
signal(21:50)=20;
signal(51:70)=200;
signal(71:90)=130;
signal(91:130)=50;
signal(131:150)=150;
signal(151:175)=70;
signal(176:200)=175;
signal = noise+signal;


// ex2forward(h,0.5,500,20)
// ex2forward(h,0.5,500,21)
// ex2forward(h,0.5,500,10)

function u=ex2forward(f,tau,T,a)
  // computes a minimiser for the given energy functional 
  // f: initial signal
  // tau: time step size
  // T: Number of iterations
  
  N=length(f) // compute length of signal
  
  //set signal to 0 and the boundary values u(1)=10 and u(11)=20
  u(1:N) = 0 
  u(1)=10
  u(N)=20
  //u(N+1)=a
  //compute helper signal
  temp=u
  for t=1:T
    //perform iteration
    temp(1)=u(1)
    for i=2:(N-2)
      temp(i)=u(i) - tau*((u(i+2)-2*u(i+1)+u(i)))
    end      
    temp(N-1)=u(N-1)-tau*((a-2*u(N)+u(N-1)))
    temp(N)= u(N)
    // update u for next iteration
    u=temp  
  end  
  plot(u)
endfunction


// ex2backward(h,0.5,500,20)
// ex2backward(h,0.5,500,21)
// ex2backward(h,0.5,500,10)

function u=ex2backward(f,tau,T,a)
  // computes a minimiser for the given energy functional 
  // f: initial signal
  // tau: time step size
  // T: Number of iterations
  
  N=length(f) // compute length of signal
  
  //set signal to 0 and the boundary values u(1)=10 and u(11)=20
  u(1:N) = 0 
  u(1)=10
  u(N)=20
  //compute helper signal
  temp=u
  for t=1:T
    //perform iteration
    temp(1)=u(1)
    temp(2)=u(2) - tau*(u(2)-2*u(1)+a)
    for i=3:(N-1)
      temp(i)=u(i) - tau*((u(i)-2*u(i-1)+u(i-2)))
    end      
    temp(N)= u(N)
    // update u for next iteration
    u=temp  
  end  
  plot(u)
endfunction

// ex2central(h,0.5,1000,10,20)
// ex2central(h,0.5,1000,9,21)
// ex2forward(h,0.5,1000,20,20)

function u=ex2central(f,tau,T,a,b)
  // computes a minimiser for the given energy functional 
  // f: initial signal
  // tau: time step size
  // T: Number of iterations
  
  N=length(f) // compute length of signal
  
  //set signal to 0 and the boundary values u(1)=10 and u(11)=20
  u(1:N) = 0 
  u(1)=10
  u(N)=20
  //compute helper signal
  temp=u
  for t=1:T
    //perform iteration
    temp(1)=u(1)
    temp(2)=u(2) + tau*(u(4)-2*u(2)+a)/4
    for i=3:(N-2)
      temp(i)=u(i) + tau*((u(i-2)-2*u(i)+u(i+2)))/4
    end      
    temp(N-1)=u(N-1)+tau*(u(N-3)-2*u(N-1)+b)/4
    temp(N)= u(N)
    // update u for next iteration
    u=temp  
  end  
  plot(u)
endfunction

//ex2standard(h,0.5,1000)

function u=ex2standard(f,tau,T)
  // computes a minimiser for the given energy functional 
  // f: initial signal
  // tau: time step size
  // T: Number of iterations
  
  N=length(f) // compute length of signal
  
  //set signal to 0 and the boundary values u(1)=10 and u(11)=20
  u(1:N) = 0 
  u(1)=10
  u(N)=20
  
  //compute helper signal
  temp=u
  for t=1:T
    //perform iteration
    
    // Insert your iterative scheme here!
    temp(1)=u(1)
    for i=2:(N-1)
      temp(i)=u(i) + tau*((u(i-1)-2*u(i)+u(i+1)))
    end      
    temp(N)= u(N)  
    
    // update u for next iteration
    u=temp  
  end  
endfunction


  
  // [q1,q2]=ex3(1,1,1,signal/255,0.1,1000)
  function [u,v]=ex3(alpha,betaa,gama,f,tau,T)
  N=length(f)
  u=f
  v(1:N)=1
  tempu=u
  tempv=v
  dx=1
  for i=1:T  
      tempu(1) = u(1) - tau * (betaa*(u(1)-f(1)) -v(1)^2*(u(2)-u(1)) )
      tempv(1) = v(N) - tau * (v(1) * (u(2)-u(1))^2 -alpha*(v(2)-v(1))-+alpha/(4*gama)*(1-v(1)))
      for j=2:N-1 
      tempu(j) = u(j) - tau * (betaa*(u(j)-f(j)) -v(j)^2*(u(j+1)-u(j)) + v(j-1)*(u(j)-u(j-1)))
      tempv(j) = v(j) - tau * (v(j) * (u(j+1)-u(j))^2 -alpha*(v(j+1)-v(j)) + alpha * (v(j)-v(j-1))-alpha/(4*gama)*(1-v(j)))
    end
      tempu(N) = u(N) - tau * (betaa*(u(N)-f(N)) + v(N-1)*(u(N)-u(N-1)))
      tempv(N) = v(N) - tau * (alpha * (v(N)-v(N-1))-alpha/(4*gama)*(1-v(N)))
    u=tempu
    v=tempv
  end  
    
  endfunction
  
  
  function g=funct(x)
g=(x-1)*(x-1)*(x+2)*(x+3)
//
//   g= x^4
endfunction

function a=goldsection(a,b)
  k = 0
  tau =(sqrt(5)-1)/2
  
  x=b-tau*(b-a)
  y=a+tau*(b-a)
  
  f_a = funct(a)
  f_b = funct(b)
  f_x = funct(x)
  f_y = funct(y)
  
  while ( abs(b-a) > 0.000001)
    if (f_x > f_y)
      a = x
      b = b
      x = y
      y = a + tau*(b-a)
      f_a = f_x
      f_b = f_b
      f_x = f_y
      f_y = funct(y)
  else
      a = a
      b = y
      y = x
      x = b - tau*(b-a)
      f_a = f_a
      f_b = f_y
      f_y = f_x
      f_x = funct(x)
end
k=k+1
  end
          printf("Iterationen: %i\n",k)
endfunction  



