Waves of a string
Afficher commentaires plus anciens
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 commentaires
TTA
le 5 Oct 2016
Hamza saeed khan
le 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
le 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?
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Entering Commands dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!