# Thread: matlab - defining boundaries

1. ## matlab - defining boundaries

Hi
I'm writing a script in matlab to simulate waves in a string such that "For this example, we assume a string of length 100 cm and a step size in x (along the string) of 1 cm. We shall assume a wave velocity of 1 cm/sec. You will write your program so that it computes the propogation of waves along the string for 200 seconds"
and
"for boundary conditions we will assume a triangular displacement with height of 1 cm located more or less in the center of the string. The slope of the sides of the triangle is .25 cm per cm"
You can see how this is illustrated here:
Slide 1
What I *want* to do is:
V(1,j) = 0.00;
V(2,j) = 0.25;
V(3,j) = 0.50;
V(4,j) = 0.75;
V(5,j) = 1.00;
although this isnt working.
My program looks like this:

Code:
% Julie Miller
% Exercise 17

dx = 1.0;
x = 0:dx:100.0;
dt = input('enter time step(s):  ');
t = 0:dt:200.0;

tlength = (200/dt)+1;
xlength = (100/dx)+1;

V = zeros(xlength,tlength);
V(1,j) = 0.00;
V(2,j) = 0.25;
V(3,j) = 0.50;
V(4,j) = 0.75;
V(5,j) = 1.00;

alpha = (dt^2)/(dx^2);
while alpha < 1.0;
for i = 2:xlength-1
V(i,2) = (alpha/2)*(V(i-1,1)+V(i+1,1))+(1-alpha)*(V(i,1));
end
for j = 2:tlength-1
for i = 2:xlength-1
V(i,j+1) = alpha*(V(i-1,j)+V(i+1,j))+2*(1-alpha)*(V(i,j)-V(i,j-1));
end
end
end

mesh(V)
....how do I get this slope of 25 cm / cm? Thanks very much

2. Originally Posted by plopony
Code:
% Julie Miller
% Exercise 17

dx = 1.0;
x = 0:dx:100.0;
dt = input('enter time step(s):  ');
t = 0:dt:200.0;

tlength = (200/dt)+1;
xlength = (100/dx)+1;

V = zeros(xlength,tlength);
V(1,j) = 0.00;
V(2,j) = 0.25;
V(3,j) = 0.50;
V(4,j) = 0.75;
V(5,j) = 1.00;

alpha = (dt^2)/(dx^2);
while alpha < 1.0;
for i = 2:xlength-1
V(i,2) = (alpha/2)*(V(i-1,1)+V(i+1,1))+(1-alpha)*(V(i,1));
end
for j = 2:tlength-1
for i = 2:xlength-1
V(i,j+1) = alpha*(V(i-1,j)+V(i+1,j))+2*(1-alpha)*(V(i,j)-V(i,j-1));
end
end
end

mesh(V)
What does the code in red above do? What value of j is it going to use?

Code in blue is an infinite loop or does nothing.

CB

3. the red code i know is incorrect. that is just me trying to take a stab at defining a boundary.

in the blue code, i've been instructed to take out the while loop. i was trying to set the condition that there is only stability if alpha < 1.