function p=taylor()
hold off
close all
while 1
f = menu('Välj funktion att Taylorutveckla','sin(x)','cos(x)','exp(x)',...
        'ln(1+x)','arctan(x)','(1+x)^1/2','1/(1+x)^1/2');

n1=-1;
a=0;
while 1 
    n=menu('Ange graden på det approximerande Taylorpolynomet',1,2,3,4,5,6,7,8,9,10,...
    11,12,13,14,15,16,17,18,19,20,25,30,35);
    
    if n==21
        n=25;
    elseif n==22;
        n=30;
    elseif n==23
        n=35;
    end
    
        
    n1=n1+1;
    if ~mod(n1,3)
        hold off
    end
    
    switch f
      case 1
         nt=n;
         n=2*ceil(n/2)-1;
         p=zeros(1,n+1);p(1+1)=1;
         for k=3:2:n
             p(k+1)=-p(k-2+1)./(k*(k-1));
         end
         x=linspace(a-12,a+12,600);
         y=sin(x);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         m=max(abs(y));axis([min(x),max(x),-m-m/3,m+m/3]),shg
         title('\bf Mclaurinutveckling av  f(x) = sin(x)','FontSize',12)
         n=nt;
      case 2
         nt=n; 
         n=2*ceil((n-1)/2);
         p=zeros(1,n+1);p(1)=1;
         for k=2:2:n
             p(k+1)=-p(k-1)./(k*(k-1));
         end
         x=linspace(a-12,a+12,400);
         y=cos(x);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         m=max(abs(y));axis([min(x),max(x),-m-m/3,m+m/3]),shg
         title('\bf Mclaurinutveckling  f(x) = cos(x)' ,'FontSize',12)
         n=nt;
     case 3
         p=zeros(1,n+1);p(1)=1;
         for k=1:n
             p(k+1)=p(k)./k;
         end
         x=linspace(a-1,a+3,400);
         y=exp(x);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         mx=max(y);mn=min(y);axis([min(x),max(x),mn-.2,mx+.5]),shg
         title('\bf Mclaurinutveckling f(x) = e^x ' ,'FontSize',12)
     case 4
         p=ones(1,n);p(2:2:n)=-1;p=p./[1:n];p=[0 p];
         x=linspace(-1+.1,2,400);
         y=log(1+x);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         mx=max(y);mn=min(y);axis([min(x),max(x),mn+.2,mx+.5]),shg
         title('\bf Mclaurinutveckling f(x) = ln(1+x)' ,'FontSize',12)
     case 5
         nt=n;
         n=2*ceil(n/2)-1;
         p=zeros(1,n);p(1:4:n)=1;p(3:4:n)=-1;p(1:2:n)=p(1:2:n)./[1:2:n];p=[0 p];
         x=linspace(-2,2,400);
         y=atan(x);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         m=max(abs(y));axis([min(x),max(x),-m-m/3,m+m/3]),shg
         title('\bf Mclaurinutveckling f(x) = arctan(x) k' ,'FontSize',12)
         n=nt;
     case 6
         p=zeros(1,n+1);p(1)=1;
         for k=1:n
             p(k+1)=(.5-k+1).*p(k)./k;
         end
         x=linspace(-1+.01,2,400);
         y=(1+x).^.5;
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         mx=max(y);mn=min(y);axis([min(x),max(x),mn-.2,mx+.5]),shg
         title('\bf Mclaurinutveckling f(x) = (1+x)^{1/2} ' ,'FontSize',12)
     case 7
         p=zeros(1,n+1);p(1)=1;
         for k=1:n
             p(k+1)=(-.5-k+1).*p(k)./k;
         end
         x=linspace(-1+.1,2,400);
         y=(1+x).^(-.5);
         plot(x,y,'--r','LineWidth',4),hold on,grid on
         mx=max(y);mn=min(y);axis([min(x),max(x),mn-.2,mx+.5]),shg
         title('\bf Mclaurinutveckling f(x) = (1+x)^{-1/2} ' ,'FontSize',12)
    end

    p=fliplr(p);
    
    plot(x,polyval(p,x),'LineWidth',3,'Color',[rand rand rand])
    gtext(['\bf n = ' num2str(n)], 'FontSize',11)
    m=menu('Vill du fortsätta ?','Ja','Nej');
    if m==2
        break
    end
    
end
    m2=menu('Ny funktion ?', 'Ja','Nej)');
    if m2==2
        break
    end
end

p=fliplr(p);