Sunday, 19 May 2013

Wave Equation Solution using Finite Difference Method in MATLAB

Problem:- solve the Wave equation







Copy and Paste the following code in MATLAB command window or Matlab Editor and press F5 or run.
See the result.

MATLAB program::


% To solve wave equation using finite difference method
% By antennatutorials.com
% phitt=phixx            0<x<1
% boundary conditions
% phi(0,t)=0=phi(1,t)    t>=0
% iniital conditions
% phi(x,0)=sin(pi*x)     0<x<1
% phit(x,0)=0            0<x<1

% define no. of steps for r =1
clc
x=0 :0.1 :1;
t=0:0.1:1;
N=10;
%define stepsize
dx=0.1;
dt=0.1;
phi=zeros(11,11);
%Boundary condition
phi(:,1)=0;
phi(:,11)=0;
phi(1,:)= sin(pi.*x);
%initial condition
for i=2:10
phi(2,i)=0.5*(phi(1,i+1) + phi (1,i-1));
end
for j=2:10
    for i=2:10
        phi(j+1,i)=phi(j,i+1)+phi(j,i-1)-phi(j-1,i);
    end
end

fprintf(1,'value of phi in matrix form \n')

fprintf(1,'By antennatutorials.com \n')
fprintf(1,'-------\t-------\t------\n')

phi
[x t]= meshgrid(x,t);
surf(x,t,phi)
axis([0 1 0 1 -1 1])
xlabel('X distance')
ylabel('time ')
zlabel('value of \phi')
text(0,0.4,0.4,'By antennatutorials.com');
title ('wave function as a function of time and distance By antennatutorials.com');

Results::


value of phi in matrix form
------- ------

phi =

         0    0.3090    0.5878    0.8090    0.9511    1.0000    0.9511    0.8090    0.5878    0.3090    0.0000
         0    0.2939    0.5590    0.7694    0.9045    0.9511    0.9045    0.7694    0.5590    0.2939         0
         0    0.2500    0.4755    0.6545    0.7694    0.8090    0.7694    0.6545    0.4755    0.2500         0
         0    0.1816    0.3455    0.4755    0.5590    0.5878    0.5590    0.4755    0.3455    0.1816         0
         0    0.0955    0.1816    0.2500    0.2939    0.3090    0.2939    0.2500    0.1816    0.0955         0
         0         0   -0.0000         0         0   -0.0000         0         0    0.0000   -0.0000         0
         0   -0.0955   -0.1816   -0.2500   -0.2939   -0.3090   -0.2939   -0.2500   -0.1816   -0.0955         0
         0   -0.1816   -0.3455   -0.4755   -0.5590   -0.5878   -0.5590   -0.4755   -0.3455   -0.1816         0
         0   -0.2500   -0.4755   -0.6545   -0.7694   -0.8090   -0.7694   -0.6545   -0.4755   -0.2500         0
         0   -0.2939   -0.5590   -0.7694   -0.9045   -0.9511   -0.9045   -0.7694   -0.5590   -0.2939         0
         0   -0.3090   -0.5878   -0.8090   -0.9511   -1.0000   -0.9511   -0.8090   -0.5878   -0.3090         0




No comments:

Post a Comment