Subscripted assignment dimension mismatch - Help

조회 수: 5 (최근 30일)
Francis Mboya
Francis Mboya 2011년 4월 12일
Hello everybody i have been trying to code a 1D wave problem using the Leap Frog Scheme but i am running into an error that i am not familiar with. Any suggestions?
freq = 1; % Hz
omega = 2*pi*freq; % angular speed m/s
c = 10; % m/s
amp = 1;
% Space
dx = 0.001;
xmax = 1; % domain of unit length
lambda = 0.05; % size of wave
k = 2*pi/lambda; % wavenumber
x = 0:dx:xmax;
Vx = length(x); % matrix of propagation in the x direction
u_0 = zeros(size(x));
% defining wave initial conditions
for i = 1:Vx;
if x(i) < lambda;
u_0(i) = (1 - cos(k*x(i)))*0.5;
else
break
end
end
%Time
dt = 0.01;
t = 0:dt:1;
Vt = length(t);
figure(1)
plot(x,u_0,'.-b');
axis([0 lambda 0 1])
phi = zeros(Vx,Vt);
for n = 1:Vx;
phi(1,i) = u_0(i);
phi(2,i) = u_0(i);
end
for i = 2:Vt
for n = 2:Vx-1
phi(i,n+1) = phi(i,n-1) + (u_0*(dt/dx))*(phi(i+1,n) - phi(i-1,n)/phi(i,n-1));
end
end

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 4월 12일
It means you're trying to put more (or less) elements in a place than you've indexed.
%E.g.
A = [2, 3]
B = magic(3)
B(1,1:3) = A; %your error
What line does it say the error occurred on and what do you do for indexing at that line?

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 4월 12일
In your line
phi(i,n+1) = phi(i,n-1) + (u_0*(dt/dx))*(phi(i+1,n) - phi(i-1,n)/phi(i,n-1));
u_0 is a vector, so the right hand side is going to have a vector result, which you then try to store in to a single memory location.
Perhaps your right hand side should use u_0(i) ?
  댓글 수: 2
Francis Mboya
Francis Mboya 2011년 4월 12일
Thanks Walter, worked like a charm!!! I have been working on this for a week now and i was clueless. kudos
Francis Mboya
Francis Mboya 2011년 4월 13일
Hello Walter, I was wondering if there is a way to make the wave plot figure(1) move across the domain till it reaches xmax? Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by