- Fixed the wrong assignment of x.
- Added missing semicolons at the end of each line to suppress unnecessary output.
- Renamed the variable “x” in the initial condition section to “x_plot” to avoid conflicts with the x variable used for grid points.
- Changed the range of “x_plot” from 0:.001:3 to x to match the grid points used in the rest of the code.
- Renamed the variable “u” in the initial condition section to “u_func” to avoid conflicts with the “U“ variable used for the wave solution.
I'm trying to write a MATLAB code to solve the first order 1-D wave equation (transport equation) using the Euler method
4 次查看(过去 30 天)
显示 更早的评论
% This program describes a moving 1-D wave
% using the finite difference method
clc
close all;
%%
% Initialization
Nx= 480; % x-Grid
dx= 3/480; % Step size
x(:,1)= (0:Nx-1)*dx;
f= 125.7; % Wave number
U(:,1)= -16*(x(:,1)-1/2).^2
% U(:,1) = exp(-16*(x(:,1)-1/2).^2);
U(:,1)= exp(U(:,1));
U(:,1)= U(:,1).*sin(f*x(:,1))
%U(:,1)= exp(-16*(x(:,1)-1/2).^2)%sin*(f*x(:,1))
%U(:,1)= U(:,1).*sin(f*x(:,1))
mpx= (Nx+1)/2; % Mid point of x-axis
% (Mid point of 1 to 3= 2 here)
T= 10; % Total no. of time step
f= 125.7; % Wave number
dt= 0.000625; % time-step
t(:,1)= (0:T-1)*dt;
v= 1; % wave velocity
c=v*(dt/dx); % CFL condition
s1= floor(T/f);
%%
% Initial condition
x=0:.001:3;
u=@(t)exp(-16*(x-(.75*t+.5)).^2).*sin(125.7*x);
plot(x,u(0))
figure
plot(x,u(2))
0 个评论
回答(1 个)
Gyan Vaibhav
2023-10-20
编辑:Gyan Vaibhav
2023-10-20
Hi Ayushman,
It seems like you have already written some MATLAB code to solve the 1-D wave equation using the Euler method. However, I noticed a few issues and inconsistencies in your code. Here's the corrected version:
clc
close all;
% Initialization
Nx = 480; % x-Grid
dx = 3 / Nx; % Step size
x = (0:Nx-1) * dx;
f = 125.7; % Wave number
U = -16 * (x - 1/2).^2;
U = exp(U);
U = U .* sin(f * x);
mpx = (Nx + 1) / 2; % Mid point of x-axis
T = 10; % Total number of time steps
dt = 0.000625; % time-step
t = (0:T-1) * dt;
v = 1; % wave velocity
c = v * (dt / dx); % CFL condition
s1 = floor(T / f);
% Initial condition
x_plot = x; % Use the same grid points as x
u_func = @(t) exp(-16 * (x_plot - (.75 * t + .5)).^2) .* sin(125.7 * x_plot);
plot(x_plot, u_func(0));
figure;
plot(x_plot, u_func(2));
Here are the changes I made to your code:
These changes should fix the inconsistencies and make the code run without errors.
Also, the plots using above code do match your given outputs in the pdf file.
I hope these suggestions help you resolve the issue.
Best regards,
Gyan
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spatial Search 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!