% ========== % Apartado I % ========== % % * * * Ejemplo 1 * * * % Definición de variables y funciones simbólicas syms x real; fs=x*cos(x); dfs=diff(fs),subs(dfs,pi/2) % Derivación simbólica h=0.1;F=subs(fs,pi/2+[0,h]) ;diff(F)/h fn=inline(vectorize(fs)); h=0.1;F=fn(pi/2+[0,h]) ;diff(F)/h % %% * * * EJERCICIO 1 * * * % X=linspace(0,2);F=subs(fs,X);DF=subs(dfs,X);nx=length(X); h=[0.25,0.05];nh=length(h);Y=zeros(nh,nx);DY=Y; for i=1:nh; Y(i,:)=(fn(X+h(i))-F)/h(i);DY(i,:)=Y(i,:)-DF;end subplot(2,1,1);plot(X,[DF;Y]);legend('analitica','h=0.1','h=0.01','Location','B');title(char(dfs));grid on subplot(2,1,2);plot(X,DY);legend('h=0.1','h=0.01','Location','B');title(char(dfs));grid on % %% * * * EJERCICIO 2 * * * % X=[0,1,2];F=subs(fs,X);DF=subs(dfs,X);nx=length(X); h=logspace(0,-20);nh=length(h);Y=zeros(nh,nx);DY=Y; for i=1:nx; Y(:,i)=(fn(X(i)+h)-F(i))./h;DY(:,i)=Y(:,i)-DF(i);end subplot(2,1,1);semilogx(h,Y);legend('x=0','x=1','x=2','Location','B');title(char(dfs));grid on subplot(2,1,2);semilogx(h,DY);legend('x=0','x=1','x=2','Location','B');title(char(dfs));grid on %% * * * EJERCICIO 3 * * * % X=linspace(0,2);nx=length(X);h=0.2; F=zeros(5,nx);for i=1:5;F(i,:)=subs(fs,X+(i-2)*h);end DF=subs(dfs,X); Y=zeros(4,nx);DY=Y; Y(1,:)=(-3*F(2,:)+4*F(3,:)-F(4,:))/2/h;DY(1,:)=Y(1,:)-DF; Y(2,:)=(-2*F(1,:)-3*F(2,:)+6*F(3,:)-F(4,:))/6/h;DY(2,:)=Y(2,:)-DF; Y(3,:)=(-F(1,:)+F(3,:))/2/h;DY(3,:)=Y(3,:)-DF; Y(4,:)=(-11*F(2,:)+18*F(3,:)-9*F(4,:)+2*F(5,:))/6/h;DY(4,:)=Y(4,:)-DF; subplot(2,1,1);plot(X,[DF;Y]);legend('analitica','f1','f2','f3','f4','Location','BO');title(char(dfs));grid on subplot(2,1,2);plot(X,DY);legend('f1','f2','f3','f4','Location','BO');title(char(dfs));grid on % =========== % Apartado II % =========== % % * * * Ejemplo 1 * * * a=0;b=1; syms x real; fs=1/cos(x); F=int(fs);subs(F,b)-subs(F,a) F=int('1/cos(x)');subs(F,b)-subs(F,a) int(fs,a,b) % fn=inline(vectorize(fs)); quad(fn,a,b,1e-6) quadl(fn,a,b,1e-6) % * * * Ejemplo 2 * * * syms x y real; fs2=x^2+y^2; F=int(int(fs2,'y',0,1-x),'x',0,1) % * * * Ejemplo 3 * * * n=10;X=linspace(a,b,n+1);h=X(2)-X(1); F=fn(X);valor=(sum(2*F)-F(1)-F(end))*h/2 % % Romberg a=0;b=1;h=(b-a)/1; X=[a,b]; F=fn(X);valor=(2*sum(F)-F(1)-F(end))*h/2 N=1:40; for k=N n=2^k;h=h/2;X=a+h:2*h:b-h; F=fn(X);valor=valor/2+sum(F)*h; disp(['Equiespaciado:',num2str(h),' Integral:',num2str(valor)]); end % %% * * * EJERCICIO 4 * * * % clear all close all syms x real fs=sin(x)/x; fn=inline(vectorize(fs)); a=1;b=5;h=(b-a)/1; N=0:20; R=zeros(size(N)); X=[a,b]; F=fn(X);R(1)=sum(F)*h/2; for k=2:length(N) n=2^N(k);h=h/2;X=a+h:2*h:b-h; F=fn(X);R(k)=R(k-1)/2+sum(F)*h; disp(['Equiespaciado:',num2str(h),' Integral:',num2str(R(k))]); end SE=double(int(fs,a,b)); subplot(2,1,1);semilogx(2.^N,R); title(char(fs));xlabel('Intervalos');ylabel('Integral') subplot(2,1,2);loglog(2.^N,abs(SE-R)); title(char(fs));xlabel('Intervalos');ylabel('Error') %% * * * EJERCICIO 5 * * * % clear all close all syms x real fs=1/cos(x); fn=inline(vectorize(fs)); a=0;b=1; n=10;X=linspace(a,b,2*n+1);h=X(2)-X(1); F=fn(X);valor=sum([F(1),4*F(2:2:end),2*F(3:2:end-1),F(end)])*h/3