Damped plucked string wave equation Fourier series assignment/ matrix dimensions problem

12 次查看(过去 30 天)
Hi there.
I'm having a little trouble implementing the solution to the damped 1-dimensional wave equation for a damped, plucked string. I would like to plot the position of a large number of points along the string (to represent the string itself), at discrete time instants.
My commented code is:
MATLAB code
L_s = 0.33; % length of string (m)
m = 0.000125; % mass of string (kg)
T = 68; % stretched tension of string (N)
R = 100; % linear resistance for string (Ns/m)
rho_L = m/L_s; % mass per unit length of string (kg/m)
c = sqrt(T/rho_L); % wave speed in string (m/s)
t = 1.3; % current time value (s)
d_0 = 0.2; % initial displacement value (m)
% range variables
x = linspace(0,L_s,1000); % position points on string (m)
n = linspace(1,length(x),length(x)); % integers for Fourier series
Omega_n = sqrt((c.*n.*pi./L_s).^2 - R^2); % argument coefficient for Fourier sine series
alpha_n = 8*d_0./(n.^2*pi^2).*sin(n.*pi./2); % first Fourier series coefficient
beta_n = R.*alpha_n./Omega_n; % second Fourier series coefficient
w = zeros(1,length(n),length(x)); % displacement function for string
for i = 1:length(n)
for j = 1:length(x)
w(i,j) = w + sin(n(i).*pi.*x(j)./L_s).*exp(-R./t).*(alpha_n(i).*cos(Omega_n(i).*t) ...
+ beta_n(i).*sin(Omega_n(i).*t));
end
end
hold on
figure(1)
plot(x,w)
xlabel('Position on string (m)')
ylabel('Displacement (m)')
xlim([0 0.33])
The error message is: "Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ass_assignment2 (line 39) w(i,j) = w + sin(n(i).*pi.*x(j)./L_s).*exp(-R./t).*(alpha_n(i).*cos(Omega_n(i).*t) ..."
Could anyone offer some advice? I understand the error message is telling me that the RHS comprises more dimensions than the LHS, but I cannot see why this would be.
Any help is much appreciated. Any general comments on more efficient and clearer coding techniques are also most welcome.
Regards,
Mike

采纳的回答

Jonathan Epperl
Jonathan Epperl 2013-1-24
In here
w(i,j) = w + sin(n(i).*pi.*x(j)./L_s).*exp(-R./t).*alpha_n(i).*cos(Omega_n(i).*t) ...
+ beta_n(i).*sin(Omega_n(i).*t));
the term w is a vector, as you defined it, the rest is all scalars, so what you will get is a vector with size size(w) with the scalars added to each element.
I'm also not sure why you initialize w as a 1 x length(n) x length(x) 3-D array, wouldn't a matrix be sufficient?
  5 个评论
Michael
Michael 2013-1-27
Thanks alot for the tips - much appreciated.
I managed to use the
sum(w,2)
function to combine all the string modes and give the position of the string at time t, as you suggest. The
exp(-R/t)
was indeed an error, it should be
exp(-Rt)
The
w(i,j) + ...
bit of the loop was unnecessary.
Sorted!
I'll look into use of the
diag
function to vectorise - it's new to me.
Thanks a lot for your help.
Jonathan Epperl
Jonathan Epperl 2013-1-27
Glad I could help. Vectorization is probably the most speed-gaining technique in Matlab, you should definitely get familiar with it. Functions that I find often helpful:
ones(), diag(), blkdiag(), repmat(), meshgrid(), ndgrid()

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by