Waves of a string

3 visualizaciones (últimos 30 días)
TTA
TTA el 5 de Oct. de 2016
Comentada: Geoff Hayes el 21 de Oct. de 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 comentarios
Hamza saeed khan
Hamza saeed khan el 18 de Oct. de 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 el 21 de Oct. de 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?

Iniciar sesión para comentar.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 6 de Oct. de 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 comentarios
TTA
TTA el 6 de Oct. de 2016
Thanks so much please let me work on this and I get back to you
TTA
TTA el 6 de Oct. de 2016
Thanks so much

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by