Trinh @ Bath

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision Both sides next revision
vpde_lecture25 [2020/03/31 08:26]
trinh
vpde_lecture25 [2020/03/31 08:49]
trinh
Line 39: Line 39:
 u_n(x, t) = \sin\left(nx\right) \left[ A_n \cos(nct) + B_n \sin(nct)\right] u_n(x, t) = \sin\left(nx\right) \left[ A_n \cos(nct) + B_n \sin(nct)\right]
 $$ $$
 +
 +<Code:Matlab linenums:1 |Demonstration of the modes>
 +% Code for MA20223 30 Mar 2020
 +clear
 +close all
 +
 +% Length and time
 +L = pi; T = 2*pi/(c*n);
 +
 +% Function
 +n = 2; c = 1;
 +un = @(x,t) sin(n*x/L).*cos(n*c*t/L);
 +
 +% Make vectors for space and time
 +x = linspace(0, pi, 50); t = linspace(0, 2*T, 50);
 +
 +% Create a mesh of x vs. t
 +[X,T] = meshgrid(x,t);
 +
 +% Matrix of U values to imagine the surface
 +U = sin(n*X).*cos(n*T);
 +
 +figure(1); subplot(1,2,1); xlabel('x'); ylabel('u');
 +
 +subplot(1,2,2);
 +% Plot the surface and make it pretty
 +s = surf(X,T,U); set(s, 'EdgeColor', 'none', 'FaceColor', 'interp');
 +view([-48, 17]); xlabel('x'); ylabel('t'); zlabel('u');
 +hold on
 +
 +% Plot an animation in time
 +for j = 1:length(t)    
 +    tt = t(j); uu = un(x,tt);
 +    
 +    subplot(1,2,1);
 +    plot(x, uu);   title(['t = ', num2str(tt)]);
 +    ylim([-1,1]); xlim([0, pi]);
 +    
 +    subplot(1,2,2);
 +    if j == 1
 +        p = plot3(x, tt*ones(size(x)), un(x, t(j)), 'LineWidth', 3);
 +        pause;
 +    else
 +        set(p, 'XData', x, 'YData', tt*ones(size(x)), 'ZData', un(x,t(j)));
 +    end
 +    
 +    drawnow
 +    shg
 +end
 +</Code>