2D Plot changing in a for loop - Want to plot in 3D
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello! Newish to MATLAB and struggling with what is probably something simple. I've got a function 'v(x)' that changes in a for loop. I'd like to make a 3D plot of v(x,m), where m is the index of the loop. I didn't write this whole code - I've made some adjustments but most is taken from this paper: https://www.researchgate.net/publication/281441538_An_introduction_to_the_Split_Step_Fourier_Method_using_MATLAB. (Remember, always cite your sources!)
For reference, I've got:
N = 512; % Number of Fourier modes
dt = .001; % Time step
tfinal = 10; % Final time
M = round(tfinal./dt); % Total number of time steps
L = 50; % Space period
h = L/N; % Space step
n =( -N/2:1:N/2-1); % Indices
x = n*h; % Grid points
u = exp(1i*x).*sech(x); % Intial condition
k = 2*n*pi/L; % Wavenumbers.
for m = 1:1:M % Start time loop
u = exp(dt*1i*(abs(u).*abs(u))).*u; % Solve non-linear part of NLSE
c = fftshift(fft(u)); % Take Fourier transform
c = exp(-dt*1i*k.*k/2).*c; % Advance in Fourier space
u = ifft(fftshift(c)); % Return to Physical Space
v = sqrt(abs(u).*abs(u)); % Normalize NLSE Solution
plot(x,v);
hold on
end
The 2D plot this generates is basically what I'm looking for - I just need to spread it out in the 'm' dimension. Any help is appreciated!
0 commentaires
Réponses (1)
Voss
le 19 Nov 2022
Something like this?
N = 512; % Number of Fourier modes
dt = .001; % Time step
tfinal = 10; % Final time
M = round(tfinal./dt); % Total number of time steps
L = 50; % Space period
h = L/N; % Space step
n =( -N/2:1:N/2-1); % Indices
x = n*h; % Grid points
u = exp(1i*x).*sech(x); % Initial condition
k = 2*n*pi/L; % Wavenumbers.
v_all = zeros(N,M);
for m = 1:M % Start time loop
u = exp(dt*1i*(abs(u).*abs(u))).*u; % Solve non-linear part of NLSE
c = fftshift(fft(u)); % Take Fourier transform
c = exp(-dt*1i*k.*k/2).*c; % Advance in Fourier space
u = ifft(fftshift(c)); % Return to Physical Space
v = sqrt(abs(u).*abs(u)); % Normalize NLSE Solution
% plot(x,v);
% hold on
v_all(:,m) = v;
end
surf(v_all,'EdgeColor','none')
xlim([1 M])
ylim([1 N])
0 commentaires
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!