Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear
- clc
- clf
- %Number of Fourier coefficients
- N=50;
- %Wave velocity
- v=1.25;
- %Length
- L=1;
- %Width of pulse, 2w
- w = 0.2;
- %height of pulse
- h=0.4;
- %X axis
- x=linspace(0,L,100);
- y=zeros(1,length(x));
- %framerate
- fps=45;
- %Simulation time
- simtime=7;
- T=linspace(0,simtime,fps*simtime);
- %Shift position of initial peak
- offset=0.0;
- %Placeholder for Fourier and other coefficients
- a_n = zeros(1,N);
- F_n = zeros(1,N);
- lambda_n = zeros(1,N);
- %Solution matrix
- soln = zeros(length(T),length(x));
- %Magnitude of constant forcing, set to 0 for unforced version of problem
- g=-9.8;
- %Damping, set to 0 for undamped version of problem
- b=0;
- %Compute the Fourier coefficients
- for n=1:N
- bound1=0.5*L-w-offset;
- bound2=0.5*L-offset;
- bound3=0.5*L+w-offset;
- f1= @(s)((2/L)*sin(n*pi*s/L).*((h/w)*((s+offset)-0.5*L)+h));
- f2= @(s)((2/L)*sin(n*pi*s/L).*(-(h/w)*((s+offset)-0.5*L)+h));
- A = integral(@(s)f1(s), bound1, bound2);
- B = integral(@(s)f2(s), bound2, bound3);
- a_n(n) = A+B;
- F_n(n) = 2*g*(1-(-1)^n)/(n*pi);
- lambda_n(n) = sqrt(b^2-n^2*pi^2*v^2/L^2);
- end
- % for n=1:N
- % y = y+a_n(n)*sin(n*pi*x/L);
- % end
- % plot(x,y)
- for k = 1:length(T)
- t=T(k);
- y=zeros(1,length(x));
- for n=1:N
- y = y + real((1/(2*lambda_n(n)))*(a_n(n)-L^2*F_n(n)/(n^2*pi^2*v^2))*exp(-b*t)*((lambda_n(n)-b)*exp(-lambda_n(n)*t)+(b+lambda_n(n))*exp(lambda_n(n)*t))+F_n(n)*L^2/(n^2*pi^2*v^2))*sin(n*pi*x/L);
- end
- soln(k,:)=y;
- end
- % mesh(x,T,soln)
- % xlabel('x');
- % ylabel('t');
- fig=figure;
- elapsed=0;
- nImages=length(T);
- for m = 1:nImages
- clf
- hold on
- plot(x,soln(m,:))
- hold off
- xlim([0 1])
- ylim([-2.25 0.5])
- title(['Elapsed time = ' num2str(T(m), '%.2f') ' seconds'])
- drawnow
- frame = getframe(fig);
- im{m} = frame2im(frame);
- end
- close
- filename = 'wave2.gif';
- for idx = 1:nImages
- [A,map] = rgb2ind(im{idx},256);
- if idx == 1;
- imwrite(A,map,filename,'gif','LoopCount',Inf,'DelayTime',1.5);
- elseif (idx > 1) && (idx < nImages);
- imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',0.04);
- elseif idx == nImages
- imwrite(A,map,filename,'gif','WriteMode','append','DelayTime',1.5);
- end
- end
- clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement