This shows you the differences between two versions of the page.
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 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/ | ||
+ | |||
+ | % 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, | ||
+ | |||
+ | % Matrix of U values to imagine the surface | ||
+ | U = sin(n*X).*cos(n*T); | ||
+ | |||
+ | figure(1); subplot(1, | ||
+ | |||
+ | subplot(1, | ||
+ | % Plot the surface and make it pretty | ||
+ | s = surf(X, | ||
+ | view([-48, 17]); xlabel(' | ||
+ | hold on | ||
+ | |||
+ | % Plot an animation in time | ||
+ | for j = 1: | ||
+ | tt = t(j); uu = un(x,tt); | ||
+ | | ||
+ | subplot(1, | ||
+ | plot(x, uu); | ||
+ | ylim([-1, | ||
+ | | ||
+ | subplot(1, | ||
+ | if j == 1 | ||
+ | p = plot3(x, tt*ones(size(x)), | ||
+ | pause; | ||
+ | else | ||
+ | set(p, ' | ||
+ | end | ||
+ | | ||
+ | drawnow | ||
+ | shg | ||
+ | end | ||
+ | </ |