Wave Equation Using Matlab and Finite Differencing.
조회 수: 39 (최근 30일)
이전 댓글 표시
I'm trying to solve a wave equation, , over and ,with boundary/initial conditions:
I'm relatively new to numerically solving PDEs, but I'm not entirely sure what I'm doing wrong here. My code yields that the string vibrates to a height on the order of units, which is obviously unreasonable given the initial values. The spatial (n) / time (m) intervals are 25 and 700 respectively for the final equation, however they are shortened to 15 each in order to help me diagnose the code.
Here's what I have so far:
clear all;close all;clc;
% Initialize Variables
n = 15; % spacial steps
m = 15; % time steps
Li = 0;
Lf = 2;
ti = 0;
tf = 50;
h = Lf/n;
k = tf/m;
u_left = 0;
u_right = 0;
u0 = @(x) -(x-1)^2 + 1;
ut0 = @(x) (x-1)^4 -1;
v = ut0;
c = sqrt(.25);
u = zeros(m+1,n+1);
% Boundary Conditions
u(:,1) = u_left;
u(:,n+1) = u_right;
% Initial Conditions
for i=2:n
u(1,i) = u0(i*h); % Dirichlet Condition
end
for i=2:n
u(2,i) = u0(i*h) + ut0(i*h)*k; % Niemann Condition, used to fill timestep 2
end
% Solve using Finite Differencing
for i=3:m
u(i,2:n) = c*k/h^2*u(i-1,1:n-1) + (1-2*c*k/h^2) * u(i-1,2:n) + c*k/h^2*u(i-1,3:n+1);
end
format long
u
surf(u)
댓글 수: 0
답변 (1개)
nick
2024년 4월 15일
Hi Tyler,
I found some errors in the equations for the initial condition and the finite difference method that you used to solve the wave equation above.
Instead of (ck/h^2), it should be ((ck/h)^2) in the finite difference method. Furthermore, it seems that there are certain terms missing in the finite difference equation. In the initial conditions, when using u0 and ut0, the input argument should correspond to (i-1) instead of (i). Kindly refer to the attached code below for your reference:
clear all;close all;clc;
%%
% Initialize Variables
n = 25; % spacial steps
Li = 0; %inital
Lf = 2; %final
h = Lf/n; %total number steps x
m = 700; % time steps
ti = 0; %initial
tf = 50; %final
k = tf/m; %total number steps time
u_left = 0;
u_right = 0;
u0 = @(x) -(x-1)^2 + 1; %u(x,0)
ut0 = @(x) (x-1)^4 -1; %d/dt(u(t)) @ (x,0)
v = ut0;
c = sqrt(.25);
u = zeros(m+1,n+1); %u(t,x)
%%
% Boundary Conditions
u(:,1) = u_left; %u(0,t) x(0)==u(:,1)
u(:,n+1) = u_right; %u(2,t) 2==n+1
%%
% Initial Conditions
for i=2:n
%i*h misses the first coordinate
u(1,i) = u0((i-1)*h); % Dirichlet Condition
end
for i=2:n
%i*h misses the first coordinate
u(2,i) = u0((i-1)*h) + ut0((i-1)*h)*k; % Niemann Condition, used to fill timestep 2
end
% Solve using Finite Differencing
r = (c*k/h)^2;
for i=3:m
%u(i,2:n) = c*k/h^2*u(i-1,1:n-1) + (1-2*c*k/h^2) * u(i-1,2:n) + c*k/h^2*u(i-1,3:n+1);
u(i,2:n) = 2*u(i-1,2:n) - u(i-2,2:n) + r*(u(i-1,3:n+1) - 2*u(i-1,2:n) + u(i-1,1:n-1));
end
format long
u
surf(u)
I hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Geometry and Mesh에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!