Waves of a string

3 ビュー (過去 30 日間)
TTA
TTA 2016 年 10 月 5 日
コメント済み: Geoff Hayes 2020 年 10 月 21 日
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 件のコメント
Hamza saeed khan
Hamza saeed khan 2020 年 10 月 18 日
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 2020 年 10 月 21 日
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?

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2016 年 10 月 6 日
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 件のコメント
TTA
TTA 2016 年 10 月 6 日
Thanks so much please let me work on this and I get back to you
TTA
TTA 2016 年 10 月 6 日
Thanks so much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by