function [error,tirax,tiray,tirat] = escena(v,alfa,pinta) if nargin<3, pinta = true; end inicio = [0 0]; papelera = [120 0]; % Escenario 3 amort = @(t) 8*exp(-(t-20).^2/100); V1x = @(t,x,y) amort(sqrt(0.1*x.^2+y.^2)).*(x.*cos(2*pi*t)+y.*sin(2*pi*t)); V1y = @(t,x,y) amort(sqrt(0.1*x.^2+y.^2)).*(-x.*sin(2*pi*t)+y.*cos(2*pi*t)); V2x = @(t,x,y) 0; V2y = @(t,x,y) 0; V3x = @(t,x,y) 0; V3y = @(t,x,y) 0; Vx = @(t,x,y) V1x(t,x,y)+V2x(t,x,y)+V3x(t,x,y); Vy = @(t,x,y) V1y(t,x,y)+V2y(t,x,y)+V3y(t,x,y); k = 0.5; g = 9.8; F = @(t,x,y) [x(2) -k*x(2)+Vx(t,x(1),y(1)); y(2) -g-k*y(2)+Vy(t,x(1),y(1))]; X = 0; Y = 0; dX = v*cos(alfa*pi/180); dY = v*sin(alfa*pi/180); X = [0 dX; 0 dY]; tirax = 0; tiray = 0; tirat = 0; h = 0.02; t = 0; while Y(1)>=0 nt = t+h; NX = X+h*F(nt,X(1,:),X(2,:)); if NX(2,1)<0, if X(2,1)<1e-5; break; else, h = h/2; continue; end; end X = NX; t = nt; tirat = [tirat t]; tirax = [tirax, X(1,1)]; tiray = [tiray, X(2,1)]; end sol = [tirax(end) tiray(end)]; error = sol(1)-papelera(1); if pinta, dibuja(tirax,tiray,tirat,papelera,inicio,Vx,Vy); end end function dibuja(tirax,tiray,tirat,papelera,inicio,Vx,Vy) figure(1), clf xm = min(tirax); xM = max(tirax); ym = 0; yM = max(tiray); xm = 5*round(xm/5-.5); xM = max(5*round(xM/5+.5),papelera(1)+5); ym = 0; yM = 5*round(yM/5+.5); longitud = 50; Long = (tirax(2:end)-tirax(1:end-1)).^2+(tiray(2:end)-tiray(1:end-1)).^2; Long = [0 sqrt(Long)]; Long = cumsum(Long); hold on ptl = plot(inicio(1),inicio(2),'c-');%,'linewidth',1); ptestela = plot(inicio(1),inicio(2),'-');%,'linewidth',2); pt = plot(inicio(1),inicio(2),'g.','markersize',25); plot(papelera(1),papelera(2),'ro'); [mx,my] = meshgrid(linspace(xm,xM,25),linspace(ym,yM,25)); qv = quiver(mx,my,Vx(0,mx,my),Vy(0,mx,my)); set(qv,'color',[1 .8 0]); axis([xm xM ym yM]) t = 0; tic; while t