Waves of a string

3 views (last 30 days)
TTA
TTA on 5 Oct 2016
Commented: Geoff Hayes on 21 Oct 2020
Please I need this code to run. It saying it exceed matrix. this is the code and the function
% Solution of wave equation for string
% based on 'Computational Physics' book by N Giordano and H Nakanishi
%
clear all; clc;
string_dimension=100;
time_loops=1500;
% Preallocate matrices for speed;
x=1/string_dimension:1/string_dimension:1;
x_scale=1:1:string_dimension;
y_next =zeros(1,string_dimension);
signal_data=zeros(1,time_loops);
elapsed_time=zeros(1,time_loops);
% Initialise string position
k=1000;
x_0=0.5;
delta_t=3.33e-5;
f_sample=1/delta_t;
initial_position=exp(-k.*(x-x_0).^2);
y_current =initial_position;
y_previous = initial_position;
initial_time=0;
time=initial_time;
for time_step = 1:time_loops;
time=time+delta_t;
[y_next]=realistic(y_current, y_previous);
y_previous=y_current;
y_current=y_next;
clf;
subplot(2,2,1);
plot(x_scale/string_dimension, y_current,'r');
title('Waves on a string - fixed ends');
xlabel('distance');
ylabel('Displacement');
axis([0 1 -1 1]);
hold on;
%drawnow;
%%%%%%%
% Record displacement at 5 percent from left end of the string for future plot
signal_data(time_step)=y_current(40);
elapsed_time(time_step)=time;
subplot(2,2,2);
% plot displacement at 5 percent from left end of the string
% using suitable scaling
plot(elapsed_time,signal_data);
title('Signal from a string');
xlabel('time (s)');
ylabel('Displacement(au)');
end;
and the function
function [y_next] = realistic(y_current, y_previous)
r=1;
M=size(y_current,2);
y_next=zeros(1,M);
i=2:1:M-1;
%This loop index takes care of the fact that the boundaries are fixed
y_next(i) = (2-2*r^2-6*7.5e-6*r^2*90)*y_current(i)-y_previous(i)+...
(r^2+4*r^2*7.5e-6*90^2).*(y_current(i+1)+y_current(i-1))-...
r^2*7.5e-6*90^2.*(y_current(i+2)+y_current(i-2));
% y_next(1)=y_next(2);
% y_next(M)=y_next(M-1);
end
this is the error that is giving me
??? Index exceeds matrix dimensions.
Error in ==> realistic at 7
y_next(i) = (2-2*r^2-6*7.5e-6*r^2*90)*y_current(i)-y_previous(i)+...
Error in ==> Waves_6_16Debugg at 26
[y_next]=realistic(y_current, y_previous);
  3 Comments
Hamza saeed khan
Hamza saeed khan on 18 Oct 2020
I also tried the same code but the error is as follow:
>>Index exceeds matrix dimensions.
Error in realistic (line 9)
r^2*7.5e-6*90^2.*(y_current(i+2)+y_current(i-2));
Error in Untitled (line 25)
[y_next]=realistic(y_current, y_previous);
Please resolve this issue for my project. Thanks
Geoff Hayes
Geoff Hayes on 21 Oct 2020
Hamza - what integers does i iterate over? How do you ensure that i+2 and i-2 are valid indices into your y_current array?

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Oct 2016
Toyese - your comment is correct. The problem is with the i+2 and the i-2. Consider your code in the realistic function
i=2:1:M-1;
y_next(i) = (2-2*r^2-6*7.5e-6*r^2*90)*y_current(i)-y_previous(i)+...
(r^2+4*r^2*7.5e-6*90^2).*(y_current(i+1)+y_current(i-1))-...
r^2*7.5e-6*90^2.*(y_current(i+2)+y_current(i-2));
So i is an array of elements from 2 to M-1. (Note that you should rename this variable as MATLAB uses i and j to represent the imaginary number.)
In your assignment, look at how you index into y_current: using i, i+1, i-1, i+2, and i-2. Thus your array of elements will be offset by positive or negative one, and positive or negative two. This means that the array of elements then can range from 0 to M+1. As these elements are used as indices into y_current, you will get errors since the Subscript indices must either be real positive integers (which occurs for the i-2) and the index mustn't exceed the matrix bounds (which will fail for i+2). To get around this just change your array of elements to be
indices = 3:1:M-2;
and use this array when updating y_next and accessing y_current and y_previous. Note that since the minimum value for indices is 3 and the maximum is M-2, our offsets will ensure that we never have an index less than 1 and never exceeding M.
  2 Comments
TTA
TTA on 6 Oct 2016
Thanks so much please let me work on this and I get back to you
TTA
TTA on 6 Oct 2016
Thanks so much

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!