I have the function and the 1D wave equation i.e. du/dt + c.du/dx=0. I have to solve it using Euler's time step series.
2 ビュー (過去 30 日間)
古いコメントを表示
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/949909/image.png)
% 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))
%% Euler's method
h= 0.5;
x= 0:h:3; %the range of x
a= 1;
dx= 3/480;
dt= 0.000625;
f= 125.7;
u= zeros(size(x));%allocate the result u
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_prev(i)= U(:,1);
%u(1)= (1); %the initial u value
n= numel(u);%the number of u values
%loop to solve the differential equation
for i=1:n-1
u(i)= exp((-16*(x-.5)).^2).*sin(125.7*x);
u(i)=u_prev(i)-((a*(dtdx)).*(u_prev(i)-u_prev(i-1)));
u(i)=u_prev(i)+(dudt)*h;
end
%%
回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!