Subscripted assignment dimension mismatch.

조회 수: 9 (최근 30일)
John
John 2014년 11월 29일
댓글: John 2014년 11월 29일
I am working on modeling a physical pendulum using the 4th order Runge-Kutta method and I don't understand why I am getting this error. As far as I can tell my dimensions match and am at a bit of a loss how to proceed. This is the line that is causing the error
'theta(i + h,:) = theta(i) + dtheta;'
w0 = 1;
alpha = 0.2;
f = 0.52;
w = 0.666;
h = 1;
t = 1:h:100;
%Create placement to put values generated
theta = zeros ( 1 , length(t));
v= zeros ( 1 , length(t) );
k_v1=zeros ( 1 , length(t) );
k_theta1=zeros ( 1 , length(t) );
%first initial conditions
theta(1)=-0.0885;
v(1)=0.8;
%Function....this will plot as it should be
F_t_theta_v = @(t,theta,v)-w0^2 * sin(theta) - alpha * v + f * cos(w*t);
%Apply 4th RK
for i=1:(length(t)-1);
k_v1 = h * F_t_theta_v( t(i), theta(i), v(i) );
k_theta1 = h * v;
k_v2 = h * F_t_theta_v ( (t(i) + 0.5 * h), (theta(i) + 0.5 * k_theta1), (v + 0.5 * k_v1) );
k_theta2 = h * (v + k_v1);
k_v3 = h * F_t_theta_v ( (t(i) + 0.5 * h), ( theta (i) + 0.5 * k_theta2), (v + k_v1) );
k_theta3 = h * ( v + k_v2 );
k_v4 = h * F_t_theta_v ( ( t(i) + h ), ( theta(i) + k_theta3), ( v + k_v1));
k_theta4 = h * ( v + k_v3 );
dtheta = 1/6 * (k_theta1 + 2 * k_theta2 + 2 * k_theta3 + k_theta4);
dv = 1/6 * (k_v1 + 2 * k_v2 + 2 * k_v3 + k_v4);
theta(i + h,:) = theta(i) + dtheta;
v (i + h,:) = v(i) + dv;
end

채택된 답변

Image Analyst
Image Analyst 2014년 11월 29일
편집: Image Analyst 2014년 11월 29일
You say:
theta(i + h,:) = theta(i) + dtheta;
as if theta is a 2D matrix. It's not. It's a 1 by 100 row vector. Get rid of the colon and index dtheta:
theta(i + h,:) = theta(i) + dtheta(i);
Now look at your equation for v and fix that similarly.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by