Subscripted assignment dimension mismatch.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Image Analyst
le 29 Nov 2014
Modifié(e) : Image Analyst
le 29 Nov 2014
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Assembly dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!